我在OpenShift上部署了一个应用程序,我需要从一个Google帐户发送电子邮件。
在本地部署应用程序,它完美运行;但是,当在OpenShift上部署的应用程序尝试相同时,我收到以下授权错误:
19:23:16,265 ERROR [stderr](默认任务-2)javax.mail.AuthenticationFailedException:535-5.7.8不接受用户名和密码。了解更多信息 https://support.google.com/mail/answer/14257 wn10sm1673177wjc.46 - gsmtp
19:23:16,266错误[stderr](默认任务-2)at com.sun.mail.smtp.SMTPTransport $ Authenticator.authenticate(SMTPTransport.java:843)
其他一切在我的OpenShift部署中完美运行。
这就是我的实现:
Properties props = new Properties();
String host = "smtp.gmail.com";
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", host);
props.put("mail.smtp.user", myEmail);
props.put("mail.smtp.password", password);
props.put("mail.smtp.port", "587");
props.put("mail.smtp.auth", "true");
Session session = Session.getDefaultInstance(props);
MimeMessage message = new MimeMessage(session);
try {
message.setFrom(new InternetAddress(myEmail));
InternetAddress toAddress = new InternetAddress(myEmail);
message.addRecipient(Message.RecipientType.TO, toAddress);
message.setSubject(title);
message.setText(msg);
Transport transport = session.getTransport("smtp");
transport.connect(host, myEmail, password);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
}
catch (AddressException ae) {
ae.printStackTrace();
}
catch (MessagingException me) {
me.printStackTrace();
}
我尝试了以下解决方案在URL中提出的异常跟踪解决方案以及其他SO问题的答案: