当前位置: 首页 > 知识库问答 >
问题:

发送javax时发生异常。邮政MessaginException:530 5.7.57 SMTP;客户端未通过身份验证,无法在从发送邮件期间发送匿名邮件

段干祺
2023-03-14

这仅适用于(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;
    }

共有3个答案

倪炎彬
2023-03-14

我在使用旧版本的javax时遇到了这个错误。邮件(1.3.1)。当我切换到更高版本(1.4.4)时,问题消失了。

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

您可以通过在代码中调试以下语句来检查正在使用的库的确切版本:Session=Session。getInstance(…

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

童铭晨
2023-03-14

如果在添加上述代码后错误仍然存在。请参考邮件。这是项目的一部分。可能是邮件。该项目的jar已损坏。使用最新版本的mail进行更改。罐和检查

狄阳华
2023-03-14

我觉得你有问题。你的授权是空的。

Authenticator auth = new SMTPAuthenticator();

尝试

 Authenticator auth = new Authenticator()
    {
        @Override
        protected PasswordAuthentication getPasswordAuthentication()
        {
            return new PasswordAuthentication(SMTP_AUTH_USER, SMTP_AUTH_PWD);
        }
    };

这个代码对我有用。javax。邮件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();
        }
    }
}
 类似资料: