java 不支持或无法识别的SSL消息

pbpqsu0x  于 2023-03-16  发布在  Java
关注(0)|答案(2)|浏览(317)

我正在尝试在Java+ springboot中从outlook电子邮件发送电子邮件。
配置如下:

@Service
public class EmailService {

    
    String  from_address = "no-reply@jpmorganchase.com",
            password = "somepassword!sd#",
            host = "smtp.office365.com",
            port = "587",
            to_address = "no-reply@jpmorganchase.com",
            mail_subject = "Test",
            mail_content = "testing mail",
             username="",
            user_password="";
    
    
     public void sendmail(UserBean user,String uname, String pword)
        {
            username=uname;
            user_password=pword;
            to_address= user.getEmail();
            Properties props = new Properties();
            props.put("mail.smtp.user", from_address);
            props.put("mail.smtp.host", host);
            props.put("mail.smtp.port", port);
            props.put("mail.smtp.starttls.enable","true");
            props.put("mail.smtp.auth", "true");
            props.put("mail.smtp.debug", "true");
            props.put("mail.smtp.socketFactory.port", port);
            props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
            props.put("mail.smtp.socketFactory.fallback", "false");
            mail_content = 
                     "Hello "+user.getName()+"\n"
                            +"Experience "+user.getExperience()+"\n"
                            + "Email "+user.getEmail()+"\n"
                            + "Set allocated: "+user.getSet()+"\n"
                            +"Mobile number "+user.getPhone()+"\n"+"\n"
                            + "Your login credentials are: "+"\n"
                            +"User Name: "+username+"\n"
                            +"Password:  " + user_password+"\n"+"\n"
                            + "Click on link to take test   "+"https://www.jpmorganchase.com/en_sg"+"\n" 
                            +  "Message from jpmorganchase"+user.getMessage()+"\n";
                    
            mail_subject = "jpmorganchase Online Assesment Test";
            SecurityManager security = System.getSecurityManager();

            try
            {
                Authenticator auth = new SMTPAuthenticator();
                Session session = Session.getInstance(props, auth);
                session.setDebug(true);
                MimeMessage msg = new MimeMessage(session);
                msg.setText(mail_content);
                msg.setSubject(mail_subject);
                msg.setFrom(new InternetAddress(from_address));
                msg.addRecipient(Message.RecipientType.TO, new InternetAddress(to_address));
                Transport.send(msg);
            }
            catch (Exception mex)
            {
                mex.printStackTrace();
            } 
        }
     
     public void sendConfirmationMail(User user, String confirmationToken)
        {
            Properties props = new Properties();
            props.put("mail.smtp.user", from_address);
            props.put("mail.smtp.host", host);
            props.put("mail.smtp.port", port);
            props.put("mail.smtp.starttls.enable","true");
            props.put("mail.smtp.ssl.enable","true");
            props.put("mail.smtp.auth", "true");
            props.put("mail.smtp.debug", "true");
            props.put("mail.smtp.socketFactory.port", port);
            props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
            props.put("mail.smtp.socketFactory.fallback", "false");
            mail_content = 
                     "Hello "+user.getName()+"\n"
                     +"To confirm your account, please click here : "
                        +"http://localhost:8080/jpmorganchase/confirm-account?token="+confirmationToken;
            mail_subject="Complete Registration!";
           // String mail[] = user.getEmail().split("@");
            //to_address = mail[0]+"@gmail.com";
            to_address = user.getEmail();
                            
            SecurityManager security = System.getSecurityManager();

            try
            {
                Authenticator auth = new SMTPAuthenticator();
                Session session = Session.getInstance(props, auth);
                session.setDebug(true);
                MimeMessage msg = new MimeMessage(session);
                msg.setText(mail_content);
                msg.setSubject(mail_subject);
                msg.setFrom(new InternetAddress(from_address));
                msg.addRecipient(Message.RecipientType.TO, new InternetAddress(to_address));
                Transport.send(msg);
            }
            catch (Exception mex)
            {
                mex.printStackTrace();
            } 
        }

}

我收到以下异常:

DEBUG: setDebug: JavaMail version 1.5.6
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]
DEBUG SMTP: need username and password for authentication
DEBUG SMTP: protocolConnect returning false, host=smtp.office365.com, user=no-reply@jpmorganchase.com,
password=<null>
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "smtp.office365.com", port 587, isSSL false
javax.mail.MessagingException: Could not connect to SMTP host: smtp.office365.com, port: 587;
  nested exception is:
  javax.net.ssl.SSLException: Unsupported or unrecognized SSL message
  at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2120)
  at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:712)

已经尝试了各种解决方案,在互联网上,但没有运气。

agxfikkp

agxfikkp1#

我花了两个星期的时间来解决这个问题,并四处寻找。最后,我设法把它放在一起,使它工作。我找到了解决方案here的**关键部分。

简而言之,您需要创建SMTPTransport,而不是传统的Transport
SMTPTransport t = (SMTPTransport)session.getTransport("smtp");
Socket socket = new Socket(host, port);
t.connect(socket);
t.sendMessage(message, message.getAllRecipients());
注:

不需要超过以下属性:

properties.setProperty("mail.smtp.host", host);
properties.put("mail.smtp.port", port);
properties.put("mail.smtp.starttls.enable", "true");
properties.put("mail.smtp.ssl.enable", "true");

如果您使用self-signed证书,则这是必需的

properties.put("mail.smtp.ssl.trust", host);
如果计划使用smtps,则在创建Session后需要添加以下内容:
Session session = Session.getInstance(properties, null);
...
session.setProtocolForAddress("rfc822", "smtps");

并确保在创建SMTPTransport时获得smtps,如下所示:

SMTPTransport t = (SMTPTransport)session.getTransport("smtps");

并且需要为smtps协议定义所有属性:
使用smtps时,属性设置为mail.smtps.<prop>
通常,基于任何协议:mail.<protocol>.<prop>

properties.setProperty("mail.smtps.host", host);
properties.put("mail.smtps.port", port);
properties.put("mail.smtps.starttls.enable", "true");
properties.put("mail.smtps.ssl.enable", "true");
properties.put("mail.smtps.ssl.trust", host);
2vuwiymt

2vuwiymt2#

host=smtp.office365.com
port=587

我评论说
属性。put(“邮件。smtp。ssl。使能”,“真”);
当使用smtp.office365.com=〉时,它将工作

相关问题