发送时出现异常,javax.mail.MessagingException: 530 5.7.57 SMTP; 客户端未经身份验证无法在MAIL FROM期间发送匿名邮件。

3
这仅适用于(smtp.office365.com)SMTP。
public int sendEmail(String fromName,String fromAddress,ArrayList toAddressList ,ArrayList ccAddressList,ArrayList bccAddressList,String subject,String message,String SmtpServerIP,String smtpUserName,String smtpPassword, ArrayList<String>  attachmentFilePath ){
        SMTP_HOST_NAME=SmtpServerIP;
        SMTP_AUTH_USER=smtpUserName;
        SMTP_AUTH_PWD=smtpPassword;
        String emailmultipart="true";
        String smtpHost=SmtpServerIP;
        //System.out.println("SmtpServerIP"+SmtpServerIP);

        System.out.println("fromName :"+fromName+":SmtpServerIP:"+SmtpServerIP+":smtpPassword:"+smtpPassword+":smtpUserName:"+smtpUserName);
        boolean debug = false;
        Properties props = new Properties();
        props.put("mail.smtp.starttls.enable","true"); 
        props.setProperty("mail.transport.protocol", "smtp");
        if(smtpHost==null){
          return -99; 
        }
        if(smtpHost.length()>0){
          //props.put("mail.smtp.host", smtpHost);
          props.setProperty("mail.host", smtpHost);
         }
        else
        { 
          return 1; 
        }//Error No SmtpHost name Found.
       /* if(smtpUserName!=null && smtpUserName.length()>0){
            props.setProperty("mail.user", smtpUserName);
        }
        if(smtpPassword!=null && smtpPassword.length()>0){
            props.setProperty("mail.password", smtpPassword);
        }*/
        Session session=null;

        if(smtpUserName!=null && smtpUserName.length()>0&&smtpPassword!=null && smtpPassword.length()>0){
            System.out.println("smtpUserName111111111111111"+smtpUserName);
            props.put("mail.smtp.auth","true");
            Authenticator auth = new SMTPAuthenticator();
            session = Session.getDefaultInstance(props, auth);
        }else{
            System.out.println("smtpUserName2222222222222222"+smtpUserName);
            session = Session.getDefaultInstance(props, null);  
        }
        session.setDebug(debug);
        try{
            //System.out.println("toAddressList.size()"+toAddressList.size());
            Message msg = new MimeMessage(session);
            InternetAddress from = new InternetAddress(fromAddress,fromName);
            String msgSubject=subject;
            msg.setFrom(from);
            msg.setSubject(msgSubject);
            msg.setContent(message, "text/html; charset=utf-8");

            //msg.setSentDate(new Date(2005,9,12));

            //for adding TO address list.
            if(toAddressList.size()>0){
              Address toAddresses[]= new InternetAddress[toAddressList.size()];      
              toAddresses=getAddresses(toAddressList);
              //System.out.println(" toAddresses |||"+toAddresses);
              msg.setRecipients(Message.RecipientType.TO,toAddresses);

             }
             else{
                 return 2;
             }
            //for adding CC address list.
            if(ccAddressList.size()>0){
              Address ccAddresses[]= new InternetAddress[ccAddressList.size()];      
              ccAddresses=getAddresses(ccAddressList);
              msg.setRecipients(Message.RecipientType.CC,ccAddresses);
            }
            //for adding BCC address list.
            if(bccAddressList.size()>0){
              Address bccAddresses[]= new InternetAddress[bccAddressList.size()];      
              bccAddresses=getAddresses(bccAddressList);
              msg.setRecipients(Message.RecipientType.BCC,bccAddresses);
            }
            if(attachmentFilePath.size()>0){
                //System.out.println("Inside File Attachment attachmentFilePath.length()"+attachmentFilePath.length());
                //File file=new File(attachmentFilePath); 
                //if(file.exists()){
                    msg=attachFileAndMessage(msg,attachmentFilePath,message);
                //}else{
                  //  System.out.println("File does Not exists.");  
                  //  return 3;
                //}
            }
           // Transport.send(msg);
            msg.saveChanges(); // implicit with send()
            Transport transport = session.getTransport("smtp");
            //System.out.println("smtpHost :: "+smtpHost+"     smtpUserName ::"+smtpUserName+"       smtpPassword ::"+smtpPassword);
            transport.connect();
            transport.sendMessage(msg, msg.getAllRecipients());
            transport.close();

        }
       catch(MessagingException mex){
            mex.printStackTrace();
          String errmsg=mex.getMessage().toString();
          if(errmsg.indexOf("Unknown SMTP host")>-1){
            System.out.println("Invalid SMTP server entry");
            return 101;
          }else if(errmsg.indexOf("Invalid Addresses")>-1){
            System.out.println("Invalid Address entry");
            return 102;
          }              
          System.out.println("Error 1 ::"+mex.getMessage());
          mex.printStackTrace();
          return 4;
        }
        catch(Exception e){
          System.out.println("Error 2 ::"+e.getMessage());
          e.printStackTrace();
          return 5;
        }
        return 0;
    }

1
问题在哪里,问的是什么? - Buhake Sindi
发送电子邮件时出现以下异常:javax.mail.MessagingException: 530 5.7.57 SMTP; 客户端未经身份验证,无法在MAIL FROM期间发送匿名邮件。 - Raju
仍然没有意义。请更新您的问题并提供完整的异常堆栈跟踪,告诉我们发生了什么。 - Buhake Sindi
3个回答

5
我认为你在这里有问题。您的身份验证为空。
Authenticator auth = new SMTPAuthenticator();

尝试一下。
 Authenticator auth = new Authenticator()
    {
        @Override
        protected PasswordAuthentication getPasswordAuthentication()
        {
            return new PasswordAuthentication(SMTP_AUTH_USER, SMTP_AUTH_PWD);
        }
    };

这段代码在我的环境下可以运行。使用的是javax.mail v1.5。
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;
public class Main {
    public static void main(String[] args) {
        final String smtpAuthUserName = "YOUR_ADDRES@YOURCOMPANY.COM";
        final String smtpAuthPassword = "YOUR_PASSWORD";
        String emailFrom = "EMAIL_FROM";
        String emailTo   = "EMAIL_TO";
        Authenticator authenticator = new Authenticator()
        {
            @Override
            protected PasswordAuthentication getPasswordAuthentication()
            {
                return new PasswordAuthentication(smtpAuthUserName, smtpAuthPassword);
            }
        };
        Properties properties = new Properties();
        properties.setProperty("mail.smtp.host", "smtp.office365.com");
        properties.setProperty("mail.smtp.port", "587");
        properties.setProperty("mail.smtp.auth", "true");
        properties.setProperty("mail.smtp.starttls.enable", "true");
        Session session = Session.getInstance( properties, authenticator );
        try
        {
            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress(emailFrom));
            InternetAddress[] to = {new InternetAddress(emailTo)};
            message.setRecipients(Message.RecipientType.TO, to);
            message.setSubject("PLACE_SUBJECT_HERE");
            message.setText("YOUR_MESSAGE_HERE");
            Transport.send(message);
        }
        catch (MessagingException exception)
        {
            exception.printStackTrace();
        }
    }
}

1
你最好解释一下为什么这段代码对你有效,因为提问者想知道他的问题出在哪里以及为什么现在得到了解决。 - Muhammed Refaat

0
如果即使添加了上述代码错误仍然存在,请参考项目中的mail.jar。可能是mail.jar对该项目进行了破坏。使用最新版本的mail.jar并检查它。

我遇到了相同的错误,在添加更新的mail.jar文件之后,一切都运行得很顺利。 - Ritzz081
欢迎来到SO!请注意,这不是一个讨论论坛。答案没有固定的顺序,因此没有“上面”这个概念。如果您引用了一个问题或答案,可以使用链接指向它。此外,这应该是对用户SomethingNew的答案的评论。一旦您获得了更多的声望,您将能够评论其他人的帖子。在那之前,请专注于提供高质量的答案或问题。请阅读并理解帮助页面,以了解本网站的工作原理。 - cfi
好的。从现在开始我会只翻译文本内容。但是我之前只看到了一个答案,所以才这样评论。 - Ritzz081

0

我在使用旧版本的javax.mail(1.3.1)时遇到了这个错误。当我切换到较新版本(1.4.4)后,问题就解决了。

如果您正在使用Eclipse,则可能需要查看构建路径中您的项目正在使用哪些库,不仅是您指定的外部库,还包括内置库,如JavaEE5等,其中可能包含此库的旧版本。

您可以通过在代码中调试此语句来检查所使用的库的确切版本:Session session = Session.getInstance(...

它不会向您显示源代码,但会给出邮件.jar的目录路径,您可以打开它以查看版本(在MANIFEST文件中)。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接