I have a bizarre problem that I can't seem to get a handle on. :( I have a Web based application that sends emails. It does so by connecting a Windows based SMTP server that was setup on a local network. This SMTP server does not require a username or a passord from my code in order to send the emails. Most of the day and sometimes most of the week everything works beautifully, the emails are sent and users are happy. Then out of nowhere and for no apparent reason I start seeing the Exception in my log that says:
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)
I upgraded my java mail jar to the latest version hich I guess is called javax.mail.far these days. We're running Tomcat 7, and Windows Server 2008R2 and the mail server is a Microsoft one.
I don't understand why this works sometimes but then stops for no apparent reason. But what I really would like to do is fix this issue so that it does not appear again. If any one has seen something like this before and has any ideas I would love to hear them. Here's the Java code that sends the emails:
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());
Thank you in advance for any and all help.