引入依赖
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| <!-- 父配置--> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.4.3</version> <relativePath/> </parent> ...... <!-- springboot--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- 邮件--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency>
|
配置文件:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| spring: mail: host: smtp.qq.com username: ooahz@qq.com password: axxxxxxxxz port: 587 default-encoding: UTF-8 properties: mail: smtp: socketFactoryClass: javax.net.ssl.SSLSocketFactory debug: true
|
启动类:
1 2 3 4 5 6 7 8 9 10 11 12 13
| import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.scheduling.annotation.EnableScheduling;
@EnableScheduling @SpringBootApplication public class Application {
public static void main(String[] args){ SpringApplication.run(Application.class, args); } }
|
携带文字发送
接口类:
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
| import org.springframework.beans.factory.annotation.Autowired; import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RestController;
import java.util.Date;
@RestController public class email {
@Autowired JavaMailSender javaMailSender;
@PostMapping("/send") public String sendEmail() { SimpleMailMessage message = new SimpleMailMessage(); message.setSubject("这是一封测试邮件"); message.setFrom("ooahz@qq.com"); message.setTo("ooahz@qq.com");
message.setSentDate(new Date()); message.setText("这是一封测试邮件————from Ahzoo"); javaMailSender.send(message); return "发送成功"; }
}
|
因为开启了debug功能,所以邮件发送结果会在控制台打印:

测试发送:


携带附件发送
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
| import org.springframework.beans.factory.annotation.Autowired; import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.mail.javamail.MimeMessageHelper; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RestController;
import javax.mail.MessagingException; import javax.mail.internet.MimeMessage; import java.io.File; import java.util.Date;
@RestController public class email {
@Autowired JavaMailSender javaMailSender;
@PostMapping("/sendAttachment") public String sendFile() { MimeMessage mimeMessage = javaMailSender.createMimeMessage(); MimeMessageHelper helper = null; try { helper = new MimeMessageHelper(mimeMessage, true); helper.setSubject("测试附件"); helper.setFrom("ooahz@qq.com"); helper.setTo("ooahz@qq.com");
helper.setSentDate(new Date()); helper.setText("这是一封测试邮件————from Ahzoo"); File path = new File(ResourceUtils.getURL("classpath:").getPath()); helper.addAttachment("文件.txt", new File(path + "/static/z.txt")); javaMailSender.send(mimeMessage); return "发送成功"; } catch (Exception e) { e.printStackTrace(); return "发送失败" + e.getMessage(); } }
}
|

携带静态资源发送
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
| import org.springframework.beans.factory.annotation.Autowired; import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.mail.javamail.MimeMessageHelper; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RestController;
import javax.mail.MessagingException; import javax.mail.internet.MimeMessage; import java.io.File; import java.util.Date;
@RestController public class email {
@Autowired JavaMailSender javaMailSender;
@PostMapping("sendResource") public String sendImg() { MimeMessage mimeMessage = javaMailSender.createMimeMessage(); MimeMessageHelper helper = null; try { helper = new MimeMessageHelper(mimeMessage, true); helper.setSubject("测试静态资源"); helper.setFrom("ooahz@qq.com"); helper.setTo("ooahz@qq.com");
helper.setSentDate(new Date()); helper.setText("<p>图片1:</p><img src='cid:image1'/><p>图片2:</p><img src='cid:image1'/>", true);
File path = new File(ResourceUtils.getURL("classpath:").getPath()); helper.addInline("image1", new File(path + "/static/1.png")); helper.addInline("image2", new File(path + "/static/ahzoo.jpg")); javaMailSender.send(mimeMessage); return "发送成功"; } catch (Exception e) { e.printStackTrace(); return "发送失败" + e.getMessage(); } }
}
|

使用模板发送
常见的模板引擎主要有:Thymeleaf和Freemaker,这里不做演示
后记
项目结构:

项目源码