【问题标题】:Getting error failed to connect, no password specified? sporadically出现错误连接失败,没有指定密码?偶尔
【发布时间】:2015-09-03 09:34:45
【问题描述】:

我有一个奇怪的问题,我似乎无法解决。 :( 我有一个发送电子邮件的基于 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());

提前感谢您的任何帮助。

【问题讨论】:

  • 是否有其他库或线程(例如记录器或错误报告器)会更改 mail.smtp.host 或其他邮件属性?
  • 我不知道。我使用 Log4j。已知 Log4j 会做这样的事情吗?我不记得 Log4j 中有任何发送电子邮件的选项。我的系统中没有使用任何其他错误报告器。我从我的初始化 servlet 中的数据库表中读取了mail.smtp,host 的值,我首先在启动时加载。上面代码 sn-p 中的变量 mailhost 是一个静态变量,一旦调用执行邮件发送的类,就会分配该值。这个类中发送邮件的方法也是静态的。

标签: java tomcat7 jakarta-mail


【解决方案1】:

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

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

我与我的电子邮件管理员交谈,他告诉我,我们的电子邮件服务器使用的主要端口实际上是 25。 我没有改变创建会话的方式。至少现在还没有。顺便说一句,Bill 提供的那个链接读起来非常棒,我强烈建议您点击它并阅读它。

谢谢大家

【讨论】:

    【解决方案2】:

    【讨论】:

    • 嗨,比尔。谢谢您的建议。很想知道它背后的更多内容。但是如果问题没有随着其他“修复”而消失,可能会尝试它。 :)
    • 如果您点击链接,您会找到原因here。理解它很重要。再读一遍。
    【解决方案3】:

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

    Properties props = System.getProperties();
    

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

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

    Properties properties = new Properties();
    

    【讨论】:

      猜你喜欢
      • 2011-09-30
      • 1970-01-01
      • 2011-07-07
      • 2021-02-14
      • 1970-01-01
      • 1970-01-01
      • 2012-06-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多