一、发邮件流程
使用javax.mail包中的api发送邮件,大致有5个步骤
- 1、继承Authenticator类,重写getPasswordAuthentication方法。用于登录校验。
- 2、创建一个Properties对象,用于存放SMTP服务器地址、端口号
- 3、用步骤1和2得到的对象创建一个Session对象,相当于邮箱登录
- 4、创建一个MimeMessage,设置发件人、收件人、抄送人、主题、内容,添加附件
- 5、Transport发送邮件
二、关于邮件协议和端口
常用的电子协议协议有SMTP、POP3、IMAP4,它们都隶属于TCP/IP簇,默认状态下,分别通过TCP端口25、110和143建立连接。
发送邮件基于SMTP协议。下面列出了常用的邮件服务器地址和端口。
sina.com:
POP3服务器地址:pop3.sina.com.cn(端口:110)
SMTP服务器地址:smtp.sina.com.cn(端口:25)
sinaVIP:
POP3服务器:pop3.vip.sina.com (端口:110)
SMTP服务器:smtp.vip.sina.com (端口:25)
sohu.com:
POP3服务器地址:pop3.sohu.com(端口:110)
SMTP服务器地址:smtp.sohu.com(端口:25)
126邮箱:
POP3服务器地址:pop.126.com(端口:110)
SMTP服务器地址:smtp.126.com(端口:25)
139邮箱:
POP3服务器地址:POP.139.com(端口:110)
SMTP服务器地址:SMTP.139.com(端口:25)
163.com:
POP3服务器地址:pop.163.com(端口:110)
SMTP服务器地址:smtp.163.com(端口:25)
QQ邮箱
POP3服务器地址:pop.qq.com(端口:110)
SMTP服务器地址:smtp.qq.com (端口:25)
QQ企业邮箱
POP3服务器地址:pop.exmail.qq.com (SSL启用 端口:995)
SMTP服务器地址:smtp.exmail.qq.com(SSL启用 端口:587/465)
yahoo.com:
POP3服务器地址:pop.mail.yahoo.com
SMTP服务器地址:smtp.mail.yahoo.com
yahoo.com.cn:
POP3服务器地址:pop.mail.yahoo.com.cn(端口:995)
SMTP服务器地址:smtp.mail.yahoo.com.cn(端口:587
HotMail
POP3服务器地址:pop3.live.com (端口:995)
SMTP服务器地址:smtp.live.com (端口:587)
gmail(google.com)
POP3服务器地址:pop.gmail.com(SSL启用 端口:995)
SMTP服务器地址:smtp.gmail.com(SSL启用 端口:587)
263.net:
POP3服务器地址:pop3.263.net(端口:110)
SMTP服务器地址:smtp.263.net(端口:25)
263.net.cn:
POP3服务器地址:pop.263.net.cn(端口:110)
SMTP服务器地址:smtp.263.net.cn(端口:25)
x263.net:
POP3服务器地址:pop.x263.net(端口:110)
SMTP服务器地址:smtp.x263.net(端口:25)
21cn.com:
POP3服务器地址:pop.21cn.com(端口:110)
SMTP服务器地址:smtp.21cn.com(端口:25)
Foxmail:
POP3服务器地址:POP.foxmail.com(端口:110)
SMTP服务器地址:SMTP.foxmail.com(端口:25)
china.com:
POP3服务器地址:pop.china.com(端口:110)
SMTP服务器地址:smtp.china.com(端口:25)
tom.com:
POP3服务器地址:pop.tom.com(端口:110)
SMTP服务器地址:smtp.tom.com(端口:25)
etang.com:
POP3服务器地址:pop.etang.com
SMTP服务器地址:smtp.etang.com
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.9</version>
<scope>test</scope>
</dependency>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
4.2. 方法重写
继承Authenticator并重写getPasswordAuthentication方法
package com.gblfy.mail;
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
public class MyAuthenticator extends Authenticator {
String userName = null;
String password = null;
public MyAuthenticator() {
}
public MyAuthenticator(String username, String password) {
this.userName = username;
this.password = password;
}
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(userName, password);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
4.3. 发送邮件核心类工具类EmailUtils
package com.gblfy.mail.util;
import com.gblfy.mail.MyAuthenticator;
import javax.activation.CommandMap;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.activation.MailcapCommandMap;
import javax.mail.*;
import javax.mail.internet.*;
import java.util.List;
import java.util.Properties;
/**
* 使用javax.mail发送邮件
*
* @author: gblfy
* @date 2021-06-22
*
* <p>
* 参数列表:
* 1.邮件服务器
* 2.发件人邮箱
* 3.发件人的授权密码
* 4.邮件主题
* 5.收件人,多个收件人以半角逗号分隔
* 6.抄送,多个抄送以半角逗号分隔
* 7.正文,可以用html格式的哟
* </p>
*/
public class EmailUtils {
private String smtpHost; // 邮件服务器地址
private String sendUserName; // 发件人的用户名
private String sendUserPass; // 发件人密码
private MimeMessage mimeMsg; // 邮件对象
private Multipart mp;// 附件添加的组件
private void init() {
// 创建一个密码验证器
MyAuthenticator authenticator = null;
authenticator = new MyAuthenticator(sendUserName, sendUserPass);
// 实例化Properties对象
Properties props = System.getProperties();
props.put("mail.smtp.host", smtpHost);
props.put("mail.smtp.auth", "true"); // 需要身份验证
props.put("mail.smtp.starttls.enable", "true");
// 建立会话
Session session = Session.getDefaultInstance(props, authenticator);
// 置true可以在控制台(console)上看到发送邮件的过程
session.setDebug(true);
// 用session对象来创建并初始化邮件对象
mimeMsg = new MimeMessage(session);
// 生成附件组件的实例
mp = new MimeMultipart();
}
private EmailUtils(String smtpHost, String sendUserName, String sendUserPass, String to, String cc, String mailSubject,
String mailBody, List<String> attachments) {
this.smtpHost = smtpHost;
this.sendUserName = sendUserName;
this.sendUserPass = sendUserPass;
init();
setFrom(sendUserName);
setTo(to);
setCC(cc);
setBody(mailBody);
setSubject(mailSubject);
if (attachments != null) {
for (String attachment : attachments) {
addFileAffix(attachment);
}
}
}
/**
* 邮件实体
*
* @param smtpHost 邮件服务器地址
* @param sendUserName 发件邮件地址
* @param sendUserPass 发件邮箱密码
* @param to 收件人,多个邮箱地址以半角逗号分隔
* @param cc 抄送,多个邮箱地址以半角逗号分隔
* @param mailSubject 邮件主题
* @param mailBody 邮件正文
* @param attachments 附件路径
* @return
*/
public static EmailUtils entity(String smtpHost, String sendUserName, String sendUserPass, String to, String cc,
String mailSubject, String mailBody, List<String> attachments) {
return new EmailUtils(smtpHost, sendUserName, sendUserPass, to, cc, mailSubject, mailBody, attachments);
}
/**
* 设置邮件主题
*
* @param mailSubject
* @return
*/
private boolean setSubject(String mailSubject) {
try {
mimeMsg.setSubject(mailSubject);
} catch (Exception e) {
return false;
}
return true;
}
/**
* 设置邮件内容,并设置其为文本格式或HTML文件格式,编码方式为UTF-8
*
* @param mailBody
* @return
*/
private boolean setBody(String mailBody) {
try {
BodyPart bp = new MimeBodyPart();
bp.setContent("<meta http-equiv=Content-Type content=text/html; charset=UTF-8>" + mailBody,
"text/html;charset=UTF-8");
// 在组件上添加邮件文本
mp.addBodyPart(bp);
} catch (Exception e) {
System.err.println("设置邮件正文时发生错误!" + e);
return false;
}
return true;
}
/**
* 添加一个附件
*
* @param filename 邮件附件的地址,只能是本机地址而不能是网络地址,否则抛出异常
* @return
*/
public boolean addFileAffix(String filename) {
try {
if (filename != null && filename.length() > 0) {
BodyPart bp = new MimeBodyPart();
FileDataSource fileds = new FileDataSource(filename);
bp.setDataHandler(new DataHandler(fileds));
bp.setFileName(MimeUtility.encodeText(fileds.getName(), "utf-8", null)); // 解决附件名称乱码
mp.addBodyPart(bp);// 添加附件
}
} catch (Exception e) {
System.err.println("增加邮件附件:" + filename + "发生错误!" + e);
return false;
}
return true;
}
/**
* 设置发件人地址
*
* @param from 发件人地址
* @return
*/
private boolean setFrom(String from) {
try {
mimeMsg.setFrom(new InternetAddress(from));
} catch (Exception e) {
return false;
}
return true;
}
/**
* 设置收件人地址
*
* @param to 收件人的地址
* @return
*/
private boolean setTo(String to) {
if (to == null)
return false;
try {
mimeMsg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
} catch (Exception e) {
return false;
}
return true;
}
/**
* 设置抄送
*
* @param cc
* @return
*/
private boolean setCC(String cc) {
if (cc == null) {
return false;
}
try {
mimeMsg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(cc));
} catch (Exception e) {
return false;
}
return true;
}
/**
* no object DCH for MIME type multipart/mixed报错解决
*/
private void solveError() {
MailcapCommandMap mc = (MailcapCommandMap) CommandMap.getDefaultCommandMap();
mc.addMailcap("text/html;; x-java-content-handler=com.sun.mail.handlers.text_html");
mc.addMailcap("text/xml;; x-java-content-handler=com.sun.mail.handlers.text_xml");
mc.addMailcap("text/plain;; x-java-content-handler=com.sun.mail.handlers.text_plain");
mc.addMailcap(
"multipart/*;; x-java-content-handler=com.sun.mail.handlers.multipart_mixed; x-java-fallback-entry=true");
mc.addMailcap("message/rfc822;; x-java-content-handler=com.sun.mail.handlers.message_rfc822");
CommandMap.setDefaultCommandMap(mc);
Thread.currentThread().setContextClassLoader(EmailUtils.class.getClassLoader());
}
/**
* 发送邮件
*
* @return
*/
public boolean send() throws Exception {
mimeMsg.setContent(mp);
mimeMsg.saveChanges();
System.out.println("正在发送邮件....");
solveError();
Transport.send(mimeMsg);
System.out.println("发送邮件成功!");
return true;
}
}
- 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
- 208
- 209
- 210
- 211
- 212
- 213
- 214
- 215
- 216
- 217
- 218
- 219
- 220
- 221
- 222
- 223
- 224
- 225
- 226
- 227
- 228
- 229
- 230
- 231
- 232
- 233
- 234
- 235
- 236
五、QQ邮件发送测试
5.1. QQ发送测试
package com.gblfy.mail;
import com.gblfy.mail.util.EmailUtils;
import org.junit.Test;
import java.util.Arrays;
import java.util.List;
public class MailApplicationTests {
@Test
public void sendEmail() throws Exception {
//QQ邮箱测试
String userName = "754263923@qq.com"; // 发件人邮箱
String password = "wzxsscqpvrikbbfa"; // 发件人密码,其实不一定是邮箱的登录密码,对于QQ邮箱来说是SMTP服务的授权文本
String smtpHost = "smtp.qq.com"; // 邮件服务器
//163邮箱测试
// String userName = "gblfy02@163.com"; // 发件人邮箱
// String password = "TBFJUSKCUOPEYOYU"; // 发件人密码,其实不一定是邮箱的登录密码,对于QQ邮箱来说是SMTP服务的授权文本
// String smtpHost = "smtp.163.com"; // 邮件服务器
String to = "guo_bin@sinosoft.com.cn"; // 收件人,多个收件人以半角逗号分隔
String cc = "songzihao@sinosoft.com.cn"; // 抄送,多个抄送以半角逗号分隔
String subject = "这是邮件的主题 163"; // 主题
String body = "这是邮件的正文163"; // 正文,可以用html格式的哟
List<String> attachments = Arrays.asList("D:\\安装包\\【繁星课堂】DataX3.0离线同步课件.pdf", "D:\\安装包\\权限控制优化需求.docx"); // 附件的路径,多个附件也不怕
EmailUtils emailUtils = EmailUtils.entity(smtpHost, userName, password, to, cc, subject, body, attachments);
emailUtils.send(); // 发送!
}
}
- 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
5.2. 查看QQ发送邮箱
5.3. 查看收件人收件箱
正常发送,正常接收!
六、163邮件发送测试
6.1. 163发送测试
package com.gblfy.mail;
import com.gblfy.mail.util.EmailUtils;
import org.junit.Test;
import java.util.Arrays;
import java.util.List;
public class MailApplicationTests {
@Test
public void sendEmail() throws Exception {
//QQ邮箱测试
// String userName = "754263923@qq.com"; // 发件人邮箱
// String password = "wzxsscqpvrikbbfa"; // 发件人密码,其实不一定是邮箱的登录密码,对于QQ邮箱来说是SMTP服务的授权文本
// String smtpHost = "smtp.qq.com"; // 邮件服务器
//163邮箱测试
String userName = "gblfy02@163.com"; // 发件人邮箱
String password = "TBFJUSKCUOPEYOYU"; // 发件人密码,其实不一定是邮箱的登录密码,对于QQ邮箱来说是SMTP服务的授权文本
String smtpHost = "smtp.163.com"; // 邮件服务器
String to = "guo_bin@xxx.com.cn"; // 收件人,多个收件人以半角逗号分隔
String cc = "songzihao@xxx.com.cn"; // 抄送,多个抄送以半角逗号分隔
String subject = "这是邮件的主题 163"; // 主题
String body = "这是邮件的正文163"; // 正文,可以用html格式的哟
List<String> attachments = Arrays.asList("D:\\安装包\\【繁星课堂】DataX3.0离线同步课件.pdf", "D:\\安装包\\权限控制优化需求.docx"); // 附件的路径,多个附件也不怕
EmailUtils emailUtils = EmailUtils.entity(smtpHost, userName, password, to, cc, subject, body, attachments);
emailUtils.send(); // 发送!
}
}
- 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
6.2. 查看163发送邮箱
6.3. 查看收件人收件箱
七、企业需求实战
7.1. Email工具类
package com.gblfy.email.util;
import com.gblfy.email.config.EmailAuthenticatorConfig;
import javax.activation.CommandMap;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.activation.MailcapCommandMap;
import javax.mail.*;
import javax.mail.internet.*;
import java.util.List;
import java.util.Properties;
/**
* 使用javax.mail发送邮件
*
* @author: gblfy
* @date 2021-10-15
*
* <p>
* 参数列表:
* 1.邮件服务器
* 2.发件人邮箱
* 3.发件人的授权密码
* 4.邮件主题
* 5.收件人,多个收件人以半角逗号分隔
* 6.抄送,多个抄送以半角逗号分隔
* 7.正文,可以用html格式的哟
* </p>
*/
public class EmailUtils {
private String smtpHost; // 邮件服务器地址
private String sendUserName; // 发件人的用户名
private String sendUserPass; // 发件人密码
private MimeMessage mimeMsg; // 邮件对象
private Multipart mp;// 附件添加的组件
private void init() {
// 创建一个密码验证器
EmailAuthenticatorConfig authenticator = null;
authenticator = new EmailAuthenticatorConfig(sendUserName, sendUserPass);
// 实例化Properties对象
Properties props = System.getProperties();
props.put("mail.smtp.host", smtpHost);
props.put("mail.smtp.auth", "true"); // 需要身份验证
//此属性待测试先注释
// props.put("mail.smtp.starttls.enable", "true");
props.put("mail.transport.protocol", "smtp");
// 建立会话
Session session = Session.getDefaultInstance(props, authenticator);
// 置true可以在控制台(console)上看到发送邮件的过程
session.setDebug(true);
// 用session对象来创建并初始化邮件对象
mimeMsg = new MimeMessage(session);
// 生成附件组件的实例
mp = new MimeMultipart();
}
private EmailUtils(String smtpHost, String sendUserName, String sendUserPass, String to, String cc, String mailSubject,
String mailBody, List<String> attachments) {
this.smtpHost = smtpHost;
this.sendUserName = sendUserName;
this.sendUserPass = sendUserPass;
init();
setFrom(sendUserName);
setTo(to);
setCC(cc);
setBody(mailBody);
setSubject(mailSubject);
if (attachments != null) {
for (String attachment : attachments) {
addFileAffix(attachment);
}
}
}
/**
* 邮件实体
*
* @param smtpHost 邮件服务器地址
* @param sendUserName 发件邮件地址
* @param sendUserPass 发件邮箱密码
* @param to 收件人,多个邮箱地址以半角逗号分隔
* @param cc 抄送,多个邮箱地址以半角逗号分隔
* @param mailSubject 邮件主题
* @param mailBody 邮件正文
* @param attachments 附件路径
* @return
*/
public static EmailUtils entity(String smtpHost, String sendUserName, String sendUserPass, String to, String cc,
String mailSubject, String mailBody, List<String> attachments) {
return new EmailUtils(smtpHost, sendUserName, sendUserPass, to, cc, mailSubject, mailBody, attachments);
}
/**
* 设置邮件主题
*
* @param mailSubject
* @return
*/
private boolean setSubject(String mailSubject) {
try {
mimeMsg.setSubject(mailSubject);
} catch (Exception e) {
return false;
}
return true;
}
/**
* 设置邮件内容,并设置其为文本格式或HTML文件格式,编码方式为UTF-8
*
* @param mailBody
* @return
*/
private boolean setBody(String mailBody) {
try {
BodyPart bp = new MimeBodyPart();
bp.setContent("<meta http-equiv=Content-Type content=text/html; charset=UTF-8>" + mailBody,
"text/html;charset=UTF-8");
// 在组件上添加邮件文本
mp.addBodyPart(bp);
} catch (Exception e) {
System.err.println("设置邮件正文时发生错误!" + e);
return false;
}
return true;
}
/**
* 添加一个附件
*
* @param filename 邮件附件的地址,只能是本机地址而不能是网络地址,否则抛出异常
* @return
*/
public boolean addFileAffix(String filename) {
try {
if (filename != null && filename.length() > 0) {
BodyPart bp = new MimeBodyPart();
FileDataSource fileds = new FileDataSource(filename);
bp.setDataHandler(new DataHandler(fileds));
bp.setFileName(MimeUtility.encodeText(fileds.getName(), "utf-8", null)); // 解决附件名称乱码
mp.addBodyPart(bp);// 添加附件
}
} catch (Exception e) {
System.err.println("增加邮件附件:" + filename + "发生错误!" + e);
return false;
}
return true;
}
/**
* 设置发件人地址
*
* @param from 发件人地址
* @return
*/
private boolean setFrom(String from) {
try {
mimeMsg.setFrom(new InternetAddress(from));
} catch (Exception e) {
return false;
}
return true;
}
/**
* 设置收件人地址
*
* @param to 收件人的地址
* @return
*/
private boolean setTo(String to) {
if (to == null)
return false;
try {
mimeMsg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
} catch (Exception e) {
return false;
}
return true;
}
/**
* 设置抄送
*
* @param cc
* @return
*/
private boolean setCC(String cc) {
if (cc == null) {
return false;
}
try {
mimeMsg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(cc));
} catch (Exception e) {
return false;
}
return true;
}
/**
* no object DCH for MIME type multipart/mixed报错解决
*/
private void solveError() {
MailcapCommandMap mc = (MailcapCommandMap) CommandMap.getDefaultCommandMap();
mc.addMailcap("text/html;; x-java-content-handler=com.sun.mail.handlers.text_html");
mc.addMailcap("text/xml;; x-java-content-handler=com.sun.mail.handlers.text_xml");
mc.addMailcap("text/plain;; x-java-content-handler=com.sun.mail.handlers.text_plain");
mc.addMailcap(
"multipart/*;; x-java-content-handler=com.sun.mail.handlers.multipart_mixed; x-java-fallback-entry=true");
mc.addMailcap("message/rfc822;; x-java-content-handler=com.sun.mail.handlers.message_rfc822");
CommandMap.setDefaultCommandMap(mc);
Thread.currentThread().setContextClassLoader(EmailUtils.class.getClassLoader());
}
/**
* 发送邮件
*
* @return
*/
public String send() throws Exception {
mimeMsg.setContent(mp);
mimeMsg.saveChanges();
System.out.println("正在发送邮件....");
solveError();
try {
Transport.send(mimeMsg);
return "发送邮件成功!";
} catch (MessagingException e) {
e.printStackTrace();
return "发送邮件异常!";
}
}
/**
* 对账结果通知邮件模板
*
* @param named 称呼
* @param totalNum 对账批次总个数
* @param succBatchNum 对账成功批次个数
* @param checkInformSucc 对账成功批次
* @param checkInformMiddle 对账中批次
* @param checkInformDiff 对账差异批次
* @param smtpHost 邮件代理服务器IP
* @param sendUserName 发送人邮箱
* @param sendUserPass 发送人邮箱秘钥
* @param to 收件人邮箱
* @param cc 抄送人邮箱
* @param attachments 附件
*/
public static String checkInformEmailNotice(String named, int totalNum, int succBatchNum, String checkInformSucc,
String checkInformMiddle, String checkInformDiff, String smtpHost,
String sendUserName, String sendUserPass, String to, String cc,
List<String> attachments) throws Exception {
String subject = "【装载结果通知】" + DateDeal.getYesterday() + " 共报送" + totalNum + "个批次,对账成功" + succBatchNum + "个批次"; // 主题
String body = "<pre>\n" +
named + ",您好!\n" +
"\n" +
" 对账状态信息:\n" +
" 对账成功批次:" + checkInformSucc + "。\n" +
" 对账中批次 :" + checkInformMiddle + ",请您及时处理。\n" +
" 对账存在差异批次:" + checkInformDiff + ",请您及时处理。\n" +
"</pre>";
EmailUtils emailUtils = EmailUtils.entity(smtpHost, sendUserName, sendUserPass, to, cc, subject, body, attachments);
return emailUtils.send();// 发送!
}
/**
* @param named 称呼
* @param totalLoadNum 装载总批次个数
* @param loadAllSuccNum 装载全部成功批次个数
* @param loadMiddle 装载中批次
* @param LoadAllSucc 装载全部成功批次
* @param partLoadSucc 装载部分成功批次
* @param withoutLoad 尚未装载批次
* @param smtpHost 邮件代理服务器IP
* @param sendUserName 发送人邮箱
* @param sendUserPass 发送人邮箱秘钥
* @param to 收件人邮箱
* @param cc 抄送人邮箱
* @param attachments 附件
* @return
* @throws Exception
*/
public static String loadEmailNotice(String named, int totalLoadNum, int loadAllSuccNum, String loadMiddle, String LoadAllSucc,
String partLoadSucc, String withoutLoad, String smtpHost,
String sendUserName, String sendUserPass, String to, String cc,
List<String> attachments) throws Exception {
String subject = "【数据装载结果通知】" + DateDeal.getToday() + " 共报送" + totalLoadNum + "个批次,装载全部成功" + loadAllSuccNum + "个批次"; // 主题
String body = "<pre>\n" +
named + ",您好!\n" +
"\n" +
" 数据装载状态信息:\n" +
" 装载中批次:" + loadMiddle + "。\n" +
" 装载全部成功批次:" + LoadAllSucc + "。\n" +
" 装载部分成功批次 :" + partLoadSucc + "\n" +
" 尚未装载批次:" + withoutLoad + "。\n" +
"</pre>";
EmailUtils emailUtils = EmailUtils.entity(smtpHost, sendUserName, sendUserPass, to, cc, subject, body, attachments);
return emailUtils.send();// 发送!
}
}
- 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
- 208
- 209
- 210
- 211
- 212
- 213
- 214
- 215
- 216
- 217
- 218
- 219
- 220
- 221
- 222
- 223
- 224
- 225
- 226
- 227
- 228
- 229
- 230
- 231
- 232
- 233
- 234
- 235
- 236
- 237
- 238
- 239
- 240
- 241
- 242
- 243
- 244
- 245
- 246
- 247
- 248
- 249
- 250
- 251
- 252
- 253
- 254
- 255
- 256
- 257
- 258
- 259
- 260
- 261
- 262
- 263
- 264
- 265
- 266
- 267
- 268
- 269
- 270
- 271
- 272
- 273
- 274
- 275
- 276
- 277
- 278
- 279
- 280
- 281
- 282
- 283
- 284
- 285
- 286
- 287
- 288
- 289
- 290
- 291
- 292
- 293
- 294
- 295
- 296
- 297
- 298
- 299
- 300
- 301
- 302
- 303
- 304
- 305
- 306
- 307
- 308
- 309
- 310
- 311
7.2. 单元测试
package com.gblfy.email.util;
import com.gblfy.email.modular.check.dto.Emailsubject;
import org.junit.Test;
public class EmailTest {
@Test
public void checkInformEmailNoticeTest() throws Exception {
//测试模拟数据
Emailsubject emailsubject = new Emailsubject();
emailsubject.setSendEmail("xxx@xxx.com");
emailsubject.setProxyserverHost("192.168.xxx.xxx");
emailsubject.setSendEmailPwd("Gblfy.com");
emailsubject.setReceiveEmail("xxx@xxx.com");
emailsubject.setCcEmail("xxx@xxx.com,xxx@xxx.com,xxx@xxx.com,xxx@xxx.com");
String msg = EmailUtils.checkInformEmailNotice("运维人员", 5, 5, "001,002",
"0002", "004,00005 ", emailsubject.getProxyserverHost(),
emailsubject.getSendEmail(), emailsubject.getSendEmailPwd(), emailsubject.getReceiveEmail(), emailsubject.getCcEmail(), null);
System.out.println("msg" + msg);
}
@Test
public void loadEmailNoticeTest() throws Exception {
//测试模拟数据
Emailsubject emailsubject = new Emailsubject();
emailsubject.setSendEmail("xxx@xxx.com");
emailsubject.setProxyserverHost("192.168.xxx.xxx");
emailsubject.setSendEmailPwd("Gblfy.com");
emailsubject.setReceiveEmail("xxx@xxx.com");
emailsubject.setCcEmail("xxx@xxx.com,xxx@xxx.com,xxx@xxx.com,xxx@xxx.com");
String msg = EmailUtils.loadEmailNotice("运维人员", 5, 5, "001,002",
"003", "004,0005 ", "008", emailsubject.getProxyserverHost(),
emailsubject.getSendEmail(), emailsubject.getSendEmailPwd(), emailsubject.getReceiveEmail(), emailsubject.getCcEmail(), null);
System.out.println("msg" + msg);
}
}
- 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