Skip to content

Commit bb733f7

Browse files
committed
20210902 发送邮件最佳实践
1 parent c148a6d commit bb733f7

File tree

16 files changed

+3975
-1
lines changed

16 files changed

+3975
-1
lines changed

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,18 @@
1818

1919

2020

21+
## 项目2:javamail-practice
22+
23+
### 参考文档
24+
25+
- [JavaMail 实现邮件推送功能(网易邮箱)](https://blog.csdn.net/weixin_43967679/article/details/107879747)
26+
27+
28+
29+
30+
31+
32+
2133
## TODO
2234

2335
- SendMail
@@ -34,6 +46,7 @@
3446
# 其他参考文档
3547

3648
- [IDEA如何设置author头注解](https://blog.csdn.net/weixin_42555514/article/details/106826894)
49+
- [Intellij Idea自动添加注释的方法](https://www.jianshu.com/p/09139b425cc3)
3750

3851

3952

javamail-practice/pom.xml

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,38 @@
1616
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
1717
<maven.compiler.source>1.7</maven.compiler.source>
1818
<maven.compiler.target>1.7</maven.compiler.target>
19+
<poi.version>[3.17,)</poi.version>
20+
<!-- Logging -->
21+
<logback.version>[1.2.0,)</logback.version>
22+
<slf4j.version>1.7.32</slf4j.version>
1923
</properties>
2024

2125
<dependencies>
2226
<dependency>
2327
<groupId>javax.mail</groupId>
2428
<artifactId>mail</artifactId>
25-
<version>1.5.0-b01</version>
29+
<version>1.4.7</version>
30+
</dependency>
31+
<!-- Logging with SLF4J & LogBack -->
32+
<dependency>
33+
<groupId>org.slf4j</groupId>
34+
<artifactId>slf4j-api</artifactId>
35+
<version>${slf4j.version}</version>
36+
<scope>compile</scope>
37+
</dependency>
38+
<!-- https://mvnrepository.com/artifact/ch.qos.logback/logback-classic -->
39+
<dependency>
40+
<groupId>ch.qos.logback</groupId>
41+
<artifactId>logback-classic</artifactId>
42+
<version>1.2.5</version>
43+
<scope>test</scope>
2644
</dependency>
2745

46+
<dependency>
47+
<groupId>org.freemarker</groupId>
48+
<artifactId>freemarker</artifactId>
49+
<version>2.3.20</version>
50+
</dependency>
2851

2952
<dependency>
3053
<groupId>junit</groupId>
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
package com.coderdream;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
6+
import java.io.IOException;
7+
import java.io.InputStream;
8+
import java.util.Properties;
9+
10+
/**
11+
* 邮件发送配置信息加载类
12+
*
13+
* @date 2014年4月26日 下午2:52:06
14+
* @author 曹志飞
15+
* @Description:
16+
* @project mailUtil
17+
*/
18+
public class ConfigLoader {
19+
//日志记录对象
20+
private static Logger log = LoggerFactory.getLogger(ConfigLoader.class);
21+
// 配置文件路径
22+
private static String mailPath = "mail/mail.properties";
23+
// 邮件发送SMTP主机
24+
private static String server;
25+
// 发件人邮箱地址
26+
private static String sender;
27+
// 发件人邮箱用户名
28+
private static String username;
29+
// 发件人邮箱密码
30+
private static String password;
31+
// 发件人显示昵称
32+
private static String nickname;
33+
34+
// 收件人显示昵称
35+
private static String mailto;
36+
37+
static {
38+
// 类初始化后加载配置文件
39+
InputStream in = ConfigLoader.class.getClassLoader()
40+
.getResourceAsStream(mailPath);
41+
Properties props = new Properties();
42+
try {
43+
props.load(in);
44+
} catch (IOException e) {
45+
log.error("load mail setting error,pleace check the file path:"
46+
+ mailPath);
47+
log.error(e.toString(), e);
48+
}
49+
server = props.getProperty("mail.server");
50+
sender = props.getProperty("mail.sender");
51+
username = props.getProperty("mail.username");
52+
password = props.getProperty("mail.password");
53+
nickname = props.getProperty("mail.nickname");
54+
mailto = props.getProperty("mail.mailto");
55+
log.debug("load mail setting success,file path:" + mailPath);
56+
}
57+
58+
public static String getServer() {
59+
return server;
60+
}
61+
62+
public static String getSender() {
63+
return sender;
64+
}
65+
66+
public static String getUsername() {
67+
return username;
68+
}
69+
70+
public static String getPassword() {
71+
return password;
72+
}
73+
74+
public static String getNickname() {
75+
return nickname;
76+
}
77+
78+
public static void setMailPath(String mailPath) {
79+
ConfigLoader.mailPath = mailPath;
80+
}
81+
82+
public static Logger getLog() {
83+
return log;
84+
}
85+
86+
public static void setLog(Logger log) {
87+
ConfigLoader.log = log;
88+
}
89+
90+
public static String getMailto() {
91+
return mailto;
92+
}
93+
94+
public static void setMailto(String mailto) {
95+
ConfigLoader.mailto = mailto;
96+
}
97+
98+
public static String getMailPath() {
99+
return mailPath;
100+
}
101+
102+
public static void setServer(String server) {
103+
ConfigLoader.server = server;
104+
}
105+
106+
public static void setSender(String sender) {
107+
ConfigLoader.sender = sender;
108+
}
109+
110+
public static void setUsername(String username) {
111+
ConfigLoader.username = username;
112+
}
113+
114+
public static void setPassword(String password) {
115+
ConfigLoader.password = password;
116+
}
117+
118+
public static void setNickname(String nickname) {
119+
ConfigLoader.nickname = nickname;
120+
}
121+
122+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package com.coderdream;
2+
3+
import javax.activation.DataHandler;
4+
import javax.activation.DataSource;
5+
import javax.activation.FileDataSource;
6+
import javax.mail.*;
7+
import javax.mail.internet.*;
8+
import java.util.Properties;
9+
10+
public class EmailHelper {
11+
12+
private String host;
13+
private String username;
14+
private String password;
15+
private String from;
16+
17+
private String to;
18+
private String subject;
19+
private String htmlContent;
20+
private String imagePath;
21+
22+
public EmailHelper(String host, String username, String password, String from) throws AddressException, MessagingException{
23+
this.host = host;
24+
this.username = username;
25+
this.password = password;
26+
this.from = from;
27+
}
28+
29+
public void sendWithImage() throws Exception {
30+
31+
Properties props = new Properties();
32+
props.put("mail.smtp.auth", "true");
33+
props.put("mail.smtp.host", host);
34+
35+
final String username1 = username;
36+
final String password1 = password;
37+
38+
Session session = Session.getInstance(props, new javax.mail.Authenticator() {
39+
protected PasswordAuthentication getPasswordAuthentication() {
40+
return new PasswordAuthentication(username1, password1);
41+
}
42+
});
43+
44+
Message message = new MimeMessage(session);
45+
message.setFrom(new InternetAddress(from));
46+
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
47+
message.setSubject(subject);
48+
Multipart multipart = new MimeMultipart("related");
49+
50+
System.out.println(" html ");
51+
BodyPart htmlPart = new MimeBodyPart();
52+
htmlContent = "<img src=\"cid:image\">" + htmlContent;
53+
htmlPart.setContent(htmlContent, "text/html");
54+
multipart.addBodyPart(htmlPart);
55+
56+
System.out.println(" image ");
57+
System.out.println("image path : " + imagePath);
58+
BodyPart imgPart = new MimeBodyPart();
59+
DataSource fds = new FileDataSource(this.imagePath);
60+
61+
imgPart.setDataHandler(new DataHandler(fds));
62+
imgPart.setHeader("Content-ID", "<image>");
63+
64+
multipart.addBodyPart(imgPart);
65+
message.setContent(multipart);
66+
Transport.send(message);
67+
68+
System.out.println(" Sent -| ");
69+
}
70+
71+
public void setTo(String to) {
72+
this.to = to;
73+
}
74+
75+
public void setSubject(String subject) {
76+
this.subject = subject;
77+
}
78+
79+
public void setHtmlContent(String htmlContent) {
80+
this.htmlContent = htmlContent;
81+
}
82+
83+
public String getImagePath() {
84+
return imagePath;
85+
}
86+
87+
public void setImagePath(String imagePath) {
88+
this.imagePath = imagePath;
89+
}
90+
}

0 commit comments

Comments
 (0)