Com.sun.mail.smtp.SMTPSendFailedException: 530-5.5.1 Authentication Required

I am trying to send email from my Java application to any specific email address. I am using Java Mail API, but unfortunately I am getting SMTPSendFailedException error. Can any body tell me where I am making a mistake. Here is my code

import java.util.*;

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

//import SeconMail.Authenticator;

public class SendMail
{

   public static void main(String [] args)
   {    

      // Recipient email ID needs to be mentioned.
      String to = "to@gmail.com";

      // Sender email ID needs to be mentioned
      String from = "from@expertflow.com";

      // Assuming you are sending email from localhost
      String host = "smtp.gmail.com";

      // Get system properties
      Properties properties = System.getProperties();

      // Setup mail server

      properties.setProperty("mail.smtp.host", host);


      properties.put("mail.smtp.starttls.enable", "true");

      properties.put("mail.smtp.auth", "false");
      // Get the default Session object.
      Session session = Session.getDefaultInstance(properties);
      session.setDebug(true);

      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("This is the Subject Line!");

         // Now set the actual message
         message.setText("This is actual message");

         // Send message
         Transport.send(message);
         System.out.println("Sent message successfully....");
      }catch (MessagingException mex) {
         mex.printStackTrace();
      }
   }
}

      

+2


source to share


6 answers


properties.setProperty("mail.smtp.user", "abc");
properties.setProperty("mail.smtp.password", "xyz");
properties.setProperty("mail.smtp.auth", "true"); 

      



try using this

+6


source


When creating a session, override the method PasswordAuthentication

and provide a username and password in it.



    Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator() 
    {
        protected PasswordAuthentication getPasswordAuthentication() 
        {
            return new PasswordAuthentication("pqr@gmail.com","xyz@123");
        }
   });

      

+2


source


Try these

String login = "myemailhere";
String pass = "mypasshere";
properties.setProperty("mail.smtp.user", login);
properties.setProperty("mail.smtp.password", pass);
properties.setProperty("mail.smtp.auth", "true");      

Transport transport = session.getTransport("smtp");
transport.connect(null, login, pass);

      

+2


source


If you still don't work, try this:

properties.setProperty("mail.smtps.ssl.enable", "true");
properties.setProperty("mail.smtps.auth", "true"); 

      

+1


source


Try also

Transport.send(message, user, pass);

      

I found this in the documentation for the transport class

Use the specified username and password to authenticate to the mail server.

public static void send(Message msg, String user, String password) throws MessagingException {

    msg.saveChanges();
    send0(msg, msg.getAllRecipients(), user, password);
}

      

I am using JavaMail v 1.6.1 and MailTrap SMTP Server

0


source


I recently faced the same problem. I tried to send email via Gmail server with API key + auth password. Transport.send(message)

didn't work for me, I debugged it and found out that it was ignoring the session settings tied to message

.

Now I have sent email to Gmail SMTP server using auth and TLS. I usedcom.sun.mail:javax.mail-1.6.1

    String host = "your-email-server-host"; // define your server host here
    int port = 587; // standard port for TLS connection

    // config session properties, here the SMTP protocol is used
    Properties properties = new Properties();
    properties.setProperty("mail.stmp.host", host);
    properties.setProperty("mail.stmp.port", String.valueOf(port));
    properties.setProperty("mail.stmp.auth", "true"); // enable auth
    properties.setProperty("mail.smtp.starttls.enable", "true"); // enable TLS

    // get session instace baed on the settings defined above
    Session session = Session.getInstance(properties);

    // `prepareMessage` implementation is omitted, construct your own message here
    MimeMessage mimeMessage = prepareMessage(email, session);

    // your credentials       
    String username = "your-username@gmail.com"; // or API key, I used API key
    String password = "your-password";

    // get the transport instance from the freshly created session
    // pass the valid protocol name, here the SMTP is used
    Transport transport = session.getTransport("stmp");

    // connect to the transport instance with your credentials
    transport.connect(host, port, username,password);

    // send the message
    transport.sendMessage(mimeMessage, mimeMessage.getAllRecipients());

      

0


source







All Articles