Jeff
Jeff

Reputation: 13

Authentication Error with Java Mail

I'm using the Java Mail API for the first time and I can't get things working. My server requires authentication, so I'm having to use that. I keep getting the following error:

> 250-PIPELINING 250-SIZE 40960000
> 250-ETRN 250-STARTTLS 250-AUTH PLAIN
> LOGIN 250-AUTH=PLAIN LOGIN
> 250-ENHANCEDSTATUSCODES 250 8BITMIME
> DEBUG SMTP: Found extension
> "PIPELINING", arg "" DEBUG SMTP: Found
> extension "SIZE", arg "40960000" DEBUG
> SMTP: Found extension "ETRN", arg ""
> DEBUG SMTP: Found extension
> "STARTTLS", arg "" DEBUG SMTP: Found
> extension "AUTH", arg "PLAIN LOGIN"
> DEBUG SMTP: Found extension
> "AUTH=PLAIN", arg "LOGIN" DEBUG SMTP:
> Found extension "ENHANCEDSTATUSCODES",
> arg "" DEBUG SMTP: Found extension
> "8BITMIME", arg "" DEBUG SMTP: Attempt
> to authenticate DEBUG SMTP: check
> mechanisms: LOGIN PLAIN AUTH LOGIN 334
> Base64text base64text 334 base64text
> base64text 235 2.7.0 Authentication
> successful DEBUG: getProvider()
> returning
> javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun
> Microsystems, Inc] DEBUG SMTP: useEhlo
> true, useAuth true
> javax.mail.AuthenticationFailedException:
> failed to connect, no password
> specified?
>         at javax.mail.Service.connect(Service.java:329)
>         at javax.mail.Service.connect(Service.java:176)
>         at javax.mail.Service.connect(Service.java:125)
>         at javax.mail.Transport.send0(Transport.java:194)
>         at javax.mail.Transport.send(Transport.java:124)
>         at Mailman.main(Mailman.java:61)

As you can see, I'm getting an "Authentication successful" message, but then it kicks me out on an "AuthenticationFailedException." I'm stumped...

Here's the relevant part of the source:

properties.setProperty("mail.smtp.host", host);               
      properties.setProperty("mail.smtp.port", "25");


      properties.setProperty("mail.smtp.user", "myemailhere");
      properties.setProperty("mail.smtp.password", "mypasshere");
      properties.setProperty("mail.smtp.auth", "true");       
      properties.setProperty("mail.debug", "true");


      // Get the default Session object.
      Session session = Session.getDefaultInstance(properties);

      try{
         // Create a default MimeMessage object.
         MimeMessage message = new MimeMessage(session);

         // Set From: header field of the header.
         message.setFrom(new InternetAddress(from));

         // Set To: header field of the header.
         message.addRecipient(Message.RecipientType.TO,
                                  new InternetAddress(to));

         // Set Subject: header field
         message.setSubject("This is the Subject Line!");

         // Now set the actual message
         message.setText("This is actual message");

         Transport transport = session.getTransport("smtp");
            transport.connect(host, 25, "myemailhere", "mypasshere");
            message.saveChanges();
            Transport.send(message);



         // Send message
         Transport.send(message);
         System.out.println("Sent message successfully....");
      }catch (MessagingException mex) {
         mex.printStackTrace();          
      }       

Any suggestions would be GREATLY appreciated...

Upvotes: 1

Views: 7901

Answers (1)

MJB
MJB

Reputation: 9389

I changed it to

message.setText("This is actual message");

             Transport transport = session.getTransport("smtp");
                transport.connect( null,smtpUser,smtpPassword); //host, 25, "myemailhere", "mypasshere");
                message.saveChanges();
                transport.sendMessage(message,message.getAllRecipients());

//              Transport.send(message);



             // Send message
            // Transport.send(message);
             System.out.println("Sent message successfully....");

and it worked

Upvotes: 1

Related Questions