简体   繁体   English

javax.mail可在PC上运行,但不能在服务器上运行

[英]javax.mail work on PC but not on server

I've search but can't seem to find a similar problem, never mind an answer. 我已经搜索过,但似乎找不到类似的问题,没关系回答。 I have a web app running on my laptop (windows 8), tomcat7 and it works fine. 我在笔记本电脑(Windows 8),tomcat7上运行了一个Web应用程序,并且运行良好。 It sends out the email as expected. 它按预期方式发送电子邮件。 I have the same code running on my linux server, also tomcat7 and I get javax.mail.AuthenticationFailedException . 我在Linux服务器上也运行了相同的代码,同样在tomcat7上运行,并且得到了javax.mail.AuthenticationFailedException

I've done the Authenticator thing from the start: 我从一开始就完成了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);
}

The exception happens on transport.connect , and there is no description with the exception :( 该异常发生在transport.connect ,没有描述,除了:(

I've checked the mail.smtp.user and mail.smtp.password and it is exactly the same :s 我检查了mail.smtp.usermail.smtp.password ,它是完全相同的:s

Any clues where I can start looking? 我可以开始寻找任何线索吗?

Just got an emails and text from Google. 刚收到Google的电子邮件和文本。 GMail thought I was trying to hack my account. GMail认为我正在尝试入侵我的帐户。 I'm glad there is a logical answer to this problem :-) 我很高兴对此问题有一个合乎逻辑的答案:-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM