0

我正在创建一个需要将电子邮件发送到特定邮件 ID 的 android 应用程序。我尝试使用意图(ACTION_SEND)。由于它需要用户交互,我没有使用这种方法。我想要的是通过后端发送电子邮件(在用户不知情的情况下)。谁能告诉我最合适的方式?提前致谢...

4

4 回答 4

1

互联网上有一些关于此的教程,这里有一个 -

于 2012-11-17T10:58:29.013 回答
0

使用此类发送电子邮件。使用用户名和密码作为参数创建此类的对象。

public class MailSender {
private String mailhost = "smtp.gmail.com";
private String user;
private String password;
private Session session;
private Multipart _multipart;
public BodyPart messageBodyPart;
DataSource source;
static {
    Security.addProvider(new com.PackageName.JSSEProvider());
}

public MailSender(String user, String password) {
    this.user = user;
    this.password = password;

    Properties props = new Properties();
    props.setProperty("mail.transport.protocol", "smtp");
    props.setProperty("mail.smtp.host", mailhost);
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.port", "465");
    props.put("mail.smtp.socketFactory.port", "465");
    props.put("mail.smtp.socketFactory.class",
            "javax.net.ssl.SSLSocketFactory");
    props.put("mail.smtp.socketFactory.fallback", "false");
    props.setProperty("mail.smtp.quitwait", "false");
    _multipart = new MimeMultipart();
    session = Session.getInstance(props, new MailAuthenticator(user,
            password));

}

public synchronized void sendMail(final String subject, final String uuid,
        final String address, final double latitude,
        final double longitude, final String recipients,
        final String filepath, final Context context, final int i, final int j)
        throws Exception {

    Thread thread = new Thread() {
        public void run() {
            Looper.prepare();
            try {
                System.out.println("SENDING       MAIL");
                Message message = new MimeMessage(session);
                messageBodyPart = new MimeBodyPart();
                DatabaseAdapter db = new DatabaseAdapter(context);
                String eId = db.getYourId();
                message.setFrom(new InternetAddress(eId));
                message.setSubject(subject);
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                String currentDate = sdf.format(new Date());
                SimpleDateFormat sTf = new SimpleDateFormat("hh:mm:ss");
                String currentTime = sTf.format(new Date());
                System.out.println(i);
                messageBodyPart
                        .setText("Someone tried to unlock your device(ID : "+uuid+" ) at: "
                                + currentTime + " on " + currentDate + "\n"
                                + "Device Location : " + address + "\n"
                                + "Map: " + "http://maps.google.com/?q="
                                + latitude + "," + longitude);
                if (recipients.indexOf(',') > 0)
                    message.setRecipients(Message.RecipientType.TO,
                            InternetAddress.parse(recipients));
                else
                    message.setRecipient(Message.RecipientType.TO,
                            new InternetAddress(recipients));

                Transport transport = session.getTransport("smtp");
                System.out.println("CONECTING.....");
                transport.connect(mailhost, user, password);
                message.saveChanges();
                message.setContent(_multipart);
                _multipart.addBodyPart(messageBodyPart);
                message.setContent(_multipart);
                if (source != null) {

                    message.setFileName("image");
                }
                Transport.send(message);
                System.out.println("Mail  sent ...");

                }

                transport.close();


            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    };
    thread.start();

}

public void addAttachment(String filename) throws Exception {
    if (!filename.equals("")) {
        System.out.println("Ataching     file        :)");
        messageBodyPart = new MimeBodyPart();
        source = new FileDataSource(filename);
        messageBodyPart.setDataHandler(new DataHandler(source));
        messageBodyPart.setFileName(filename);
        _multipart.addBodyPart(messageBodyPart);
        System.out.println("FILE    ATTACHED        :)");
    }
}


class MailAuthenticator extends Authenticator {
    String us;
    String pw;

    public MailAuthenticator(String username, String password) {
        super();
        this.us = username;
        this.pw = password;
    }

    public PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication(us, pw);
    }
}

}

还要将此添加到其他文件中。

public final class JSSEProvider extends Provider {
private static final long serialVersionUID = 1L;

public JSSEProvider() {
    super("HarmonyJSSE", 1.0, "Harmony JSSE Provider");
    AccessController
            .doPrivileged(new java.security.PrivilegedAction<Void>() {
                public Void run() {
                    put("SSLContext.TLS",
                            "org.apache.harmony.xnet.provider.jsse.SSLContextImpl");
                    put("Alg.Alias.SSLContext.TLSv1", "TLS");
                    put("KeyManagerFactory.X509",
                            "org.apache.harmony.xnet.provider.jsse.KeyManagerFactoryImpl");
                    put("TrustManagerFactory.X509",
                            "org.apache.harmony.xnet.provider.jsse.TrustManagerFactoryImpl");
                    return null;
                }
            });
}

}

于 2012-11-17T11:03:24.587 回答
0
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[]{"test@test.com"});
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Email subject");
emailIntent.putExtra(Intent.EXTRA_TEXT, "Message body");
emailIntent.setType("application/octet-stream");
startActivity(emailIntent);

您也可以使用 HTTP POST 并在服务器端编写特定方法。

于 2012-11-17T10:57:08.797 回答
0

您可以将 Java Mail API 用于特定的电子邮件 ID。你可以修复它。看这个链接

于 2012-11-17T10:57:24.630 回答