SendMailUtil.java
7.78 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
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package com.lhcredit.common.utils;
import com.sun.mail.util.MailSSLSocketFactory;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.*;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.GeneralSecurityException;
import java.time.LocalDate;
import java.util.Date;
import java.util.Properties;
import java.util.Random;
/**
* 发送邮箱信息
*/
public class SendMailUtil {
private static String account; //登录用户名
private static String pass; //登录密码
private static String host; //服务器地址(邮件服务器)
private static String port; //端口
private static String protocol; //协议
private static String from; //发件人名称
public static final String subject = "辨险识财-邮箱绑定验证"; // 邮件标题
static {
Properties prop = new Properties();
try {
prop = PropertiesLoaderUtils.loadAllProperties("mail.properties");//生产环境
} catch (IOException e) {
System.out.println("加载属性文件失败");
}
account = prop.getProperty("username");
pass = prop.getProperty("password");
host = prop.getProperty("host");
port = prop.getProperty("port");
protocol = prop.getProperty("protocol");
from = prop.getProperty("from");
}
static class MyAuthenricator extends Authenticator {
String u = null;
String p = null;
public MyAuthenricator(String u, String p) {
this.u = u;
this.p = p;
}
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(u, p);
}
}
public static void send(String title, String content, String to) {
Properties prop = new Properties();
//协议
prop.setProperty("mail.transport.protocol", protocol);
//服务器
prop.setProperty("mail.smtp.host", host);
//端口
prop.setProperty("mail.smtp.port", port);
//使用smtp身份验证
prop.setProperty("mail.smtp.auth", "true");
//使用SSL,企业邮箱必需!
//开启安全协议
MailSSLSocketFactory sf = null;
try {
sf = new MailSSLSocketFactory();
sf.setTrustAllHosts(true);
} catch (GeneralSecurityException e1) {
e1.printStackTrace();
}
prop.put("mail.smtp.ssl.enable", "true");
prop.put("mail.smtp.ssl.socketFactory", sf);
Session session = Session.getDefaultInstance(prop, new MyAuthenricator(account, pass));
session.setDebug(true);
MimeMessage mimeMessage = new MimeMessage(session);
try {
//发件人
mimeMessage.setFrom(new InternetAddress(account, from)); //可以设置发件人的别名
//mimeMessage.setFrom(new InternetAddress(account)); //如果不需要就省略
//收件人
mimeMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
//主题
mimeMessage.setSubject(title);
//时间
mimeMessage.setSentDate(new Date());
//容器类,可以包含多个MimeBodyPart对象
Multipart mp = new MimeMultipart();
//MimeBodyPart可以包装文本,图片,附件
MimeBodyPart body = new MimeBodyPart();
//HTML正文
body.setContent(content, "text/html; charset=UTF-8");
mp.addBodyPart(body);
// //添加图片&附件
// body = new MimeBodyPart();
// body.attachFile(fileStr);
// mp.addBodyPart(body);
//设置邮件内容
mimeMessage.setContent(mp);
//仅仅发送文本
//mimeMessage.setText(content);
mimeMessage.saveChanges();
Transport.send(mimeMessage);
} catch (MessagingException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void send(String to, File affix, String affixName, String reportName) {
Properties prop = new Properties();
//协议
prop.setProperty("mail.transport.protocol", protocol);
//服务器
prop.setProperty("mail.smtp.host", host);
//端口
prop.setProperty("mail.smtp.port", port);
//使用smtp身份验证
prop.setProperty("mail.smtp.auth", "true");
//使用SSL,企业邮箱必需!
//开启安全协议
MailSSLSocketFactory sf = null;
try {
sf = new MailSSLSocketFactory();
sf.setTrustAllHosts(true);
} catch (GeneralSecurityException e1) {
e1.printStackTrace();
}
prop.put("mail.smtp.ssl.enable", "true");
prop.put("mail.smtp.ssl.socketFactory", sf);
Session session = Session.getDefaultInstance(prop, new MyAuthenricator(account, pass));
session.setDebug(true);
MimeMessage mimeMessage = new MimeMessage(session);
try {
//发件人
mimeMessage.setFrom(new InternetAddress(account, from)); //可以设置发件人的别名
//mimeMessage.setFrom(new InternetAddress(account)); //如果不需要就省略
//收件人
mimeMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
//主题
mimeMessage.setSubject(subject);
//时间
mimeMessage.setSentDate(new Date());
// 向multipart对象中添加邮件的各个部分内容,包括文本内容和附件
Multipart multipart = new MimeMultipart();
LocalDate now = LocalDate.now();
// 设置邮件的文本内容
BodyPart contentPart = new MimeBodyPart();
multipart.addBodyPart(contentPart);
/*// 添加附件
BodyPart messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(affix);
// 添加附件的内容
messageBodyPart.setDataHandler(new DataHandler(source));
// 添加附件的标题
// 这里很重要,通过下面的Base64编码的转换可以保证你的中文附件标题名在发送时不会变成乱码
sun.misc.BASE64Encoder enc = new sun.misc.BASE64Encoder();
messageBodyPart.setFileName(MimeUtility.encodeText(affixName, "utf-8", null));
// System.err.println(MimeUtility.encodeText(affixName));
multipart.addBodyPart(messageBodyPart);
*/
// 将multipart对象放到message中
mimeMessage.setContent(multipart);
// 保存邮件
mimeMessage.saveChanges();
Transport.send(mimeMessage);
} catch (MessagingException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
//获取6位随机数验证码
StringBuilder str = new StringBuilder();
Random random = new Random();
for (int i = 0; i < 6; i++) {
str.append(random.nextInt(10));
}
//contentPart.setContent("您的邮箱验证码为"+str.toString(), "text/html;charset=utf-8");
send("探雷针-邮箱绑定验证","您的邮箱验证码为"+str.toString(),"xueyf_bjzx@lhcis.com");
}
}