以下代码仅适用于Google"允许安全性较低的应用程序"是ON,如果该功能关闭,我得到 javax.mail.AuthenticationFailedException ,当该功能关闭时如何让它工作?
public static void main(String[] args) {
String username = "USER-NAME@gmail.com";
String password = "PASSWORD";
String to = "USER-NAME@msn.com";
Properties properties = new Properties();
properties.setProperty("mail.smtp.auth", "true");
properties.setProperty("mail.smtp.starttls.enable", "true");
properties.setProperty("mail.smtp.host", "smtp.gmail.com");
properties.setProperty("mail.smtp.port", "587");
properties.list(System.out);
Session session = Session.getInstance(properties);
MimeMessage msg = new MimeMessage(session);
try {
msg.setFrom(new InternetAddress(username));
msg.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
msg.setSubject("This is the subject line!");
msg.setText("This is the actual message");
System.out.println("Sending message...");
Transport.send(msg, username, password);
System.out.println("Message sent!");
} catch (MessagingException ex) {
Logger.getLogger(Mail.class.getName()).log(Level.SEVERE, null, ex);
} finally {
}
}