【发布时间】: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