获取错误连接失败,没有指定密码?偶尔

时间:2015-06-17 22:35:15

标签: java tomcat7 javamail

我有一个奇怪的问题,我似乎无法掌握。 :( 我有一个基于Web的应用程序发送电子邮件。它通过连接在本地网络上设置的基于Windows的SMTP服务器来实现。此SMTP服务器不需要我的代码中的用户名或密码即可发送电子邮件。一天的大部分时间,有时大部分时间,一切都很好,电子邮件发送,用户很高兴。然后无处不在,我没有明显的理由在我的日志中看到异常,并说:

javax.mail.AuthenticationFailedException: failed to connect, no password specified?
at javax.mail.Service.connect(Service.java:398)
at javax.mail.Service.connect(Service.java:245)
at javax.mail.Service.connect(Service.java:194)
at javax.mail.Transport.send0(Transport.java:253)
at javax.mail.Transport.send(Transport.java:124)

我将我的java邮件jar升级到了最近的版本,我猜这些天被称为javax.mail.far。我们正在运行Tomcat 7,而Windows Server 2008R2和邮件服务器是Microsoft的。

我不明白为什么这种方法有时会起作用,但后来没有明显的原因停止。但我真正想做的是解决这个问题,以免它再次出现。如果有人之前见过这样的事情并有任何想法,我很乐意听到。这是发送电子邮件的Java代码:

 Properties props = System.getProperties();
 if (mailhost != null)
    props.setProperty("mail.smtp.host", mailhost);

 // Get a Session object

 Session session = Session.getDefaultInstance(props);
 // Output the email in the log window
 session.setDebug(true);

 // construct the message
 Message msg = new MimeMessage(session);
 if (from != null)
    msg.setFrom(new InternetAddress(from));
 else
    msg.setFrom();
 msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(email.getTo(), false));
 if ((email.getCc() != null) && (email.getCc().length() > 0))
    msg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(email.getCc(), false));
 if ((email.getBcc() != null) && (email.getBcc().length() > 0))
    msg.setRecipients(Message.RecipientType.BCC,    InternetAddress.parse(email.getBcc(), false));

 msg.setSubject(email.getSubject());

 // Check if Attachment file exists
 if ((attachmentFile != null) && (attachmentFile.getFileName() != null) &&
     (attachmentFile.getFileName().length() > 0) )
 {
    MimeMultipart multipart = new MimeMultipart();
    // Set the Message Text
    MimeBodyPart messageBodyPart = new MimeBodyPart();
    messageBodyPart.setText(email.getBody());
    multipart.addBodyPart(messageBodyPart);
    // Set the Message Attachment
    MimeBodyPart attachmentBodyPart = new MimeBodyPart();
    DataSource ds = new ByteArrayDataSource(attachmentFile.getInputStream(), attachmentFile.getContentType());
    attachmentBodyPart.setDataHandler(new DataHandler(ds));
    attachmentBodyPart.setFileName(attachmentFile.getFileName());

    multipart.addBodyPart(attachmentBodyPart);
    // If we also have a PDF attachment
    if (PDFAtch != null)
    {
       MimeBodyPart pdfAttachmentBodyPart = new MimeBodyPart();
       ds = new ByteArrayDataSource(PDFAtch.getAttachment(), "application/pdf");
       pdfAttachmentBodyPart.setDataHandler(new DataHandler(ds));
       pdfAttachmentBodyPart.setFileName(PDFAtch.getFilename());
       multipart.addBodyPart(pdfAttachmentBodyPart);
    }
    // Put parts in message
    msg.setContent(multipart);
 }
 else if (PDFAtch != null)
 {
    MimeMultipart multipart = new MimeMultipart();
    // Set the Message Text
    MimeBodyPart messageBodyPart = new MimeBodyPart();
    messageBodyPart.setText(email.getBody());
    multipart.addBodyPart(messageBodyPart);

    MimeBodyPart pdfAttachmentBodyPart = new MimeBodyPart();
    DataSource ds = new ByteArrayDataSource(PDFAtch.getAttachment(), "application/pdf");
    pdfAttachmentBodyPart.setDataHandler(new DataHandler(ds));
    pdfAttachmentBodyPart.setFileName(PDFAtch.getFilename());
    multipart.addBodyPart(pdfAttachmentBodyPart);

    msg.setContent(multipart);
 }
 else
    msg.setText(email.getBody());

 msg.setHeader("X-Mailer", "EWarranty MailSender");
 msg.setSentDate(email.getDateSent());

 // send the thing off
 Transport.send(msg);

 logger.debug("Message sent successfully to "+email.getTo());

提前感谢您提供任何帮助。

3 个答案:

答案 0 :(得分:3)

为那些可能遇到类似问题的人发布答案。 设置以下2个属性似乎已经成功,至少到目前为止。 :)

    props.setProperty("mail.smtp.auth", "false");
    props.put("mail.smtp.port", "25"); // Default port

我与我的电子邮件管理员交谈,他告诉我,我们的电子邮件服务器使用的主要端口实际上是25。 我没有改变创建会话的方式。至少还没有。比尔提供的链接是一个很好的读物,我强烈建议点击它并阅读它。

谢谢大家

答案 1 :(得分:2)

答案 2 :(得分:0)

我遇到了同样的问题,终于解决了。 主要问题是使用系统全局属性:

Properties props = System.getProperties();

您进程中的其他线程可以将mail.smtp.auth设置为true。

您应该只使用自己的本地属性:

Properties properties = new Properties();
相关问题