Getting error failed to connect, no password specified? sporadically

10,455

Solution 1

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

Solution 2

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

Share:
10,455
LostAndConfused
Author by

LostAndConfused

Updated on June 04, 2022

Comments

  • LostAndConfused
    LostAndConfused almost 2 years

    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
    LostAndConfused almost 9 years
    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". :)
  • Bill Shannon
    Bill Shannon almost 9 years
    If you followed the links, you found the reason here. It's important that you understand it. Read it again.