【发布时间】:2015-11-05 05:53:14
【问题描述】:
我需要在没有用户输入的情况下自动发送带有附件的电子邮件。我以前读过这两个答案。
Sending Email in Android using JavaMail API without using the default/built-in app
Android: Send e-mail with attachment automatically in the background.
但现在我没有收到任何电子邮件,而且我已经在 gmail 中启用了不太安全的应用程序。这是我的代码:
public class Mail {
private String mailhost;
private String user;
private String password;
private Session session;
private String serverport;
public void main(String user, String password, String subject, String body){
sendFromGMail(user, password, subject,body);
}
private void sendFromGMail(String from, String pass, String subject, String body){
Properties props = System.getProperties();
String host = "smtp.gmail.com";
props.put("mail.smtp.ssl.enable", "true");
props.put("mail.smtp.host", host);
props.put("mail.smtp.user", from);
props.put("mail.smtp.password", pass);
props.put("mail.smtp.port", "465");
props.put("mail.smtp.auth", "true");
Session session = Session.getInstance(props);
MimeMessage message = new MimeMessage(session);
try {
InternetAddress adressTo = new InternetAddress(from);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO, adressTo);
message.setSubject(subject);
message.setText(body);
Transport transport = session.getTransport("smtp");
transport.connect(host, from, pass);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
}
catch(MessagingException e){
Log.e("Not Sent", e.getMessage(), e);
}
}
}
这就是我使用它的方式:
try {
Mail mail = new Mail();
mail.main(gmailusername, gmailpassword, "Test", "Test2");
this.publishProgress("Email Sent");
}catch(Exception e){
Log.e("SendMail", e.getMessage(), e);
this.publishProgress("Email not sent");
}
编辑:我不想像标题所建议的那样使用内置应用程序。只想说清楚。
Java 调试:
08-12 15:08:41.152 24062-24340/com.documax.cardreader I/System.out﹕ DEBUG: setDebug: JavaMail version 1.4.1
08-12 15:08:41.162 24062-24340/com.documax.cardreader I/System.out﹕ DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc.,1.4.1]
08-12 15:08:41.162 24062-24340/com.documax.cardreader I/System.out﹕ DEBUG SMTP: useEhlo true, useAuth true
08-12 15:08:41.162 24062-24340/com.documax.cardreader I/System.out﹕ DEBUG SMTP: trying to connect to host "smtp.gmail.com", port 465, isSSL false
【问题讨论】:
-
那些错误是......?
-
@Nouman Ghaffar 你读过我的问题吗?我已经读过这个,这不是我要找的。不完全。
-
@ChristopheRoberge:我们可以使用 javaMail API 来接收电子邮件吗?我正在制作一个应用程序。阻止电子邮件。你认为 JavaMail 有可能吗?
标签: android email gmail jakarta-mail stack-overflow