3

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.

LostAndConfused
  • 157
  • 2
  • 3
  • 15
  • 1
    Is there another library or thread, such as a logger or error reporter, that changes `mail.smtp.host` or other mail properties? – nanofarad Jun 17 '15 at 22:43
  • None that I'm aware of. I use Log4j. Is Log4j known to do such things? I don't recall any options in Log4j to send emails. I don't use any other error reporter in my system. I read the value of `mail.smtp,host` from a DB table in my Initialization servlet that I load-on-startup first. The variable mailhost that is in the code snippet above is a static and is assigned that value once the class that does the mail sending is invoked. The method that does the mail sending in this class is also static. – LostAndConfused Jun 18 '15 at 00:05

3 Answers3

5

Posting the answer for those who may run into a similar problem. Setting the following 2 properties seems to have done the trick, at least so far. :)

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

I spoke to my email admin and he told me that the main port that our email server uses is in fact 25. I did not change the way I create the session. At least not yet. BTW that link that Bill provided is an outstanding read and I highly recommend clicking on it and reading it.

Thanks everyone

LostAndConfused
  • 157
  • 2
  • 3
  • 15
3

Change Session.getDefaultInstance to Session.getInstance and see if that helps.

Bill Shannon
  • 29,579
  • 6
  • 38
  • 40
  • Hi Bill. Thank you for your suggestion. Would love to know more of what's behind it. But will probably try it anyway if the problem doesn't go away with other "fixes". :) – LostAndConfused Jun 18 '15 at 06:33
  • If you followed the links, you found the reason [here](http://www.oracle.com/technetwork/java/javamail/faq/index.html#getdefaultinstance). It's important that you understand it. Read it again. – Bill Shannon Jun 18 '15 at 20:39
0

I've got the same problem and finally I've solved it. The main problem is using system global properties:

Properties props = System.getProperties();

Other thread in your process can set mail.smtp.auth to true.

You should just use you own local properties:

Properties properties = new Properties();
Yuriy
  • 1
  • 1