javax.mail 可以在 PC 上工作,但不能在服务器上工作

javax.mail work on PC but not on server

我已经搜索过了,但似乎找不到类似的问题,更不用说答案了。我的笔记本电脑 (windows 8)、tomcat7 上有一个网络应用程序 运行,它运行良好。它按预期发送电子邮件。我的 linux 服务器上有相同的代码 运行,还有 tomcat7,我得到 javax.mail.AuthenticationFailedException.

我从一开始就完成了身份验证器的工作:

...
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

有什么线索可以开始寻找吗?

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