javax.mail在PC上运行但在服务器上运行

时间:2015-05-25 17:02:34

标签: java email javax.mail

我已经搜索但似乎无法找到类似的问题,更不用说答案了。我有一个在我的笔记本电脑(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.usermail.smtp.password,它们完全相同:s

我可以开始寻找任何线索吗?

1 个答案:

答案 0 :(得分:0)

刚收到Google的电子邮件和短信。 GMail认为我试图破解我的帐户。我很高兴这个问题有合理的答案: - )