SendEmail.java
3.74 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package com.essa.framework;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
public class SendEmail {
public static void main(String[] args) {
// 创建一个Property文件对象
Properties props = new Properties();
// 设置邮件服务器的信息,这里设置smtp主机名称
props.put("mail.smtp.host", "smtp.qq.com");
// 设置socket factory 的端口
props.put("mail.smtp.socketFactory.port", "465");
// 设置socket factory
props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
// 设置需要身份验证
props.put("mail.smtp.auth", "true");
// 设置SMTP的端口,QQ的smtp端口是25
props.put("mail.smtp.port", "25");
// 身份验证实现
Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
// 第二个参数,就是我QQ开启smtp的授权码
return new PasswordAuthentication("278953238@qq.com", "pfpzqcamljytbjec");
}
});
try {
// 创建一个MimeMessage类的实例对象
Message message = new MimeMessage(session);
// 设置发件人邮箱地址
message.setFrom(new InternetAddress("278953238@qq.com"));
// 设置收件人邮箱地址
message.setRecipients(Message.RecipientType.TO,InternetAddress.parse("3289714863@qq.com"));
// 设置邮件主题
message.setSubject("测试发送邮件");
// 创建一个MimeBodyPart的对象,以便添加内容
BodyPart messageBodyPart1 = new MimeBodyPart();
// 设置邮件正文内容
messageBodyPart1.setText("这个是邮件的正文部分");
// 创建另外一个MimeBodyPart对象,以便添加其他内容
MimeBodyPart messageBodyPart2 = new MimeBodyPart();
// 设置邮件中附件文件的路径
String filename = ".\\test-output\\emailable-report.html";
// 创建一个datasource对象,并传递文件
DataSource source = new FileDataSource(filename);
// 设置handler
messageBodyPart2.setDataHandler(new DataHandler(source));
// 加载文件
messageBodyPart2.setFileName(filename);
// 创建一个MimeMultipart类的实例对象
Multipart multipart = new MimeMultipart();
// 添加正文1内容
multipart.addBodyPart(messageBodyPart1);
// 添加正文2内容
multipart.addBodyPart(messageBodyPart2);
// 设置内容
message.setContent(multipart);
// 最终发送邮件
Transport.send(message);
System.out.println("=====邮件已经发送=====");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
}