原文链接:https://blog.youkuaiyun.com/jsjsjs1789/article/details/53352409
背景:
由于需要给合作方以压缩包的形式每天返回数据,基于我们自己写的分布式程序,而月末通过返回的数据,来与合作方进行对账,所以每天数据返回的成败就至关重要了,但又懒得每天去查看日志,刚好借助一下公司内部的邮件服务器。
go、go、go
package test.util;
import org.apache.log4j.Logger;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.*;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Properties;
/**
* Created by shengjk1 on 2016/11/4.
* Blog Address:http://blog.youkuaiyun.com/jsjsjs1789
*/
public class SendMail {
private static Logger logger = Logger.getLogger(SendMail.class);
private static SendMail instance = null;
private SendMail() {
}
public static SendMail getInstance() {
if (instance == null) {
instance = new SendMail();
}
return instance;
}
public void send(String to[], String cs[], String ms[], String subject,
String content, String formEmail, String fileList[]) {
try {
Properties p = new Properties(); // Properties p =
// System.getProperties();
p.put("mail.smtp.auth", "true");
p.put("mail.transport.protocol", "smtp");
p.put("mail.smtp.host", "xxxxx");//邮件服务器的地址
p.put("mail.smtp.port", "25");
// 建立会话
Session session = Session.getInstance(p);
Message msg = new MimeMessage(session); // 建立信息
BodyPart messageBodyPart = new MimeBodyPart();
Multipart multipart = new MimeMultipart();
msg.setFrom(new InternetAddress(formEmail)); // 发件人
String toList = null;
String toListcs = null;
String toListms = null;
// 发送,
if (to != null) {
toList = getMailList(to);
new InternetAddress();
InternetAddress[] iaToList = InternetAddress
.parse(toList);
msg.setRecipients(Message.RecipientType.TO, iaToList); // 收件人
}
// 抄送
if (cs != null) {
toListcs = getMailList(cs);
new InternetAddress();
InternetAddress[] iaToListcs = InternetAddress
.parse(toListcs);
msg.setRecipients(Message.RecipientType.CC, iaToListcs); // 抄送人
}
// 密送
if (ms != null) {
toListms = getMailList(ms);
new InternetAddress();
InternetAddress[] iaToListms = InternetAddress
.parse(toListms);
msg.setRecipients(Message.RecipientType.BCC, iaToListms); // 密送人
}
msg.setSentDate(new Date()); // 发送日期
msg.setSubject(subject); // 主题
msg.setText(content); // 内容
// 显示以html格式的文本内容
messageBodyPart.setContent(content, "text/html;charset=utf-8");
multipart.addBodyPart(messageBodyPart);
// 2.保存多个附件
if (fileList != null) {
addTach(fileList, multipart);
}
msg.setContent(multipart);
// 邮件服务器进行验证
Transport tran = session.getTransport("smtp");
tran.connect("xxx", //邮件服务器地址
"shengjk1@xxx.cn",//邮箱地址
"p@ssw0rd");//邮箱的密码
tran.sendMessage(msg, msg.getAllRecipients()); // 发送
System.out.println("邮件发送成功");
} catch (Exception e) {
logger.info("邮件发送时异常",e);
}
}
// 添加多个附件
public void addTach(String fileList[], Multipart multipart)
throws MessagingException, UnsupportedEncodingException {
for (int index = 0; index < fileList.length; index++) {
MimeBodyPart mailArchieve = new MimeBodyPart();
FileDataSource fds = new FileDataSource(fileList[index]);
mailArchieve.setDataHandler(new DataHandler(fds));
mailArchieve.setFileName(MimeUtility.encodeText(fds.getName(),
"utf-8", "B"));
multipart.addBodyPart(mailArchieve);
}
}
private String getMailList(String[] mailArray) {
StringBuffer toList = new StringBuffer();
int length = mailArray.length;
if (mailArray != null && length < 2) {
toList.append(mailArray[0]);
} else {
for (int i = 0; i < length; i++) {
toList.append(mailArray[i]);
if (i != (length - 1)) {
toList.append(",");
}
}
}
return toList.toString();
}
public static void forSend(String subject,String content){
SendMail send = SendMail.getInstance();
String to[] = { "shengjk1@xxx.cn"};//收件人的地址
String cs[] = null;
String ms[] = null;
if(content==null||content.length()==0){
content = "这是邮件内容,仅仅是测试,不需要回复";
}
String fromEmail = "shengjk1@xxx.cn";//发件人的地址
String[] arrArchievList = null;
// 2.保存多个附件
send.send(to, cs, ms, subject, content, fromEmail, arrArchievList);
}
// public static void main(String args[]) {
// forSend(null);
// }
}
- 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
pom.xml文件
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4</version>
</dependency>
- 1
- 2
- 3
- 4
- 5
<link rel="stylesheet" href="https://csdnimg.cn/release/phoenix/production/markdown_views-68a8aad09e.css">
</div>