使用JavaMail发送电子邮件时出错
以下代码仅在Google的“允许不太安全的应用”处于启用状态时有效,如果该功能处于禁用状态,我将获得javax.mail.AuthenticationFailedException,当该功能处于禁用状态时,我如何使其工作使用JavaMail发送电子邮件时出错,java,jakarta-mail,Java,Jakarta Mail,以下代码仅在Google的“允许不太安全的应用”处于启用状态时有效,如果该功能处于禁用状态,我将获得javax.mail.AuthenticationFailedException,当该功能处于禁用状态时,我如何使其工作 public static void main(String[] args) { String username = "USER-NAME@gmail.com"; String password = "PASSWORD"; String to = "US
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 {
}
}
您需要使用OAuth。这可能会有帮助: