【问题标题】:com.sun.mail.smtp.SMTPAddressFailedException : Recipient address rejected: Authentication Requiredcom.sun.mail.smtp.SMTPAddressFailedException:收件人地址被拒绝:需要身份验证
【发布时间】:2010-07-15 10:45:57
【问题描述】:

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

javax.mail.SendFailedException:无效地址; 嵌套异常是: com.sun.mail.smtp.SMTPAddressFailedException:530 5.7.0:收件人地址被拒绝:需要身份验证 在 com.sun.mail.smtp.SMTPTransport.rcptTo(SMTPTransport.java:1607) 在 com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:887) 在 javax.mail.Transport.send0(Transport.java:191) 在 javax.mail.Transport.send(Transport.java:120) 在 MailClient.sendMail(MailClient.java:55) 在 MailClient.main(MailClient.java:94) 引起:com.sun.mail.smtp.SMTPAddressFailedException:530 5.7.0:收件人地址被拒绝:需要身份验证 在 com.sun.mail.smtp.SMTPTransport.rcptTo(SMTPTransport.java:1505) ... 5 更多
/**
 *
 * @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);
         }

     }
 }

【问题讨论】:

    标签: java


    【解决方案1】:

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

    请参阅here (Internet Archive),了解在 SMTP 服务器需要身份验证时如何发送电子邮件的示例。

    【讨论】:

      【解决方案2】:
      public class SendEmailServiceImpl extends Authenticator implements SendEmailService {
      
      @Autowired
      private TemplateEngine templateEngine;
      
      private final static String id = "sachinsingh@gmail.com";
      private final static String pw = "!@#$5678";
      
      private PasswordAuthentication pa;
      
      public SendEmailServiceImpl() {
          pa = new PasswordAuthentication(id, pw);
      }
      
      public PasswordAuthentication getPasswordAuthentication() {
          return pa;
      }
      
      @Override
      public void SendEmail(Person person, HttpServletRequest req, HttpServletResponse res) {
          String link = "";
      
          String contextPath = "http://" + req.getServerName() + ":" + req.getServerPort();
      
      
          WebContext ctx = new WebContext(req, res, req.getServletContext(),
                  req.getLocale());
      
      
          Properties p = System.getProperties();
          p.put("mail.smtp.starttls.enable", "true");
          p.put("mail.smtp.host", "smtp.gmail.com");
          p.put("mail.smtp.auth", "true");
          p.put("mail.smtp.port", "587");
      
          Authenticator auth = new SendEmailServiceImpl();
          Session session = Session.getDefaultInstance(p, auth);
          MimeMessage msg = new MimeMessage(session);
      
          try {
              msg.setSentDate(new Date());
              InternetAddress from = new InternetAddress();
              from = new InternetAddress(id);
      
              msg.setFrom(from);
              InternetAddress to = new InternetAddress(person.getEmail());
              msg.setRecipient(Message.RecipientType.TO, to);
      
              link = contextPath + req.getContextPath() + "/checkToken/" + person.getToken();
              ctx.setVariable("tokenLink", link);
              String template = templateEngine.process("email/email_join", ctx);
              msg.setSubject("subject", "UTF-8");
              msg.setText(template, "UTF-8");
      
              msg.setHeader("content-Type", "text/html");
      
              javax.mail.Transport.send(msg);
              System.out.println("send email");
      
          } catch (AddressException addr_e) {
              addr_e.printStackTrace();
          } catch (MessagingException msg_e) {
              msg_e.printStackTrace();
          }
      }
      

      【讨论】:

      • 也添加一些解释。
      【解决方案3】:
            public class SendEmailService {
                          private static final org.slf4j.Logger logger = LoggerFactory.getLogger(SendEmailService.class);
      
                public static void main(String args[]){
              sendingConfirmationMail.("sachinsingh@bsgroup.in","sachinsingh@bsgroup.in","password"
      ,"smtp.gmail.com","465");
      //incase of zoho host,change into smtp.zoho.com
      
              }
                          public static void sendingConfirmationMail(String fromEmail, String userName, String password,
                       String toEmail, String sender_host,
                                  String ssl_port) throws AddressException, MessagingException {
                              try {
                                  // protocol properties
                              Properties props = System.getProperties();
                              props.setProperty("mail.smtps.host", sender_host);
                              props.setProperty("mail.smtp.port", ssl_port);
                              props.setProperty("mail.smtp.startssl.enable", "true");
                              props.setProperty("mail.smtps.auth", "true");
                              // close connection upon quit being sent
                              props.put("mail.smtps.quitwait", "false");
                              // Properties props=MailProperties(sender_host, ssl_port);
                              Session session = Session.getInstance(props, null);
      
                                  final MimeMessage msg = new MimeMessage(session);
                                  // set recipients and content
                                  msg.setFrom(new InternetAddress(fromEmail));
                                  msg.addRecipients(Message.RecipientType.TO, InternetAddress.parse(toEmail, false));
      
                                  msg.setSubject("Test Mail");
      
                                  msg.setContent("Testing Mail", "text/html; charset=utf-8");
                                  msg.setSentDate(new Date());
      
                              Transport transport = session.getTransport("smtps");
                              transport.connect(sender_host, userName, password);
                              transport.sendMessage(msg, msg.getAllRecipients());
                              transport.close();
                              } catch (MessagingException e) {
      
                              }
                          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-01-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多