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

元气小坏坏 提交于 2019-12-06 08:53:17
properties.setProperty("mail.smtp.user", "abc");
properties.setProperty("mail.smtp.password", "xyz");
properties.setProperty("mail.smtp.auth", "true"); 

please try using this

While creating session override a method PasswordAuthentication and in that give username and password.

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

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);

If it still doesn't work for you try this:

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

Try also

Transport.send(message, user, pass);

I have found this in Transport class documentation

Use the specified user name 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

Vladimir Vagaytsev

I faced the same issue recently. I tried to send an email over Gmail server with API key + password auth. Transport.send(message) didn't work for me, I debugged it and found out that it ignored session settings attached to the message.

This is now I did the email sending over SMTP Gmail server with auth and TLS. I used com.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.smtp.host", host);
    properties.setProperty("mail.smtp.port", String.valueOf(port));
    properties.setProperty("mail.smtp.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());
Rishi Singhal

This works for me:

final Properties properties = System.getProperties();

// Setting up mail server
properties.setProperty("mail.smtp.host", EMAIL_SMTP_HOST);
properties.put("mail.smtp.starttls.enable", "true");
properties.put("mail.smtp.auth", "true");

Session session = Session.getInstance(properties, new Authenticator() {
    @Override
    protected PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication(EMAIL_USERNAME,
                EMAIL_PASSWORD);
    }
});
session.setDebug(true);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!