我正在android上尝试一个电子邮件应用程序。我首先输入用户名和密码,
然后在下一个活动中我接受收件人地址主题和味精并发送电子邮件。我收到错误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);
}
}
}