javax.mail.AuthenticationException-Google云应用引擎-春季启动

时间:2019-03-04 19:56:09

标签: java spring-boot email google-cloud-platform google-cloud-messaging

当我需要在特定时间发送邮件时,我目前正在使用Spring Boot应用程序,因此我使用 javax.mail API进行了此操作,该程序在本地运行良好机器,但是当我在Google Cloud App Engine中部署应用程序时,我不断收到以下错误:

javax.mail.AuthenticationFailedException

关于邮寄要求,我尝试按照Google Cloud文档的说明进行操作,但我一直遇到相同的错误。

遵循我的代码。*

                public boolean sendMail(String indicator, String destination, String hotelName, String username, String password) {
            Properties props = new Properties();
            props.put("mail.smtp.host", "smtp.gmail.com");
            props.put("mail.smtp.starttls.enable", "true");
            props.put("mail.smtp.auth", "true");
            props.put("mail.smtp.port", "587");

            Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator() {
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(env.getProperty(IConstants.mailAddress),
                            env.getProperty(IConstants.mailPass));
                }
            });

            String indic = null;
            if(indicator.equals("R")){
                indic = "RECEPTIONIST";
            }

            if(indicator.equals("M")){
                indic = "ROOMMAID";
            }

            try {

                Message message = new MimeMessage(session);
                message.setFrom(new InternetAddress(env.getProperty(IConstants.mailAddress)));
                message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(destination));
                message.setSubject(indic + "Hotel " + hotelName + " - Credentials");
                message.setText("Hello " + hotelName
                        + " Employee, Down Below Your Credentials for a secure access to your workspace " + ".\n USERNAME: "
                        + username + " PASSWORD: " + password + ".\n" + "Greetings.");
                Transport.send(message);
                return true;
            } catch (MessagingException e) {
                throw new RuntimeException(e);
            }
        }

发件人邮件在授权邮件发件人列表上,所有内容均已正确配置,应用程序响应所有请求,但在发送邮件步骤中始终失败。

我在发件人gmail帐户上启用了安全性较低的应用程序身份验证选项,但它一直拒绝身份验证尝试。

谢谢。

2 个答案:

答案 0 :(得分:0)

我一直在尝试多种配置以使其正常运行,但没有成功,因此我切换到另一个选项,我实施了 sendGrid 解决方案, google cloud 将其列出为一种可能的替代方法,它是如此简单,如此安全,并且从第一次尝试开始就起作用。

SendGrid implementation

感谢您的建议

答案 1 :(得分:0)

您使用的是标准环境还是灵活环境。在我们的案例中,我们在Google smtp邮件中使用了灵活的环境。 对我们来说很好。而且我怀疑您的问题出在Google邮件帐户的第三方访问检查中。 启用此选项后,请尝试相同的smtp,以便您的应用程序能够访问gmail smtp。

  

通过屏幕快照中显示的设置打开对smtp正在使用的帐户的访问权限

enter image description here

尽管sendgrid是一种不错的邮件服务。但是我们只喜欢google smtp。