forked from eacdy/spring-boot-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
189 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http:https://maven.apache.org/POM/4.0.0" xmlns:xsi="http:https://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http:https://maven.apache.org/POM/4.0.0 http:https://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<groupId>com.itmuch.boot</groupId> | ||
<artifactId>spring-boot-study</artifactId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
<packaging>pom</packaging> | ||
|
||
|
||
<modules> | ||
<module>spring-boot-banner</module> | ||
<module>spring-boot-mail</module> | ||
<module>spring-boot-lock</module> | ||
</modules> | ||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http:https://maven.apache.org/POM/4.0.0" xmlns:xsi="http:https://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http:https://maven.apache.org/POM/4.0.0 http:https://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-parent</artifactId> | ||
<version>2.1.3.RELEASE</version> | ||
<relativePath/> <!-- lookup parent from repository --> | ||
</parent> | ||
<groupId>com.itmuch</groupId> | ||
<artifactId>spring-boot-mail</artifactId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
<name>spring-boot-mail</name> | ||
<description>使用Spring boot发送Email</description> | ||
|
||
<properties> | ||
<java.version>1.8</java.version> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-mail</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-web</artifactId> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-maven-plugin</artifactId> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
</project> |
112 changes: 112 additions & 0 deletions
112
spring-boot-mail/src/main/java/com/itmuch/email/MailController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
package com.itmuch.email; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.autoconfigure.mail.MailProperties; | ||
import org.springframework.core.io.ClassPathResource; | ||
import org.springframework.mail.SimpleMailMessage; | ||
import org.springframework.mail.javamail.JavaMailSender; | ||
import org.springframework.mail.javamail.MimeMessageHelper; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import javax.mail.MessagingException; | ||
import javax.mail.internet.MimeMessage; | ||
|
||
/** | ||
* @author itmuch.com | ||
*/ | ||
@SuppressWarnings("ALL") | ||
@RestController | ||
public class MailController { | ||
@Autowired | ||
private JavaMailSender javaMailSender; | ||
@Autowired | ||
private MailProperties mailProperties; | ||
|
||
/** | ||
* 简单邮件测试 | ||
* | ||
* @return success | ||
*/ | ||
@GetMapping("/simple") | ||
public String simple() { | ||
SimpleMailMessage message = new SimpleMailMessage(); | ||
// 发件人邮箱 | ||
message.setFrom(this.mailProperties.getUsername()); | ||
// 收信人邮箱 | ||
message.setTo("[email protected]"); | ||
// 邮件主题 | ||
message.setSubject("简单邮件测试"); | ||
// 邮件内容 | ||
message.setText("简单邮件测试"); | ||
this.javaMailSender.send(message); | ||
return "success"; | ||
} | ||
|
||
/** | ||
* HTML内容邮件测试 | ||
* | ||
* @return success | ||
* @throws MessagingException | ||
*/ | ||
@GetMapping("/html") | ||
public String html() throws MessagingException { | ||
MimeMessage message = javaMailSender.createMimeMessage(); | ||
MimeMessageHelper messageHelper = new MimeMessageHelper(message); | ||
|
||
messageHelper.setFrom(this.mailProperties.getUsername()); | ||
messageHelper.setTo("[email protected]"); | ||
messageHelper.setSubject("HTML内容邮件测试"); | ||
// 第二个参数表示是否html,设为true | ||
messageHelper.setText("<h1>HTML内容..</h1>", true); | ||
|
||
this.javaMailSender.send(message); | ||
return "success"; | ||
} | ||
|
||
/** | ||
* 带附件的邮件测试 | ||
* | ||
* @return success | ||
* @throws MessagingException | ||
*/ | ||
@GetMapping("/attach") | ||
public String attach() throws MessagingException { | ||
MimeMessage message = this.javaMailSender.createMimeMessage(); | ||
// 第二个参数表示是否开启multipart模式 | ||
MimeMessageHelper messageHelper = new MimeMessageHelper(message, true); | ||
|
||
messageHelper.setFrom(this.mailProperties.getUsername()); | ||
messageHelper.setTo("[email protected]"); | ||
messageHelper.setSubject("带附件的邮件测试"); | ||
// 第二个参数表示是否html,设为true | ||
messageHelper.setText("<h1>HTML内容..</h1>", true); | ||
messageHelper.addAttachment("附件名称", | ||
new ClassPathResource("wx.jpg")); | ||
|
||
this.javaMailSender.send(message); | ||
return "success"; | ||
} | ||
|
||
/** | ||
* 内嵌附件的邮件测试 | ||
* | ||
* @return success | ||
* @throws MessagingException | ||
*/ | ||
@GetMapping("/inline-attach") | ||
public String inlineAttach() throws MessagingException { | ||
MimeMessage message = this.javaMailSender.createMimeMessage(); | ||
// 第二个参数表示是否开启multipart模式 | ||
MimeMessageHelper messageHelper = new MimeMessageHelper(message, true); | ||
messageHelper.setFrom(this.mailProperties.getUsername()); | ||
messageHelper.setTo("[email protected]"); | ||
messageHelper.setSubject("内嵌附件的邮件测试"); | ||
// 第二个参数表示是否html,设为true | ||
messageHelper.setText("<h1>HTML内容..<img src=\"cid:attach\"/></h1>", true); | ||
messageHelper.addInline("attach", new ClassPathResource("wx.jpg")); | ||
|
||
this.javaMailSender.send(message); | ||
return "success"; | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
spring-boot-mail/src/main/java/com/itmuch/email/SpringBootEmailApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.itmuch.email; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.CommandLineRunner; | ||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
import org.springframework.mail.SimpleMailMessage; | ||
import org.springframework.mail.javamail.JavaMailSender; | ||
|
||
@SpringBootApplication | ||
public class SpringBootEmailApplication { | ||
public static void main(String[] args) { | ||
SpringApplication.run(SpringBootEmailApplication.class, args); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
spring: | ||
mail: | ||
host: smtp.126.com | ||
username: [email protected] | ||
password: |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.