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("Recipient@gmail.com"));
    generateMailMessage.addRecipient(Message.RecipientType.CC, new InternetAddress("Recipient@gmail.com"));
    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", "myusername@gmail.com", "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 ?
 
     
     
    