Sumit Ghosh
Sumit Ghosh

Reputation: 39

Getting javax.mail.AuthenticationFailedException in Tomcat7

The program attempts to send e-mail but throws a run time exception:AuthenticationFailedException.I have used smtp protocol for sending the mail.I have used TLS.Below is my Mailsender class named SendMail.java

  package com.xxx.dashboard.mail;

  import java.util.Map;
  import java.util.Properties;
  import javax.mail.Message;
  import javax.mail.MessagingException;
  import javax.mail.PasswordAuthentication;
  import javax.mail.Session;
  import javax.mail.Transport;
  import javax.mail.internet.InternetAddress;
  import javax.mail.internet.MimeMessage;
  import com.edifixio.dashboard.configuration.ConfigurationBundle;

  public class SendMail {

  public static void sendMail(String toAddresses, String subject, String url, String name, String email){

    Map<String, String> mapdata = ConfigurationBundle.getMapdata();
    final String emailId = mapdata.get("mail_id");
    final String username = mapdata.get("mail_username");
    final String password = mapdata.get("mail_password");
    final String smtpHost = mapdata.get("mail_smtp_host");
    final String smtpPort = mapdata.get("mail_smtp_port");

    Properties props = new Properties();
    props.put("mail.smtp.auth", true);
    props.put("mail.smtp.starttls.enable", true);
    props.put("mail.smtp.host", smtpHost);
    props.put("mail.smtp.port", smtpPort);


    Session session = Session.getInstance(props,
            new javax.mail.Authenticator() {
                                protected PasswordAuthentication getPasswordAuthentication() {

                                  return new PasswordAuthentication(username, password);
                           }
                    });

       try {
              Message message = new MimeMessage(session);
              message.setFrom(new InternetAddress(emailId));
              message.setRecipients(Message.RecipientType.TO,InternetAddress.parse(toAddresses));
              message.setSubject(subject);

              String text =  "<b>Hello "+name+",</b><br/><br/> Thank you for registering with WARM for monitoring of your URL."
                    + "<br/>In order to activate your account please <a href="+url+"><b>Click Here</b></a><br/><br/>"
                            + "Once you have activated your WARM account you can login to the WARM Dashboard using your Username "
                            + "<a href=\"mailto:"+email+"\">"+email+"</a>.<br/>If you are having problems with the given link, "
                                    + "please reply back to the following Email address: <a href=\"mailto:[email protected]\">[email protected]</a>.</br>This is a system generated mail. Please do not reply to this mail.<br/><br/>Thanks and Regards<br/>WARM Admin.";

              message.setContent(text,"text/html");
              Transport.send(message);
       } catch (MessagingException e) {
              throw new RuntimeException(e);
       }
 }

public static void main(String args[]) {
    sendMail("[email protected]", "Hello", "h", "Rakesh", "[email protected]");
    System.out.println("Successfully send mail to - "+toAddresses); 
  }
}

When I am choosing Run as Java Application from Eclipse I am getting the following error log

       Exception in thread "main" java.lang.RuntimeException: javax.mail.AuthenticationFailedException: 534-5.7.9 Please log in with your web browser and then try again. Learn more at 534 5.7.9 https://support.google.com/mail/bin/answer.py?answer=78754 qk9sm15826619pac.16 - gsmtp
       at com.xxx.dashboard.mail.SendMail.sendMail(SendMail.java:80)
       at com.xxx.dashboard.mail.SendMail.main(SendMail.java:87)
       Caused by: javax.mail.AuthenticationFailedException: 534-5.7.9 Please log in with your web browser and then try again. Learn more at 534 5.7.9 https://support.google.com/mail/bin/answer.py?answer=78754 qk9sm15826619pac.16 - gsmtp
       at com.sun.mail.smtp.SMTPTransport$Authenticator.authenticate(SMTPTransport.java:826)
       at com.sun.mail.smtp.SMTPTransport.authenticate(SMTPTransport.java:761)
       at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:685)
       at javax.mail.Service.connect(Service.java:317)
       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 com.edifixio.dashboard.mail.SendMail.sendMail(SendMail.java:74)
       ... 1 more

I have a properties file named config.properties where I have kept the username and password,and other credentials like port no.Please find below its code:

[email protected]
[email protected]
mail_password=testxxx
mail_smtp_host=smtp.gmail.com
mail_smtp_port=587
ldap_provider_ip=192.168.15.50
ldap_port=1666
ldap_base=ou=users,ou=warm,dc=xxx,dc=co,dc=in
ldap_dir_context=dc=edifixio,dc=co,dc=in
ldap_security_principal=cn=Directory Manager,dc=xxx,dc=co,dc=in
ldap_security_credential=testxxx

Upvotes: 0

Views: 6760

Answers (5)

Guru Cse
Guru Cse

Reputation: 3285

  1. Log in to the gmail account specified in your properties file
  2. Click on the circle on your top right corner -> manage your google account -> security -> Less secure app access . Turn it on
  3. Go to https://accounts.google.com/DisplayUnlockCaptcha.
  4. Click on continue and logout of the gmail.
  5. Test your service. Now it will establish a connection to send mails

Upvotes: 1

Altanai
Altanai

Reputation: 1393

I was also trying out the simple javax mail program using the google smtp server . On receiving Authentication failed messages ,I had to goto https://www.google.com/settings/security/lesssecureapps and turn on access for less secure apps enter image description here

Upvotes: 2

John Sapp
John Sapp

Reputation: 21

I had this issue as well, I had to go into my gmail account settings and under Security Enable "Access for less secure apps" to fix it.

Upvotes: 2

Nikhil Talreja
Nikhil Talreja

Reputation: 2774

Try with these properties:

mail.smtp.auth=true
mail.smtp.starttls.enable=true
mail.smtp.auth.mechanisms=login
mail.smtp.quitwait=false
mail.debug = false
[email protected]
mail.password=test
mail.smtp.host=smtp.gmail.com
mail.smtp.port=587
mail.smtp.protocol=smtps

Upvotes: 0

Sridhar
Sridhar

Reputation: 11806

Your code sent mail without any exceptions in eclipse on Debian OS. Are you running any firewall or anti-virus software that might be interfering with the connection attempt?

Upvotes: 0

Related Questions