我正在 android 上试用电子邮件应用程序。我先输入用户名和密码
然后在下一个 Activity 中,我获取收件人地址主题和消息并发送电子邮件。
我收到错误 JavaX 身份验证失败。这是我的邮件身份验证和发送代码。发送按钮的 OnClick 函数调用此类
public class MailHandler {
final String username;
final String password;
public MailHandler(String username,String password){
this.username=username;
this.password=password;
}
public void sendMail(String Sub,String msg,String sender,String cc,String to) {
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(sender));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(to));
message.setSubject(Sub);
message.setText(msg);
Transport.send(message);
System.out.println("Done");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
}
最佳答案
一个简单的解决方案是使用最后的String
参数,比如
public void sendMail(final String username ,final String password );
.
关于javax 认证失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11989057/