簡體   English   中英

com.sun.mail.smtp.SMTPSendFailedException: 530-5.5.1 需要身份驗證

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

我正在嘗試從我的 Java 應用程序向任何特定的電子郵件地址發送電子郵件。 我正在使用 Java Mail API,但不幸的是我收到了 SMTPSendFailedException 錯誤。 任何機構都可以告訴我我在哪里做錯了。 這是我的代碼

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

請嘗試使用這個

在創建會話時覆蓋方法PasswordAuthentication並在其中提供用戶名和密碼。

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

試試這些

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

如果它仍然不適合你試試這個:

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

也試試

Transport.send(message, user, pass);

我在運輸類文檔中找到了這個

使用指定的用戶名和密碼對郵件服務器進行身份驗證。

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

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

我正在使用 JavaMail v 1.6.1 和MailTrap SMTP 服務器

我最近遇到了同樣的問題。 我嘗試使用 API 密鑰 + 密碼身份驗證通過 Gmail 服務器發送電子郵件。 Transport.send(message)對我不起作用,我調試了它並發現它忽略了附加到message會話設置。

現在我使用身份驗證和 TLS 通過 SMTP Gmail 服務器發送電子郵件。 我用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());

這對我有用:

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

它有效,解決了我的問題

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM