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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
| import java.util.Date;
import java.util.Properties;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMessage.RecipientType;
import javax.mail.internet.MimeMultipart;
public class JavaMailUtils {
//连接SMTP服务器的主机名,这里以qq邮箱为例
private static final String SMTP_HOST = "smtp.qq.com";
//连接的端口号,587为ssl端口,默认为25端口
private static final String SMTP_PORT = "587";
//发件人
private static final String MAIL_FROM = "XXX@qq.com";
//连接邮件服务器的用户名(邮箱地址去除了@qq.com的部分)
private static final String USER = "XXX";
/**
* 授权码,就是你在邮件服务器上注册的密码,不是你的qq密码
* 在邮箱里开启smtp/imap服务时需要发送短信,成功后会得到一个授权码
*/
private static final String PASSWORD = "授权码";
private JavaMailUtils() {
}
/**
* send mail without attachments
*
* @param mailTo
* @param mailCc
* @param subject
* @param content
* @throws AddressException
* @throws MessagingException
*/
public static void sendMail(final String mailTo, final String mailCc, final String subject, final String content)
throws Exception {
// get the session
final Session session = getSession();
// create a mail
final MimeMessage message = createMail(session, mailTo, mailCc, subject, content);
// get the transport
final Transport transport = session.getTransport();
transport.connect(USER, PASSWORD);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
}
public static void sendMail(final String mailTo, final String mailCc, final String subject, final String content,
final String[] attachments) throws Exception {
// get the session
final Session session = getSession();
// create a mail
final MimeMessage message = createMail(session, mailTo, mailCc, subject, content, attachments);
// get the transport
final Transport transport = session.getTransport();
transport.connect(USER, PASSWORD);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
}
/**
* create a mail without attachments
*
* @param session
* @param mailTo
* @param mailCc
* @param subject
* @param content
* @return
* @throws Exception
*/
private static MimeMessage createMail(final Session session, final String mailTo, final String mailCc,
final String subject, final String content) throws Exception {
final MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(MAIL_FROM));
message.setRecipients(RecipientType.TO, InternetAddress.parse(mailTo));
message.setRecipients(RecipientType.CC, InternetAddress.parse(mailCc));
message.setSubject(subject);
message.setContent(content, "text/html;charset=UTF-8");
message.setSentDate(new Date());
message.saveChanges();
return message;
}
/**
* create a mail with attachments
*
* @param session
* @param mailTo
* @param mailCc
* @param subject
* @param content
* @param attachments
* @return
*/
private static MimeMessage createMail(final Session session, final String mailTo, final String mailCc,
final String subject, final String content, final String[] attachments) throws Exception {
final MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(MAIL_FROM));
message.setRecipients(RecipientType.TO, InternetAddress.parse(mailTo));
message.setRecipients(RecipientType.CC, InternetAddress.parse(mailCc));
message.setSubject(subject);
// set the multipart of the mail
final MimeMultipart multipart = new MimeMultipart();
// set content part of the mail
final MimeBodyPart contentPart = new MimeBodyPart();
contentPart.setContent(content, "text/html;charset=UTF-8");
multipart.addBodyPart(contentPart);
// set attachment part of the mail
final MimeBodyPart attach1 = new MimeBodyPart();
final MimeBodyPart attach2 = new MimeBodyPart();
if (attachments != null && attachments.length != 0) {
final int length = attachments.length;
if (length == 1) {
attach1.attachFile(attachments[0]);
multipart.addBodyPart(attach1);
}
if (length == 2) {
attach1.attachFile(attachments[0]);
attach2.attachFile(attachments[1]);
multipart.addBodyPart(attach1);
multipart.addBodyPart(attach2);
}
}
message.setContent(multipart);
message.setSentDate(new Date());
message.saveChanges();
return message;
}
/**
* get the session to transport mails
*/
private static Session getSession() {
// set the properties for connecting the mail server
final Properties props = new Properties();
props.setProperty("mail.debug", "true");
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.host", SMTP_HOST);
props.setProperty("mail.smtp.auth", "true");
props.setProperty("mail.smtp.port", SMTP_PORT);
// ssl
props.setProperty("mail.imap.ssl.enable", "true");
props.setProperty("mail.imap.ssl.socketFactory.class", "DummySSLSocketFactory");
props.setProperty("mail.imap.ssl.socketFactory.fallback", "false");
// create the session
final Session session = Session.getDefaultInstance(props);
return session;
}
}
|