前言
绑定阿里云域名,创建域名账户,并配置解析域名账户,获得发送邮箱的权限,上限两百封,超出要¥…
阿里云邮件推送控制台
https://www.aliyun.com/product/directmail
申请邮箱并解析域名
https://yq.aliyun.com/articles/707997
1. 邮箱信息实体类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
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 receiveMailAccount;
}
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包依赖
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version> 4.11 </version>
<scope>test</scope>
</dependency>
<!-- mail dependency -->
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version> 1.6 . 2 </version>
</dependency>
|
3. 发送邮箱实现
注意:
1. 更改下面的属性,与实体类对应
代码实现
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
package cn.bitqian.mail.test;
import org.junit.Test;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
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;
}
}
|
总结
到此这篇关于Java实现邮箱发送功能(阿里云邮箱推送)的文章就介绍到这了,更多相关Java邮箱发送(阿里云邮箱推送)内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://blog.csdn.net/qq_44783283/article/details/108669051