阿里云企业邮箱
无需导入依赖,使用Java包
/**
* redisKey 用来进行频率校验的rediskey
* toAddress 发给谁
* title 标题
* content 内容
*/
public boolean sendMailSecurityCode(String redisKey, String toAddress, String title, String content, long interval, long expire) {
// 当前时间
long currentTimeMillis = System.currentTimeMillis();
// 发送频率校验
RedisCacheValidateUtil.validateTime(redisUtils, redisKey, currentTimeMillis, interval);
// 生成六位随机数
String code = NumberUtil.getRandomNumber(6);
try {
//创建Session对象,session对象表示整个邮件的环境信息
Session session = authenticationMail();
// Message的实例对象表示一封电子邮件
MimeMessage message = new MimeMessage(session);
// 设置发件人的地址,sender是代表邮箱用户的昵称,自定义即可,accessKeyId是用户登录账号(邮箱号)
message.setFrom(emailProperties.getSender() + "<" + emailProperties.getAccessKeyId() + ">");
// 设置主题
message.setSubject(title);
// 设置邮件的文本内容
content = String.format(content, code);
message.setContent(content, "text/html;charset=utf-8");
// 从session的环境中获取发送邮件的对象
Transport transport = session.getTransport();
// 连接邮件服务器 address为邮箱实际服务器地址 accessKeyId是用户登录账号(邮箱号) secret为密钥
transport.connect(emailProperties.getAddress(), 465, emailProperties.getAccessKeyId(), emailProperties.getAccessKeySecret());
// 设置收件人地址,并发送消息
transport.sendMessage(message, new Address[]{new InternetAddress(toAddress)});
transport.close();
} catch (Exception e) {
throw new SmsEmailException(SmsEmailResponseCodeEnum.SERVER_FAIL, e);
}
// 发送成功,放入缓存
RedisCacheValidateUtil.setCache(redisUtils, redisKey, code, currentTimeMillis, expire);
return true;
}
/**
* 验证认证信息
* @return
*/
private Session authenticationMail(){
// 定义Properties对象,设置环境信息
Properties props = new Properties();
// 设置邮件服务器的地址
// emailProperties.getAccessKeyId() 你需要连接的服务器,不同厂商不一样,比如
// smtp.qiye.aliyun.com smtp.qiye.163.com smtp.163.com
props.setProperty("mail.smtp.host", emailProperties.getAccessKeyId());//指定的smtp服务器 指定发送邮件的主机smtp.qq.com(QQ)|smtp.163.com(网易)
props.setProperty("mail.smtp.auth", "true");
props.setProperty("mail.transport.protocol", "smtp"); //设置发送邮件使用的协议
// 不同厂商的端口也不同,直接百度就行,阿里云当前时间使用465端口需要进行加密,使用25端口不需要加密
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
return Session.getInstance(props);
// 设置输出调试信息
//session.setDebug(true);
}
163 企业邮箱
public boolean sendMailSecurityCode(String redisKey, String toAddress, String title, String content, long interval, long expire) {
// 当前时间
long currentTimeMillis = System.currentTimeMillis();
// 发送频率校验
RedisCacheValidateUtil.validateTime(redisUtils, redisKey, currentTimeMillis, interval);
Session session = authenticationMail();
// 生成六位随机数
String code = NumberUtil.getRandomNumber(6);
try {
MimeMessage msg = getMimeMessage(session, title, String.format(content, code), toAddress);
// 3.发送
Transport.send(msg);
// 发送成功,放入缓存
RedisCacheValidateUtil.setCache(redisUtils, redisKey, code, currentTimeMillis, expire);
}catch (Exception e) {
throw new SmsEmailException(SmsEmailResponseCodeEnum.SERVER_FAIL, e);
}
return true;
}
private MimeMessage getMimeMessage(Session session, String title, String content, String toAddress) throws MessagingException {
// 2.创建邮件对象(Message抽象类的子类对象)
MimeMessage msg = new MimeMessage(session); // 传入session
msg.setSubject(title, "UTF-8");
// sender为昵称 accessKeyId是用户登录账号(邮箱号)
msg.setFrom(emailProperties.getSender() + "<" + emailProperties.getAccessKeyId() + ">");
msg.setRecipients(MimeMessage.RecipientType.TO, new InternetAddress[] {
new InternetAddress(toAddress),
});
// 3.邮件内容"复合"对象
Multipart multipart = new MimeMultipart();
// 正文
BodyPart textPart = new MimeBodyPart();
// 参数1:正文内容
// 参数2:内容类型;字符编码集
textPart.setContent(content, "text/html;charset=utf-8");
// 添加至邮件内容
multipart.addBodyPart(textPart); // 添加正文
// 设置邮件内容
msg.setContent(multipart);
return msg;
}
/**
* 验证认证信息
*/
private Session authenticationMail() {
Session session;
try {
// SMTP服务器的连接信息
Properties props = new Properties();
// SMTP主机号 address为邮箱实际服务器地址
props.put("mail.smtp.host", emailProperties.getAddress());
// 主机端口号
props.put("mail.smtp.port", "25");
// 是否需要认证
props.put("mail.smtp.auth", "true");
// 启用TLS加密
props.put("mail.smtp.starttls.enable", "true");
// 创建Session
// 参数1:SMTP服务器的连接信息
// 参数2:用户认证对象(Authenticator接口的匿名实现类)
session = Session.getInstance(props, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(emailProperties.getAccessKeyId(), emailProperties.getAccessKeySecret());
}
});
// 开启调试模式
session.setDebug(true);
} catch (Exception e) {
throw new SmsEmailException(SmsEmailResponseCodeEnum.SERVER_FAIL, e);
}
return session;
}
163 个人免费邮箱
@Override
public boolean sendMailSecurityCode(String redisKey, String toAddress, String title, String content, long interval, long expire) {
// 当前时间
long currentTimeMillis = System.currentTimeMillis();
// 发送频率校验
RedisCacheValidateUtil.validateTime(redisUtils, redisKey, currentTimeMillis, interval);
Session session = authenticationMail();
MimeMessage message = getMimeMessage(true, session, emailProperties.getAddress(), toAddress);
// 生成六位随机数
String code = NumberUtil.getRandomNumber(6);
message = getContent(message, title, String.format(content, code), new Date());
try {
Transport transport = session.getTransport();
// 这里是你的163邮箱账号和imap授权码
transport.connect(emailProperties.getAccessKeyId(), emailProperties.getAccessKeySecret());
transport.sendMessage(message, message.getAllRecipients());
transport.close();
// 发送成功,放入缓存
RedisCacheValidateUtil.setCache(redisUtils, redisKey, code, currentTimeMillis, expire);
}catch (Exception e) {
throw new SmsEmailException(SmsEmailResponseCodeEnum.SERVER_FAIL);
}
return true;
}
@Override
public boolean sendMail(List<EmailMessageDetail> details) {
Session session = authenticationMail();
details.forEach(item -> {
MimeMessage message = getMimeMessage(false, session, emailProperties.getAddress(), item.getToAddress());
message = getContent(message, item.getTitle(), item.getContent(), new Date());
try {
Transport transport = session.getTransport();
// 这里是你的163邮箱账号和imap授权码
transport.connect(emailProperties.getAccessKeyId(), emailProperties.getAccessKeySecret());
transport.sendMessage(message, message.getAllRecipients());
transport.close();
}catch (Exception e) {
throw new SmsEmailException(SmsEmailResponseCodeEnum.SERVER_FAIL);
}
});
return true;
}
/**
* 验证认证信息
*/
private Session authenticationMail() {
Session session;
try {
Properties props = new Properties();
//设置用户的认证方式
props.setProperty("mail.smtp.auth", "true");
//设置传输协议
props.setProperty("mail.transport.protocol", "smtp");
//设置发件人的SMTP服务器地址
props.setProperty("mail.smtp.host", "smtp.163.com");
session = Session.getInstance(props);
session.setDebug(true);
} catch (Exception e) {
throw new SmsEmailException(SmsEmailResponseCodeEnum.SERVER_FAIL);
}
return session;
}
/**
* @param isSingle 是否单发
* <P>true-向指定的一个收件人发送邮件,比如:找回密码、登录验证
* <P>false-向多个收件人群发邮件,比如:优惠活动推送
* <P>群发时多个收件人之间用英文逗号','分割
* @param senderAddress 发件人地址
* @param recipientAddress 收件人地址
*/
private MimeMessage getMimeMessage(Boolean isSingle, Session session, String senderAddress, String recipientAddress) {
MimeMessage message = new MimeMessage(session);
try {
message.setFrom(emailProperties.getSender() + "<" + senderAddress + ">");
/*
设置收件人地址
MimeMessage.RecipientType.TO:发送 MimeMessage.RecipientType.CC:抄送 MimeMessage.RecipientType.BCC:密送
*/
if (isSingle) {
message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(recipientAddress));
} else {
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipientAddress));
}
} catch (Exception e){
throw new SmsEmailException(SmsEmailResponseCodeEnum.SEND_FAIL);
}
return message;
}
/**
* 邮件内容格式
*/
private MimeMessage getContent(MimeMessage message, String title, String Content, Date sendTime){
sendTime = sendTime == null ? new Date() : sendTime;
try{
message.setSubject(title, "UTF-8");
message.setContent(Content, "text/html;charset=UTF-8");
message.setSentDate(sendTime);
}catch (Exception e){
throw new SmsEmailException(SmsEmailResponseCodeEnum.SEND_FAIL);
}
return message;
}
标签:String,MimeMessage,阿里,session,props,new,企业邮箱,message,163
From: https://www.cnblogs.com/xy20211005/p/17480900.html