Flask接口签名sign原理与实例代码浅析
414
2022-11-21
Java实现邮箱发送功能实例(阿里云邮箱推送)
前言
绑定阿里云域名,创建域名账户,并配置解析域名账户,获得发送邮箱的权限,上限两百封,超出要¥…
阿里云邮件推送控制台
https://aliyun.com/product/directmail
申请邮箱并解析域名
https://yq.aliyun.com/articles/707997
1. 邮箱信息实体类
package cn.bitqian.mail.test;
/**
* 邮件内容实体类
* @author echo lovely
* @date 2020/9/18 15:47
*/
public class MailInfo {
// 发件人账户
private String sendEmailAccount;
// 发件人密码
private String sendEmailPassword;
// 收件人账户
private String receiveMailAccount;
// 发送人姓名
private String sendPersonName;
// 收件人姓名
private String receivePersonName;
// 邮件标题
private String mailTitle;
// 邮件正文
private String mailContent;
public MailInfo(String sendEmailAccount, String sendEmailPassword, String receiveMailAccount,
String sendPersonName, String receivePersonName,
String mailTitle, String mailContent) {
this.sendEmailAccount = sendEmailAccount;
this.sendEmailPassword = sendEmailPassword;
this.receiveMailAccount = receiveMailAccount;
this.sendPersonName = sendPersonName;
this.receivePersonName = receivePersonName;
this.mailTitle = mailTitle;
this.mailContent = mailContent;
}
public String getSendEmailAccount() {
return sendEmailAccount;
}
public void setSendEmailAccount(String sendEmailAccount) {
this.sendEmailAccount = sendEmailAccount;
}
public String getSendEmailPassword() {
return sendEmailPassword;
}
public void setSendEmailPassword(String sendEmailPassword) {
this.sendEmailPassword = sendEmailPassword;
}
public String getReceiveMailAccount() {
return reSWqZbVvqAYceiveMailAccount;
}
public void setReceiveMailAccount(String receiveMailAccount) {
this.receiveMailAccount = receiveMailAccount;
}
public String getMailTitle() {
return mailTitle;
}
public void setMailTitle(String mailTitle) {
this.mailTitle = mailTitle;
}
public String getMailContent() {
return mailContent;
}
public void setMailContent(String mailContent) {
this.mailContent = mailContent;
}
public String getSendPersonName() {
return sendPersonName;
}
public void setSendPersonName(String sendPersonName) {
this.sendPersonName = sendPersonName;
}
public String getReceivePersonName() {
return receivePersonName;
}
public void setReceivePersonName(String receivePersonName) {
this.receivePersonName = receivePersonName;
}
}
2. jar包依赖
3. 发送邮箱实现
注意:
1. 更改下面的属性,与实体类对应
代码实现
package cn.bitqian.mail.test;
import org.junit.Test;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InSWqZbVvqAYternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Date;
import java.util.Properties;
/**
* 邮件发送工具类
* @author echo lovely
* @date 2020/9/18 15:54
*/
public class MailSendUtils {
// 邮箱smtp协议 这里是使用阿里云的
public static final String myEmailSMTPHost = "smtpdm.aliyun.com";
@Test
public void test() {
MailInfo mailInfo = new MailInfo("控制台所绑定的邮箱账户名",
"绑定账户邮箱密码",
"收件人邮箱名", "发件人姓名",
"收件人姓名", "邮箱标题", "邮箱内容");
try {
sendEmail(mailInfo);
} catch (Exception e) {
e.printStackTrace();
}
}
public void sendEmail(MailInfo mailInfo) throws Exception {
// 1. 创建参数配置, 用于连接邮件服务器的参数配置
Properties props = new Properties(); // 参数配置
props.setProperty("mail.transport.protocol", "smtp"); // 使用的协议(JavaMail规范要求)
props.setProperty("mail.smtp.host", myEmailSMTPHost); // 发件人的邮箱的 SMTP 服务器地址
props.setProperty("mail.smtp.auth", "true"); // 需要请求认证
// 2. 根据配置创建会话对象, 用于和邮件服务器交互
Session session = Session.getInstance(props);
session.setDebug(true); // 设置为debug模式, 可以查看详细的发送 log
// 3. 创建一封邮件
MimeMessage message = new MailSendUtils().createMimeMessage(session, mailInfo);
// 4. 根据 Session 获取邮件传输对象
Transport transport = session.getTransport();
// 5. 使用 邮箱账号 和 密码 连接邮件服务器, 这里认证的邮箱必须与 message 中的发件人邮箱一致, 否则报错
transport.connect(mailInfo.getSendEmailAccount(), mailInfo.getSendEmailPassword());
// 6. 发送邮件, 发到所有的收件地址, message.getAllRecipients() 获取到的是在创建邮件对象时添加的所有收件人, 抄送人, 密送人
transport.sendMessage(message, message.getAllRecipients());
// 7. 关闭连接
transport.close();
}
public MimeMessage createMimeMessage(Session session, MailInfo mailInfo) throws Exception {
// 1. 创建一封邮件
MimeMessage message = new MimeMessage(session);
// 2. From: 发件人(昵称有广告嫌疑,避免被邮件服务器误认为是滥发广告以至返回失败,请修改昵称)
message.setFrom(new InternetAddress(mailInfo.getSendEmailAccount(), mailInfo.getSendPersonName(), "UTF-8"));
// 3. To: 收件人(可以增加多个收件人、抄送、密送)
message.setRecipient(MimeMessage.RecipientType.TO,
new InternetAddress(mailInfo.getReceiveMailAccount(), mailInfo.getReceivePersonName(), "UTF-8"));
// 4. Subject: 邮件主题(标题有广告嫌疑,避免被邮件服务器误认为是滥发广告以至返回失败,请修改标题)
message.setSubject(mailInfo.getMailTitle(), "UTF-8");
// 5. Content: 邮件正文(可以使用html标签)(内容有广告嫌疑,避免被邮件服务器误认为是滥发广告以至返回失败,请修改发送内容)
message.setContent(mailInfo.getMailContent(), "text/html;charset=UTF-8");
// 6. 设置发件时间
message.setSentDate(new Date());
// 7. 保存设置
message.saveChanges();
return message;
}
}
总结
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~