Search code examples
javaemailsmtpgmailjakarta-mail

Not able to send mails from gmail using java code


package abc;

import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;

public class SendMail {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String to="[email protected]";
        String from="[email protected]";
        final String username="from";
        final String password="password";
        Properties properties=new Properties();
        properties.put("mail.smtp.host", "smtp.gmail.com");
        properties.put("mail.smtp.auth", "true");
        properties.put("mail.smtp.starttls.enable", "true");
        properties.put("mail.smtp.port", "587");

        Session session=Session.getInstance(properties,
                new javax.mail.Authenticator(){
            protected PasswordAuthentication getPasswordAuthentication(){
                return new PasswordAuthentication(username, password);
            }
        }
                );

        try{
            Message message=new MimeMessage(session);
            message.setFrom(new InternetAddress(from));
            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
            message.setSubject("Test Mail");
            message.setText("Hey I wrote a java code to send mail. Thanks ");
            Transport.send(message);
            System.out.println("Sent Mail :)");
        }       
        catch(MessagingException e){
            e.printStackTrace();
        }

    }

}

I got the following errror whene i tried running the above code ::

javax.mail.AuthenticationFailedException: 534-5.7.14 <https://accounts.google.com/ContinueSignIn?sarp=1&scc=1&plt=AKgnsbvHc
534-5.7.14 gCjfbim5WYCDTQee6sZlZ2d31nncueOizOcz9NieexN5nSlGr0c49lSZ43qc4RQPQvpWLH
534-5.7.14 qCUQUecjIR7qxdYJ5R_WgLxkzD9u4Ds3EEG7ceSMyTZg0dpSGJb-zl5C82YDTdLOYTX5Pl
534-5.7.14 tmEfWrmktFCdAxUjtDiPNruDLqhPSIZ9dd187tQjBtOw2X8zx7MUcysN9BRawwDmbXT6mJ
534-5.7.14 cLn_sJS5UBuqommn0uJK7W1tPZzU> Please log in via your web browser and
534-5.7.14 then try again.
534-5.7.14  Learn more at
534 5.7.14  https://support.google.com/mail/answer/78754 qy7sm48619995pab.34 - gsmtp

    at com.sun.mail.smtp.SMTPTransport$Authenticator.authenticate(SMTPTransport.java:648)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:583)
    at javax.mail.Service.connect(Service.java:313)
    at javax.mail.Service.connect(Service.java:172)
    at javax.mail.Service.connect(Service.java:121)
    at javax.mail.Transport.send0(Transport.java:190)
    at javax.mail.Transport.send(Transport.java:120)
    at abc.SendMail.main(SendMail.java:36)

Gmail is not allowing this code. I even received a mail from gmail about suspicious logins and reviewed the devide from where i am running this code. What is the solution ?


Solution

  • It is AuthenticationFailedException, you should use the real username and password combination in order to get the program working and should give access to your program to sign-in to the gmail which explained in detail down below.

    And also you should check this out to give access your program to send mail, the whole answer in the link given in the stack trace;

    https://support.google.com/mail/answer/78754

    What you have to do is (as explained in the link above);

    1. Give access to your user;

    https://accounts.google.com/DisplayUnlockCaptcha

    enter image description here

    1. Give access to less-secure sign-in;

    https://www.google.com/settings/security/lesssecureapps

    enter image description here

    After then, If you try again to run your code with valid parameters, the test mail will be sent.