user3920553
user3920553

Reputation: 1

Javamail fail with an authentication Exception, why?

I'm working in a Java EE webApplication and I hope to send emails to my clients. So I add mail.jar and activation.jar and I tried this simple code:

import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class SendHTML {

static Properties mailServerProperties;
static Session getMailSession;
static MimeMessage generateMailMessage;

public static void main(String args[]) throws AddressException, MessagingException {
    generateAndSendEmail();
    System.out.println("\n\n ===> Your Java Program has just sent an Email successfully. Check your email..");
}

public static void generateAndSendEmail() throws AddressException, MessagingException {

//Step1    
    System.out.println("\n 1st ===> setup Mail Server Properties..");
    mailServerProperties = System.getProperties();
    mailServerProperties.put("mail.smtp.port", "587"); // TLS Port
    mailServerProperties.put("mail.smtp.auth", "true"); // Enable Authentication
    mailServerProperties.put("mail.smtp.starttls.enable", "true"); // Enable StartTLS
    System.out.println("Mail Server Properties have been setup successfully..");

//Step2    
    System.out.println("\n\n 2nd ===> get Mail Session..");
    getMailSession = Session.getDefaultInstance(mailServerProperties, null);
    generateMailMessage = new MimeMessage(getMailSession);
    generateMailMessage.addRecipient(Message.RecipientType.TO, new InternetAddress("[email protected]"));
    generateMailMessage.addRecipient(Message.RecipientType.CC, new InternetAddress("[email protected]"));
    generateMailMessage.setSubject("Greetings from me..");
    String emailBody = "Test email by me JavaMail API example. " + "<br><br> Regards, <br>Admin";
    generateMailMessage.setContent(emailBody, "text/html");
    System.out.println("Mail Session has been created successfully..");

//Step3    
    System.out.println("\n\n 3rd ===> Get Session and Send mail");
    Transport transport = getMailSession.getTransport("smtp");
    // Enter your correct gmail UserID and Password
    transport.connect("smtp.gmail.com", "[email protected]", "mypassword");
    transport.sendMessage(generateMailMessage, generateMailMessage.getAllRecipients());
    transport.close();
}
}

So Step1 and Step2 work successfully, but Step3 generate an Authentication exception:

1st ===> setup Mail Server Properties..
Mail Server Properties have been setup successfully..
2nd ===> get Mail Session..
Mail Session has been created successfully..
3rd ===> Get Session and Send mail
Exception in thread "main" javax.mail.AuthenticationFailedException
at javax.mail.Service.connect(Service.java:264)
at javax.mail.Service.connect(Service.java:134)
at entity.SendHTML.generateAndSendEmail(SendHTML.java:53)
at entity.SendHTML.main(SendHTML.java:24)

Please can someone help me to fixe the problem ?

Upvotes: 0

Views: 2078

Answers (1)

Bill Shannon
Bill Shannon

Reputation: 29961

First, there is nothing named "JEE", you meant "Java EE".

If you're using Java EE, you shouldn't need mail.jar and activation.jar, they're already part of every Java EE server.

You're going to want to read these JavaMail FAQ entries on common mistakes and how to debug JavaMail applications.

AuthenticationFailedException means the server doesn't like your username and password. You're probably running into the problem described here.

Upvotes: 1

Related Questions