我正在尝试在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)
已经尝试了各种解决方案,在互联网上,但没有运气。
2条答案
按热度按时间agxfikkp1#
我花了两个星期的时间来解决这个问题,并四处寻找。最后,我设法把它放在一起,使它工作。我找到了解决方案here的**关键部分。
简而言之,您需要创建
SMTPTransport
,而不是传统的Transport
:注:
不需要超过以下属性:
如果您使用
self-signed
证书,则这是必需的如果计划使用
smtps
,则在创建Session
后需要添加以下内容:并确保在创建
SMTPTransport
时获得smtps
,如下所示:并且需要为
smtps
协议定义所有属性:使用
smtps
时,属性设置为mail.smtps.<prop>
通常,基于任何协议:
mail.<protocol>.<prop>
2vuwiymt2#
我评论说
属性。put(“邮件。smtp。ssl。使能”,“真”);
当使用smtp.office365.com=〉时,它将工作