【问题标题】:Not able to send mail from java mail APi from GAE无法从 GAE 的 java 邮件 APi 发送邮件
【发布时间】:2019-11-18 23:52:53
【问题描述】:

我正在尝试使用 Java 邮件 Api 发送电子邮件,并且我的应用程序部署在 google App 引擎上,

之前我可以通过我的 java 代码发送电子邮件,但最近它停止了,

下面是错误

javax.mail.AuthenticationFailedException: 534-5.7.14 <https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbuq 534-5.7.14 szPbAPjs-PmJgBd5xmPAVgRB8-3WxkoQy7WaGBIGRyltJO9LxKnw0oagw1e-jZeQcgABnK 534-5.7.14 mcxSUPyD4ohtBPkovBS45tnMElRdeWM7mzfw2wU3vqqQpDw7_8Z9rphQR_SZpR> Please 534-5.7.14 log in via your web browser and then try again. 534-5.7.14 Learn more at 534 5.7.14 https://support.google.com/mail/answer/78754 186sm22205348pfb.99 - gsmtp

我已经完成了所有工作,在 IAM 中添加了 gmail 帐户作为所有者在 gmail 中启用了不太安全的选项,但我仍然遇到同样的错误

这里是代码

public static void sendWelcomeMail(String msg,String email,String subject) {
         System.out.println("Starte - "+email);
       final String username = "serv*******@gmail.com";
       final String password = "********";

       Properties prop = new Properties();
       prop.put("mail.smtp.host", "smtp.gmail.com");
       prop.put("mail.smtp.port", "465");
       prop.put("mail.smtp.auth", "true");
       prop.put("mail.smtp.socketFactory.port", "465");
       prop.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");

       Session session = Session.getInstance(prop,
               new javax.mail.Authenticator() {
                   protected PasswordAuthentication getPasswordAuthentication() {
                       return new PasswordAuthentication(username, password);
                   }
               });

       try {

        Message message = new MimeMessage(session);
           message.setFrom(new InternetAddress(username));
           message.setRecipients(
                   Message.RecipientType.TO,
                   InternetAddress.parse(email+", fo*********@gmail.com")
           );
           message.setSubject(subject);
           Multipart multipart = new MimeMultipart(); //1
        // Create the HTML Part
        BodyPart htmlBodyPart = new MimeBodyPart(); //4
        htmlBodyPart.setContent(msg, "text/html"); //5

        multipart.addBodyPart(htmlBodyPart); // 6
        // Set the Multipart's to be the email's content
        message.setContent(multipart); //7 

           Transport.send(message);

           System.out.println("Done");
       }catch(Exception e) {
        e.printStackTrace();
       }
    }

但我可以从我的本地机器发送电子邮件。 让我知道我做错了什么。

TIA

【问题讨论】:

  • 添加此行后,message.setFrom(new InternetAddress(username, " Admin"));new InternetAddress("fo***********@gmail.com", "") 工作正常
  • 您介意编辑您的帖子以包含工作代码吗?
  • 是的,我会更新帖子

标签: java google-app-engine jakarta-mail


【解决方案1】:

以下是我用来解决问题的更改。

我更改了这部分代码:

message.setRecipients(
                   Message.RecipientType.TO,
                   InternetAddress.parse(email+", fo*********@gmail.com")
       );

到这里:

message.addRecipient(
                   Message.RecipientType.TO,
                   new InternetAddress(email, "")
           );

还有这段代码的另一部分:

message.setFrom(new InternetAddress(username));

到这里:

message.setFrom(new InternetAddress(username, "Admin"));

因此这里是完整的代码:

public static void sendWelcomeMail(String msg,String email,String subject) {
       final String username = "ser******@gmail.com";
       final String password = "**********";

       Properties prop = new Properties();
       prop.put("mail.smtp.host", "smtp.gmail.com");
       prop.put("mail.smtp.port", "465");
       prop.put("mail.smtp.auth", "true");
       prop.put("mail.smtp.starttls.enable", "true");
       prop.put("mail.smtp.socketFactory.port", "465");
       prop.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");

       Session session = Session.getInstance(prop,
               new javax.mail.Authenticator() {
                   protected PasswordAuthentication getPasswordAuthentication() {
                       return new PasswordAuthentication(username, password);
                   }
               });

       try {

        Message message = new MimeMessage(session);
           message.setFrom(new InternetAddress(username, "Admin"));
           message.addRecipient(
                   Message.RecipientType.TO,
                   new InternetAddress(email, "")
           );
           message.addRecipient(
                   Message.RecipientType.CC,
                   new InternetAddress("fo********@gmail.com", "")
           );
           message.setSubject(subject);
           Multipart multipart = new MimeMultipart(); //1
        // Create the HTML Part
        BodyPart htmlBodyPart = new MimeBodyPart(); //4
        htmlBodyPart.setContent(msg, "text/html"); //5

        multipart.addBodyPart(htmlBodyPart); // 6
        // Set the Multipart's to be the email's content
        message.setContent(multipart); //7 

           Transport.send(message);

           System.out.println("Done");
       }catch(Exception e) {
           System.out.println("Exception in sending mail - "+e.getMessage());
        e.printStackTrace();
       }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-11-04
    • 2013-01-07
    • 1970-01-01
    • 2016-09-16
    • 1970-01-01
    • 2010-11-20
    • 2012-09-11
    • 1970-01-01
    相关资源
    最近更新 更多