简体   繁体   English

com.sun.mail.smtp.SMTPSendFailedException: 530-5.5.1 需要身份验证

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

I am trying to send an Email from my Java Application to any particular email address.我正在尝试从我的 Java 应用程序向任何特定的电子邮件地址发送电子邮件。 I am using Java Mail API but Unfortunately i am getting SMTPSendFailedException error.我正在使用 Java Mail API,但不幸的是我收到了 SMTPSendFailedException 错误。 Can any body tell me where i am doing 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's email ID needs to be mentioned.
      String to = "to@gmail.com";

      // Sender's 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();
      }
   }
}
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.在创建会话时覆盖方法PasswordAuthentication并在其中提供用户名和密码。

    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我正在使用 JavaMail v 1.6.1 和MailTrap SMTP 服务器

I faced the same issue recently.我最近遇到了同样的问题。 I tried to send an email over Gmail server with API key + password auth.我尝试使用 API 密钥 + 密码身份验证通过 Gmail 服务器发送电子邮件。 Transport.send(message) didn't work for me, I debugged it and found out that it ignored session settings attached to the message . Transport.send(message)对我不起作用,我调试了它并发现它忽略了附加到message会话设置。

This is now I did the email sending over SMTP Gmail server with auth and TLS.现在我使用身份验证和 TLS 通过 SMTP Gmail 服务器发送电子邮件。 I used com.sun.mail:javax.mail-1.6.1我用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());

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);
properties.setProperty("mail.smtp.user", "abc");
properties.setProperty("mail.smtp.password", "xyz");
properties.setProperty("mail.smtp.auth", "true");

it worked, fix my problem它有效,解决了我的问题

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 com.sun.mail.smtp.SMTPSendFailedException:530-5.5.1需要身份验证(Java Mail) - com.sun.mail.smtp.SMTPSendFailedException: 530-5.5.1 Authentication Required (Java Mail) com.sun.mail.smtp.SMTPSendFailedException:530 5.7.1客户端未通过身份验证 - com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.1 Client was not authenticated com.sun.mail.smtp.SMTPSendFailedException Javamail - com.sun.mail.smtp.SMTPSendFailedException Javamail com.sun.mail.smtp.SMTPSendFailedException:530 5.7.0必须首先发出STARTTLS命令 - com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.0 Must issue a STARTTLS command first MailSendException:失败的消息:com.sun.mail.smtp.SMTPSendFailedException - MailSendException: Failed messages: com.sun.mail.smtp.SMTPSendFailedException 如何解决com.sun.mail.smtp.SMTPSendFailedException在Java中? - how to solve com.sun.mail.smtp.SMTPSendFailedException in java ? 获取问题com.sun.mail.smtp.SMTPSendFailedException:530 5.7.0必须首先发出STARTTLS命令。 dg12sm142339333pac.47 - gsmtp - Getting Issue com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.0 Must issue a STARTTLS command first. dg12sm142339333pac.47 - gsmtp Spring Boot 邮件发件人错误:com.sun.mail.smtp.SMTPSendFailedException - Error at Spring Boot mail sender: com.sun.mail.smtp.SMTPSendFailedException com.sun.mail.smtp.SMTPSendFailedException:452 4.4.5磁盘空间不足; 稍后再试 - com.sun.mail.smtp.SMTPSendFailedException: 452 4.4.5 Insufficient disk space; try again later 错误:com.sun.mail.smtp.SMTPSendFailedException:451 4.3.0 错误:队列文件写入错误 - Error: com.sun.mail.smtp.SMTPSendFailedException: 451 4.3.0 Error: queue file write error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM