Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用JavaMail发送电子邮件时出错_Java_Jakarta Mail - Fatal编程技术网

使用JavaMail发送电子邮件时出错

使用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

以下代码仅在Google的“允许不太安全的应用”处于启用状态时有效,如果该功能处于禁用状态,我将获得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 {

    }
}

您需要使用OAuth。这可能会有帮助: