Java发送邮件(含附件)

前几天写了一个Java发送邮件的帮助类i,可以发送QQ和163的邮箱,也可以发送附件,写个一个主要的方法,其他的可以自己封装。代码如下:

引入pom:

1
2
3
4
5
6
<dependency>
<groupId>Javax.mail</groupId>
<artifactId>Javax.mail-api</artifactId>
<version>1.6.2</version>
</dependency>

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
package com.pay.utils;

import com.pay.utils.enums.MailType;
import org.springframework.util.StringUtils;

import Javax.activation.*;
import Javax.mail.*;
import Javax.mail.internet.*;
import Javax.mail.util.ByteArrayDataSource;
import Java.io.*;
import Java.util.HashMap;
import Java.util.Map;
import Java.util.Properties;

public class MailSender {
private MailType mailType;
private String userName;
private String passWord;
private Properties properties;


public MailSender(MailType mailType, String userName, String passWord) {
this.mailType = mailType;
this.userName = userName;
this.passWord = passWord;
this.properties = getProperties();
}


public boolean sender(String recivers, String cc, String mailTitle, String mailContent, boolean isHtml, Map<String, byte[]> mapFile) throws MessagingException, IOException {
Session session = Session.getInstance(properties);
//2.通过session获取Transport对象(发送邮件的核心API)
Transport ts = session.getTransport();
//3.通过邮件用户名密码链接
ts.connect(properties.getProperty("mail.host"), userName, this.passWord);
//4.创建邮件
MimeMessage mm = new MimeMessage(session);
//设置发件人
mm.setFrom(new InternetAddress(userName));
Address[] address = new InternetAddress().parse(recivers);
mm.setRecipients(Message.RecipientType.TO, address);

//设置抄送人
if (!StringUtils.isEmpty(cc)) {
mm.setRecipient(Message.RecipientType.CC, new InternetAddress(cc));
}
mm.setSubject(mailTitle);
if (!isHtml) {
mailContent = String.format("<pre>%s</pre>", mailContent);
}
// mm.setContent(mailContent, "text/html;charset=utf-8");
// 创建多重消息
Multipart multipart = new MimeMultipart();

BodyPart bodyPart = new MimeBodyPart();
bodyPart.setContent(mailContent, "text/html;charset=utf-8");
multipart.addBodyPart(bodyPart);
if (mapFile != null) {
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");
mc.addMailcap("message/rfc822;; x-Java-content-handler=com.sun.mail.handlers.message_rfc822");
CommandMap.setDefaultCommandMap(mc);

for (Map.Entry<String, byte[]> map : mapFile.entrySet()) {
BodyPart messageBodyPart = new MimeBodyPart();
InputStream inputStream = new ByteArrayInputStream(map.getValue());
DataSource source = new ByteArrayDataSource(inputStream, "application/txt");
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(MimeUtility.encodeText(map.getKey()));
multipart.addBodyPart(messageBodyPart);
}

mm.setContent(multipart);
}
//5.发送电子邮件
ts.sendMessage(mm, mm.getAllRecipients());
return true;
}

private Properties getProperties() {
if (this.mailType.equals(MailType.m163)) {
Properties prop = new Properties();
prop.put("mail.host", "smtp.163.com");
prop.put("mail.transport.protocol", "smtp");
prop.put("mail.smtp.auth", true);
return prop;
}
if (this.mailType.equals(MailType.qq)) {
Properties prop = new Properties();
prop.setProperty("mail.host", "smtp.qq.com");
prop.setProperty("mail.transport.protocol", "smtp");
prop.setProperty("mail.smtp.auth", "true");
prop.setProperty("mail.smtp.socketFactory.class", "Javax.net.ssl.SSLSocketFactory");
prop.setProperty("mail.smtp.port", "465");
prop.setProperty("mail.smtp.socketFactory.port", "465");
return prop;
}
return null;
}

}


```

``` Java
public enum MailType {
m163, qq
}