对应的文档
https://help.aliyun.com/product/29412.html?spm=a2c4g.57202.0.0.7d751922CP0cbJ
1、域名购买
https://wanwang.aliyun.com/?utm_content=se_1013639161
2、进行域名解析和二级域名创建
2.1 在控制台进行查询 邮件推送 点击进入
2.2 点击邮件设置→发信域名→新建域名 这里要创建一个二级域名,接着就是对应的域名解析点击同意,然后再发信域名点击配置和验证
2.3 点解左侧的发信地址,右上角有一个新建发信地址,发信域名选择刚刚创建的,账号自己想一个不要太长,回信地址可以不用写,发信类型按照自己的需求来选择。点击确认,接着就是 设置SMTP密码
3、代码的编写
一般阿里都会有对应的代码实例,我的就是根据代码实例改写过来的
3.1 添加依赖
<!-- SMTP 邮件发送协议 -->
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>1.6.2</version>
</dependency>
<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.6</version>
</dependency>
3.2、编写实例
这里 我们是一个给用户发送点赞预警提醒的
module
@Data
public class WarningMessages {
//用户邮箱
private String email;
//用户名
private String userName;
//原文链接
private String link;
//邮件主题(标题)
private String subject;
//预估浏览量
private Integer estimatedViews;
//预警值-低
private Integer viewCountLow;
//预警值-高
private Integer viewCountHigh;
//笔记标题
private String noteName;
//关键词
private String keyword;
}
实例 对应邮件
protected static String genMessageID(String mailFrom) {
// message-id 必须符合 first-part@last-part
String[] mailInfo = mailFrom.split("@");
String domain = mailFrom;
int index = mailInfo.length - 1;
if (index >= 0) {
domain = mailInfo[index];
}
UUID uuid = UUID.randomUUID();
StringBuffer messageId = new StringBuffer();
messageId.append('<').append(uuid.toString()).append('@').append(domain).append('>');
return messageId.toString();
}
public boolean sendEmail(WarningMessages data) throws GeneralSecurityException {
// 配置发送邮件的环境属性
final Properties props = new Properties();
// 表示SMTP发送邮件,需要进行身份验证
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.host", "smtpdm.aliyun.com");
//设置端口:
props.put("mail.smtp.port", "465");//"465"或"25", 如果使用ssl,则去掉使用80或25端口的配置,进行如下配置:
//加密方式:
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.port", "465");
//
//
MailSSLSocketFactory sf = new MailSSLSocketFactory();
sf.setTrustAllHosts(true);
props.put("mail.smtp.ssl.enable","true");
props.put("mail.smtp.from", "邮件地址"); //mailfrom 参数
props.put("mail.user", "邮件地址");// 发件人的账号(在控制台创建的发信地址)
props.put("mail.password", "SMTP密码");// 发信地址的smtp密码(在控制台选择发信地址进行设置)
props.setProperty("mail.smtp.ssl.enable", "true"); //请配合465端口使用
System.setProperty("mail.mime.splitlongparameters", "false");//用于解决附件名过长导致的显示异常
//props.put("mail.smtp.connectiontimeout", 1000);
// 构建授权信息,用于进行SMTP进行身份验证
Authenticator authenticator = new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
// 用户名、密码
String userName = props.getProperty("mail.user");
String password = props.getProperty("mail.password");
return new PasswordAuthentication(userName, password);
}
};
//使用环境属性和授权信息,创建邮件会话
Session mailSession = Session.getInstance(props, authenticator);
//mailSession.setDebug(true);//开启debug模式
final String messageIDValue = genMessageID(props.getProperty("mail.user"));
//创建邮件消息
MimeMessage message = new MimeMessage(mailSession) {
@Override
protected void updateMessageID() throws MessagingException {
//设置自定义Message-ID值
setHeader("Message-ID", messageIDValue);//创建Message-ID
}
};
try {
// 设置发件人邮件地址和名称。填写控制台配置的发信地址。和上面的mail.user保持一致。名称用户可以自定义填写。
InternetAddress from = new InternetAddress("邮件地址", "名称");//from 参数,可实现代发,注意:代发容易被收信方拒信或进入垃圾箱。
message.setFrom(from);
//可选。设置回信地址
// Address[] a = new Address[1];
// a[0] = new InternetAddress("回信地址");
// message.setReplyTo(a);
// 设置收件人邮件地址
InternetAddress to = new InternetAddress(data.getEmail());
message.setRecipient(MimeMessage.RecipientType.TO, to);
//如果同时发给多人,才将上面两行替换为如下(因为部分收信系统的一些限制,尽量每次投递给一个人;同时我们限制单次允许发送的人数,具体参考规格清单):
// InternetAddress[] adds = new InternetAddress[data.size()];
// for (int i =0; i<data.size();i++){
// WarningMessages warningMessages = data.get(i);
// InternetAddress internetAddress = new InternetAddress(warningMessages.getEmail());
// adds[i] = internetAddress;
// }
//
// message.setRecipients(Message.RecipientType.TO, adds);
message.setSentDate(new Date()); //设置时间
//设置邮件标题
message.setSubject(data.getSubject());
message.setContent("尊敬的" + data.getUserName() + ":<br> 您在舆情控制平台上,设定的“{" + data.getKeyword() + "}”关键词任务检测到:{" + data.getNoteName() + ":" + data.getLink() + "}于" + DateUtil.date() + "," +
"预估浏览量已经超过了您所设定的预警值。该笔记预估浏览量为{" + data.getEstimatedViews() + "}次,超过了您设定的预警值[" + data.getViewCountLow() + "-" + data.getViewCountHigh() + "]。\n" +
"如果您有任何疑问或需要进一步帮助,请随时与我们联系。", "text/html;charset=UTF-8");//html超文本;// "text/plain;charset=UTF-8" //纯文本。
Transport.send(message);
return true;
} catch (MessagingException e) {
String err = e.getMessage();
log.info("发送失败:"+err);
// 在这里处理message内容, 格式是固定的
return false;
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return false;
}
}
调用
WarningMessages warningMessages = new WarningMessages();
warningMessages.setEmail("测试目标地址");
warningMessages.setUserName("名称");
warningMessages.setSubject("标题");
warningMessages.setKeyword("关键词");
warningMessages.setLink("相关链接");
。。。。。。。。。。。。。
BuildEmailUtils buildEmailUtils = new BuildEmailUtils();
buildEmailUtils.sendEmail(warningMessages);