邮件工具包
POM
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>1.5.6</version>
<scope>compile</scope>
</dependency>
MailUtils
import com.sun.mail.util.MailSSLSocketFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;
/**
* 邮件发送类
* @author yangchengguang
*/
@Component
public class MailUtils {
private static final Logger LOGGER = LoggerFactory.getLogger(MailUtils.class);
private static String USER_NAME;
@Value("${mail.name:}")
public void setUserName(String userName) {
USER_NAME = userName;
}
private static String PASSWORD;
@Value("${mail.password:}")
public void setPASSWORD(String passWord) {
PASSWORD = passWord;
}
private static String AUTHORIZATION_CODE;
@Value("${mail.code:}")
public void setAuthorizationCode(String authorizationCode) {
AUTHORIZATION_CODE = authorizationCode;
}
private static String MAIL_HOST;
@Value("${mail.host:}")
public void setMailHost(String mailHost) {
MAIL_HOST = mailHost;
}
private static String POP_PORT;
@Value("${mail.port:}")
public void setPopPort(String popPort) {
POP_PORT = popPort;
}
// public static void main(String[] args) {
// sendOutLookSMTPMail("wangshuai9776@morimatsu.cn", "看到请Call我", "这是一个测试邮件");
// sendQQMail("wangshuai9776@morimatsu.cn", "看到请Call我", "这是一个测试邮件");
// }
/**
* 发送腾讯邮件
* @param toUrl 接收邮件邮箱地址
* @param text 邮件内容
* @param title 邮件主题
*/
public static void sendQQMail(String toUrl, String text, String title) {
try {
Properties props = new Properties();
// 开启debug调试
props.setProperty("mail.debug", "true");
// 发送服务器需要身份验证
props.setProperty("mail.smtp.auth", "true");
// 设置邮件服务器主机名
props.setProperty("mail.host", MAIL_HOST);
// 发送邮件协议名称
props.setProperty("mail.transport.protocol", "smtp");
MailSSLSocketFactory sf = new MailSSLSocketFactory();
sf.setTrustAllHosts(true);
props.put("mail.smtp.ssl.enable", "true");
props.put("mail.smtp.ssl.socketFactory", sf);
Session session = Session.getInstance(props);
//邮件内容部分
Message msg = new MimeMessage(session);
msg.setSubject(title);
msg.setText(text);
//邮件发送者
msg.setFrom(new InternetAddress(USER_NAME));
//发送邮件
Transport transport = session.getTransport();
transport.connect(MAIL_HOST, USER_NAME, AUTHORIZATION_CODE);
transport.sendMessage(msg, new Address[]{new InternetAddress(toUrl)});
transport.close();
} catch (Exception e) {
LOGGER.error("qq邮件发送异常:{}",e);
}
}
/**
* 发送OutLook邮件
* @param toUrl 接收邮件邮箱地址
* @param text 邮件内容
* @param title 邮件主题
*/
public static void sendOutLookSMTPMail(String toUrl, String text, String title) {
// String MAIL_HOST = "SMC-EXSRV03.cmorimatsu.sh.cn";
// String POP_PORT = "587";
final Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.host", MAIL_HOST);
props.put("mail.store.protocol", "smtp");
props.put("mail.smtp.port", POP_PORT);
//开启SSL
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.socketFactory.port",POP_PORT);
props.put("mail.smtp.socketFactory.fallback","false");
try {
Session session = Session.getDefaultInstance(props, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(USER_NAME, PASSWORD);//账号密码
}
});
session.setDebug(true);
// 创建邮件消息
MimeMessage message = new MimeMessage(session);
// 设置发件人
InternetAddress form = new InternetAddress(USER_NAME);
message.setFrom(form);
// 设置收件人
InternetAddress toAddress = new InternetAddress(toUrl);
message.setRecipient(Message.RecipientType.TO, toAddress);
// 设置邮件标题
message.setSubject(title);
// 设置邮件的内容体
message.setContent(text, "text/html;charset=UTF-8");
// 发送邮件
Transport.send(message);
} catch (Exception e) {
LOGGER.error("OutLook邮件发送异常:{}",e);
}
}
}