[英]javax.mail work on PC but not on server
我已經搜索過,但似乎找不到類似的問題,沒關系回答。 我在筆記本電腦(Windows 8),tomcat7上運行了一個Web應用程序,並且運行良好。 它按預期方式發送電子郵件。 我在Linux服務器上也運行了相同的代碼,同樣在tomcat7上運行,並且得到了javax.mail.AuthenticationFailedException
。
我從一開始就完成了Authenticator的工作:
...
Authenticator mailAuthenticator = new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(properties.getProperty("mail.smtp.user"),
properties.getProperty("mail.smtp.password"));
}
};
try {
// Get the default Session object.
Session session = Session.getInstance(properties, mailAuthenticator);
MimeMessage mimeMessage = new MimeMessage(session);
mimeMessage.setSubject("Subject, whoot whoot!");
mimeMessage.setFrom(new InternetAddress(properties.getProperty("mail.smtp.from")));
//This is an overkill
List<String> emailList = new LinkedList<>();
emailList.add("email@example.com");
for(String item : emailList) {
mimeMessage.addRecipients(Message.RecipientType.TO, InternetAddress.parse(item));
}
//The body ).(
StringBuilder sbMsg = new StringBuilder("Some text, etc.\n");
sbMsg.append("more text");
Multipart multipart = new MimeMultipart("alternative");
BodyPart messageBodyPart1 = new MimeBodyPart();
messageBodyPart1.setContent(sbMsg.toString(), "text/plain");
multipart.addBodyPart(messageBodyPart1);
mimeMessage.setContent(multipart);
Transport transport = session.getTransport(properties.getProperty("mail.transport"));
int port = Integer.parseInt(properties.getProperty("mail.smtp.port"));
//Exception happens on the line below
transport.connect(properties.getProperty("mail.smtp.host"),
port,
properties.getProperty("mail.smtp.user"),
properties.getProperty("mail.smtp.password"));
transport.sendMessage(mimeMessage, mimeMessage.getAllRecipients());
return true;
} catch (Exception e) {
//Pokemon exception handling :(
LOG.log(Level.SEVERE, "Error while sending email", e);
}
該異常發生在transport.connect
,沒有描述,除了:(
我檢查了mail.smtp.user
和mail.smtp.password
,它是完全相同的:s
我可以開始尋找任何線索嗎?
剛收到Google的電子郵件和文本。 GMail認為我正在嘗試入侵我的帳戶。 我很高興對此問題有一個合乎邏輯的答案:-)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.