java - com.sun.mail.smtp.SMTPAddressFailedException : Recipient address rejected: Authentication Required

标签 java

我正在使用以下代码但收到错误消息 - 运行:

javax.mail.SendFailedException: Invalid Addresses;
  nested exception is:
        com.sun.mail.smtp.SMTPAddressFailedException: 530 5.7.0 : Recipient address rejected: Authentication Required

        at com.sun.mail.smtp.SMTPTransport.rcptTo(SMTPTransport.java:1607)
        at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:887)
        at javax.mail.Transport.send0(Transport.java:191)
        at javax.mail.Transport.send(Transport.java:120)
        at MailClient.sendMail(MailClient.java:55)
        at MailClient.main(MailClient.java:94)
Caused by: com.sun.mail.smtp.SMTPAddressFailedException: 530 5.7.0 : Recipient address rejected: Authentication Required

        at com.sun.mail.smtp.SMTPTransport.rcptTo(SMTPTransport.java:1505)
        ... 5 more

/**
 *
 * @author sachin
 */
import javax.mail.*;
 import javax.mail.internet.*;
 import javax.activation.*;
 import java.io.*;
 import java.util.Properties;

 public class MailClient
 {


     public void sendMail(String mailServer, String from, String to,
                             String subject, String messageBody
                             ) throws MessagingException, AddressException
     {
         // Setup mail server
         Properties props = System.getProperties();
         props.put("mail.smtp.host", mailServer);

         // Get a mail session
         Session session = Session.getDefaultInstance(props, null);

         // Define a new mail message
         Message message = new MimeMessage(session);
         message.setFrom(new InternetAddress(from));
         message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
         message.setSubject(subject);

         // Create a message part to represent the body text
         BodyPart messageBodyPart = new MimeBodyPart();
         messageBodyPart.setText(messageBody);

         //use a MimeMultipart as we need to handle the file attachments
         Multipart multipart = new MimeMultipart();

         //add the message body to the mime message
         multipart.addBodyPart(messageBodyPart);

         // add any file attachments to the message
         // addAtachments(attachments, multipart);

         // Put all message parts in the message
         message.setContent(multipart);

         // Send the message
         Transport.send(message);


     }

     protected void addAtachments(String[] attachments, Multipart multipart)
                     throws MessagingException, AddressException
     {
         for(int i = 0; i<= attachments.length -1; i++)
         {
             String filename = attachments[i];
             MimeBodyPart attachmentBodyPart = new MimeBodyPart();

             //use a JAF FileDataSource as it does MIME type detection
             DataSource source = new FileDataSource(filename);
             attachmentBodyPart.setDataHandler(new DataHandler(source));

             //assume that the filename you want to send is the same as the
             //actual file name - could alter this to remove the file path
             attachmentBodyPart.setFileName(filename);

             //add the attachment
             multipart.addBodyPart(attachmentBodyPart);
         }
     }

     public static void main(String[] args)
     {
         try
         {
             MailClient client = new MailClient();
             String server="smtp.bsgroup.in";
             String from="sachinsingh@bsgroup.in";
             String to = "sachinsingh@bsgroup.in";
             String subject="Test Mail";
             String message="Testing Mail";
          //  String[] filenames =
//{"c:\somefile.txt"};

             client.sendMail(server,from,to,subject,message);
         }
         catch(Exception e)
         {
             e.printStackTrace(System.out);
         }

     }
 }

最佳答案

您收到的错误消息告诉您,您连接到的 SMTP 服务器需要身份验证才能使用它发送电子邮件,而您还没有提供任何身份验证详细信息。

参见 here (Internet Archive)有关在 SMTP 服务器需要身份验证时如何发送电子邮件的示例。

关于java - com.sun.mail.smtp.SMTPAddressFailedException : Recipient address rejected: Authentication Required,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3254804/

相关文章:

Java Web 应用程序连接到 MYSQL 和 SQL SERVER

java - Java 中 Stack 的 EmptyStackException 错误

java - Abstract Dao 未使用 Hibernate 更新或保存

java - 如何使用 Redis 和 Flask 连接 Storm 和 D3.js?

java - ' sys.objects ' 附近的语法不正确

java - Gradle 构建因从服务器 : Proxy Authentication Required, 接收到状态代码 407 而失败,但具有类似 deps 的另一个项目成功

java - 输出流持久化问题

java - 实现搜索栏并在空对象引用错误上获取 'java.lang.Object[] java.util.Collection.toArray()'

java - Android videoview - 身临其境 - 重叠 Controller

java - 显示对话框而不卡住 UI