1
votes

I am trying to send email using gmail smtp using javax.mail. following is my code

 public static void send(String from,String password,String to,String sub,String msg){  
      //Get properties object    
      Properties props = new Properties();    
      props.put("mail.smtp.host", "smtp.gmail.com");    
      props.put("mail.smtp.socketFactory.port", "465");    
      props.put("mail.smtp.socketFactory.class",    
                "javax.net.ssl.SSLSocketFactory");    
      props.put("mail.smtp.auth", "true");    
      props.put("mail.smtp.port", "465");    
      //get Session   
      Session session = Session.getDefaultInstance(props,    
       new javax.mail.Authenticator() {    
       protected PasswordAuthentication getPasswordAuthentication() {    
       return new PasswordAuthentication(from,password);  
       }    
      });    
      //compose message    
      try {    
       MimeMessage message = new MimeMessage(session);    
       message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));    
       message.setSubject(sub);    
       message.setText(msg);    
       //send message  
       Transport.send(message);    
       System.out.println("message sent successfully");    
      } catch (MessagingException e) {throw new RuntimeException(e);}    

}  

Code is working fine When I am running it on my local server but when I am trying to run it on Elastic beanstalk (My Server is running on AWS EBS) then authentication fail exception is coming Note : I have turn ON access to less Secure app from Google A/c Setting but still I am getting this error

javax.mail.AuthenticationFailedException: 534-5.7.14 Please log in via your web browser and then try again. 534-5.7.14 Learn more at 534 5.7.14 https://support.google.com/mail/answer/78754 l13sm3053341iti.6 - gsmtp

2

2 Answers

1
votes

Same problem I faced recently, I found problem is of EC2 region. Google does not allow to login less Secure app from a non-frequent location of user. Either you use Google Mail APIs or use some other mail platforms like Yahoo. Check your EC2 instance region. try following code with yahoo mail, Deploy it on Elastic beanstalk or whatever environment you are using. This works for me.

public void yahooSend(String mail,String subject,String msg) {

        // Sender's email ID needs to be mentioned
         String from = "YOUR_YAHOO_MAIL";
         String pass ="YOUR_YAHOO_PASSWORD";
        // Recipient's email ID needs to be mentioned.
       String to = mail;
       String host = "smtp.mail.yahoo.com";

       // Get system properties
       Properties properties = System.getProperties();
       // Setup mail server
       properties.put("mail.smtp.starttls.enable", "true");
       properties.put("mail.smtp.host", host);
       properties.put("mail.smtp.user", from);
       properties.put("mail.smtp.password", pass);
      // props.put("mail.smtp.user", "YAHOO_USER_NAME"); 
       properties.put("mail.smtp.port", "587");
       properties.put("mail.smtp.auth", "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(subject);

          // Now set the actual message
          message.setText(msg);
          System.out.print("Sending msg "+msg);
          // Send message
          Transport transport = session.getTransport("smtp");
          transport.connect(host,587, from, pass);
          transport.sendMessage(message, message.getAllRecipients());
          transport.close();
         System. out.println("Sent message successfully....");
       }catch (MessagingException mex) {
           System. out.print(mex);
          mex.printStackTrace();
       }
 }
-1
votes

Please try with this

public static void sendPDFReportByGMail(String from, String pass, String to, String subject, String body) {
    Properties props = System.getProperties();
    String host = "smtp.gmail.com";
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.host", host);
    props.put("mail.smtp.user", from);
    props.put("mail.smtp.password", pass);
    props.put("mail.smtp.port", "587");
    props.put("mail.smtp.auth", "true");

    Session session = Session.getInstance(props, new javax.mail.Authenticator() {
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(from, pass);
        }
    });

    MimeMessage message = new MimeMessage(session);

    try {
        // Set from address
        message.setFrom(new InternetAddress(from));
        message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
        // Set subject
        message.setSubject(subject);
        // Set Mail body
        message.setText(body);

        BodyPart objMessageBodyPart = new MimeBodyPart();

        objMessageBodyPart.setText(body);

        Transport transport = session.getTransport("smtp");
        transport.connect(host, from, pass);
        transport.sendMessage(message, message.getAllRecipients());
        transport.close();
    } catch (AddressException ae) {
        ae.printStackTrace();
    } catch (MessagingException me) {
        me.printStackTrace();
    }
}