|
@ -1,5 +1,6 @@
|
|
|
package com.yihu.jw.base.util;
|
|
|
|
|
|
import com.sun.mail.util.MailSSLSocketFactory;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
@ -91,6 +92,58 @@ public class SendEmailUtils {
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
public static boolean sendMessage(String email,String content,String subject){
|
|
|
// 配置
|
|
|
Properties prop=new Properties();
|
|
|
// 设置邮件服务器主机名,这里是163
|
|
|
prop.put("mail.host",host );
|
|
|
// 发送邮件协议名称
|
|
|
prop.put("mail.transport.protocol",smtp);
|
|
|
// 是否认证
|
|
|
prop.put("mail.smtp.auth", auth);
|
|
|
|
|
|
try {
|
|
|
// SSL加密
|
|
|
MailSSLSocketFactory sf = null;
|
|
|
sf = new MailSSLSocketFactory();
|
|
|
// 设置信任所有的主机
|
|
|
sf.setTrustAllHosts(true);
|
|
|
prop.put("mail.smtp.ssl.enable", true);
|
|
|
prop.put("mail.smtp.ssl.socketFactory", sf);
|
|
|
|
|
|
// 创建会话对象
|
|
|
Session session = Session.getInstance(prop, new Authenticator() {
|
|
|
// 认证信息,需要提供"用户账号","授权码"
|
|
|
public PasswordAuthentication getPasswordAuthentication() {
|
|
|
return new PasswordAuthentication(username, password);
|
|
|
}
|
|
|
});
|
|
|
// 是否打印出debug信息
|
|
|
// session.setDebug(true);
|
|
|
|
|
|
// 创建邮件
|
|
|
Message message = new MimeMessage(session);
|
|
|
// 邮件发送者
|
|
|
message.setFrom(new InternetAddress(username));
|
|
|
// 邮件接受者
|
|
|
message.addRecipient(Message.RecipientType.TO, new InternetAddress(email));
|
|
|
// 邮件主题
|
|
|
message.setSubject(subject);
|
|
|
message.setContent(content, "text/html;charset=UTF-8");
|
|
|
// Transport.send(message);
|
|
|
// 邮件发送
|
|
|
Transport transport = session.getTransport();
|
|
|
transport.connect();
|
|
|
transport.sendMessage(message, message.getAllRecipients());
|
|
|
transport.close();
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
return false;
|
|
|
}
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@Value("${spring.mail.username}")
|
|
|
public void setUsername(String username) {
|
|
@ -129,4 +182,5 @@ public class SendEmailUtils {
|
|
|
public void setPassword(String password) {
|
|
|
SendEmailUtils.password = password;
|
|
|
}
|
|
|
|
|
|
}
|