在不使用 built-in 应用程序的情况下发送 e-mail
Sending an e-mail without using the built-in app
我需要在没有用户输入的情况下自动发送带附件的 e-mail。我以前读过这两个答案。
Sending Email in Android using JavaMail API without using the default/built-in app
Android: Send e-mail with attachment automatically in the background.
但现在我没有收到任何 e-mail 并且我已经在 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");
}
编辑:我不想像标题中建议的那样使用 built-in 应用程序。只是想说清楚。
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
修复所有这些问题 common mistakes,包括删除 Authenticator。
去掉构造函数中对 super() 的调用,你不需要它。
此外,删除 ByteArrayDataSource class。您不需要自己的,因为它 comes with JavaMail。另外,您的程序甚至没有使用它。
如果仍然无效,请使用新代码和新的故障详细信息更新您的post。
我需要在没有用户输入的情况下自动发送带附件的 e-mail。我以前读过这两个答案。
Sending Email in Android using JavaMail API without using the default/built-in app
Android: Send e-mail with attachment automatically in the background.
但现在我没有收到任何 e-mail 并且我已经在 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");
}
编辑:我不想像标题中建议的那样使用 built-in 应用程序。只是想说清楚。
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
修复所有这些问题 common mistakes,包括删除 Authenticator。
去掉构造函数中对 super() 的调用,你不需要它。
此外,删除 ByteArrayDataSource class。您不需要自己的,因为它 comes with JavaMail。另外,您的程序甚至没有使用它。
如果仍然无效,请使用新代码和新的故障详细信息更新您的post。