Skip to content

Commit c148a6d

Browse files
committed
增加发送邮件最佳实践
1 parent e00b9b1 commit c148a6d

File tree

5 files changed

+172
-0
lines changed

5 files changed

+172
-0
lines changed

javamail-practice/pom.xml

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.coderdream</groupId>
8+
<artifactId>javamail-practice</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
<name>javamail-practice</name>
12+
<!-- FIXME change it to the project's website -->
13+
<url>http://www.example.com</url>
14+
15+
<properties>
16+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
17+
<maven.compiler.source>1.7</maven.compiler.source>
18+
<maven.compiler.target>1.7</maven.compiler.target>
19+
</properties>
20+
21+
<dependencies>
22+
<dependency>
23+
<groupId>javax.mail</groupId>
24+
<artifactId>mail</artifactId>
25+
<version>1.5.0-b01</version>
26+
</dependency>
27+
28+
29+
<dependency>
30+
<groupId>junit</groupId>
31+
<artifactId>junit</artifactId>
32+
<version>4.11</version>
33+
<scope>test</scope>
34+
</dependency>
35+
</dependencies>
36+
37+
<build>
38+
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
39+
<plugins>
40+
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
41+
<plugin>
42+
<artifactId>maven-clean-plugin</artifactId>
43+
<version>3.1.0</version>
44+
</plugin>
45+
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
46+
<plugin>
47+
<artifactId>maven-resources-plugin</artifactId>
48+
<version>3.0.2</version>
49+
</plugin>
50+
<plugin>
51+
<artifactId>maven-compiler-plugin</artifactId>
52+
<version>3.8.0</version>
53+
</plugin>
54+
<plugin>
55+
<artifactId>maven-surefire-plugin</artifactId>
56+
<version>2.22.1</version>
57+
</plugin>
58+
<plugin>
59+
<artifactId>maven-jar-plugin</artifactId>
60+
<version>3.0.2</version>
61+
</plugin>
62+
<plugin>
63+
<artifactId>maven-install-plugin</artifactId>
64+
<version>2.5.2</version>
65+
</plugin>
66+
<plugin>
67+
<artifactId>maven-deploy-plugin</artifactId>
68+
<version>2.8.2</version>
69+
</plugin>
70+
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
71+
<plugin>
72+
<artifactId>maven-site-plugin</artifactId>
73+
<version>3.7.1</version>
74+
</plugin>
75+
<plugin>
76+
<artifactId>maven-project-info-reports-plugin</artifactId>
77+
<version>3.0.0</version>
78+
</plugin>
79+
</plugins>
80+
</pluginManagement>
81+
</build>
82+
</project>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.coderdream;
2+
3+
/**
4+
* Hello world!
5+
*
6+
*/
7+
public class App
8+
{
9+
public static void main( String[] args )
10+
{
11+
System.out.println( "Hello World!" );
12+
}
13+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.coderdream;
2+
3+
import javax.mail.*;
4+
import javax.mail.internet.InternetAddress;
5+
import javax.mail.internet.MimeMessage;
6+
import java.util.Properties;
7+
8+
/**
9+
* @author :CoderDream
10+
* @date :Created in 2021/9/2 15:35
11+
* @description:邮件工具类
12+
* @modified By:CoderDream
13+
* @version: 1.0$
14+
*/
15+
public class SendMailUtil {
16+
17+
public static void sendEmail(String to, String content) throws Exception {
18+
//1.创建连接对象
19+
Properties properties = new Properties();
20+
//1.2 设置发送邮件的服务器
21+
properties.put("mail.smtp.host","smtp.126.com");
22+
//1.3 需要经过授权,用户名和密码的校验(必须)
23+
properties.put("mail.smtp.auth",true);
24+
//1.4 默认以25端口发送
25+
properties.setProperty("mail.smtp.socketFactory.class" , "javax.net.ssl.SSLSocketFactory");
26+
properties.setProperty("mail.smtp.socketFactory.fallback" , "false");
27+
properties.setProperty("mail.smtp.port" , "465");
28+
properties.setProperty("mail.smtp.socketFactory.port" , "465");
29+
//1.5 认证信息
30+
Session session = Session.getInstance(properties, new Authenticator() {
31+
@Override
32+
protected PasswordAuthentication getPasswordAuthentication() {
33+
return new PasswordAuthentication("acoder@126.com" , "授权码");
34+
}
35+
});
36+
//2.创建邮件对象
37+
Message message = new MimeMessage(session);
38+
//2.1设置发件人
39+
message.setFrom(new InternetAddress("acoder@126.com"));
40+
//2.2设置收件人
41+
message.setRecipient(Message.RecipientType.TO,new InternetAddress(to));
42+
//2.3设置抄送者(PS:没有这一条网易会认为这是一条垃圾短信,而发不出去)
43+
message.setRecipient(Message.RecipientType.CC,new InternetAddress("acoder@126.com"));
44+
//2.4设置邮件的主题
45+
message.setSubject("主题");
46+
//2.5设置邮件的内容
47+
message.setContent(""+content+"","text/html;charset=utf-8");
48+
//3.发送邮件
49+
Transport.send(message);
50+
}
51+
52+
//直接在main方法下测试
53+
public static void main(String[] args) throws Exception {
54+
sendEmail("xulin.wh@qq.com", "邮件内容");
55+
}
56+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.coderdream;
2+
3+
import static org.junit.Assert.assertTrue;
4+
5+
import org.junit.Test;
6+
7+
/**
8+
* Unit test for simple App.
9+
*/
10+
public class AppTest
11+
{
12+
/**
13+
* Rigorous Test :-)
14+
*/
15+
@Test
16+
public void shouldAnswerWithTrue()
17+
{
18+
assertTrue( true );
19+
}
20+
}

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
<version>1.0-SNAPSHOT</version>
1111
<modules>
1212
<module>junit5-practice</module>
13+
<module>javamail-practice</module>
1314
</modules>
1415

1516
<name>Maven</name>

0 commit comments

Comments
 (0)