Skip to content

Commit

Permalink
spring boot发送邮件
Browse files Browse the repository at this point in the history
  • Loading branch information
eacdy authored and limu.zl committed Mar 21, 2019
1 parent ec98b33 commit d3193db
Show file tree
Hide file tree
Showing 6 changed files with 189 additions and 0 deletions.
16 changes: 16 additions & 0 deletions pom.xml
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>
41 changes: 41 additions & 0 deletions spring-boot-mail/pom.xml
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 spring-boot-mail/src/main/java/com/itmuch/email/MailController.java
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";
}
}
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);
}
}
5 changes: 5 additions & 0 deletions spring-boot-mail/src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
spring:
mail:
host: smtp.126.com
username: [email protected]
password:
Binary file added spring-boot-mail/src/main/resources/wx.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit d3193db

Please sign in to comment.