1.maven依赖
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.jz</groupId>
<artifactId>mail-project</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!--定时任务和@Slf4j注解日志的依赖 -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!--邮件发送 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
<version>2.1.3.RELEASE</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration> <!--设置程序执行的主类 -->
<mainClass>com.jz.ApplicationRun</mainClass>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
<finalName>test-log</finalName>
</build>
</project>
2.application.properties配置
#==================================
# servlet 相关配置
#----------------------------------
# 设置项目路径。默认是“/”
#server.servlet.context-path=/api
server.port=8888
spring.mail.host=smtp.qq.com
spring.mail.username=你的邮箱,这里QQ
spring.mail.password=申请的授权码
spring.mail.default-encoding=UTF-8
QQ邮箱都有,问题是怎么成为开发可以使用的,这里有一个百度经验,还是很简洁明了的,地址:https://jingyan.baidu.com/article/6d704a133a245f28db51caf5.html
3. 关键代码
3.1 HelloService
package cn.xdl.service;
import java.io.File;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
@Service
public class HelloService {
@Value("${spring.mail.username}")
private String from;
@Autowired
private JavaMailSender mailSender;
public void sayHello() {
System.out.println("Hello World");
}
/**
* 发送文本邮件
* @param to
* @param subject
* @param content
*/
public void sendSimpleMail(String to,String subject,
String content) {
SimpleMailMessage message = new SimpleMailMessage();
message.setTo(to);
message.setSubject(subject);
message.setText(content);
message.setFrom(from);
mailSender.send(message);
}
/**
* 发送html邮件
* @param to
* @param subject
* @param content
* @throws Exception
*/
public void sendHtmlMail(String to,String subject,
String content) throws Exception {
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message,true);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(content,true);
helper.setFrom(from);
mailSender.send(message);
}
/**
* 发送带附件邮件
* @param to
* @param subject
* @param content
* @param filePath
* @throws Exception
*/
public void sendAttachmentMail(String to,String subject,
String content,String filePath) throws Exception {
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message,true);
helper.setFrom(from);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(content,true);
FileSystemResource fileSystemResource = new FileSystemResource(new File(filePath));
String fileName = fileSystemResource.getFilename();
helper.addAttachment(fileName, fileSystemResource);
helper.addAttachment(fileName+"_test", fileSystemResource);
mailSender.send(message);
}
/**
* 发送带图片邮件
* @param to
* @param subject
* @param content
* @param rscPath
* @param rscId
* @throws MessagingException
*/
public void sendInlineResourceMail(String to,String subject,
String content,String rscPath,String rscId) throws MessagingException {
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message,true);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(content,true);
helper.setFrom(from);
FileSystemResource res = new FileSystemResource(new File(rscPath));
helper.addInline(rscId, res);
mailSender.send(message);
}
}
3.2 ApplicationRun
package cn.xdl;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ApplicationRun {
public static void main(String[] args) {
SpringApplication.run(ApplicationRun.class, args);
}
}
3.3 测试程序
package cn.xdl.test;
import javax.mail.MessagingException;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
import cn.xdl.service.HelloService;
@RunWith(SpringRunner.class)
@SpringBootTest
public class TestC {
@Autowired
private HelloService helloService;
@Autowired
private TemplateEngine templateEngine;
@Test
public void testHello() {
helloService.sayHello();
}
@Test
public void sendSimpleMailTest() {
String str = "曾经先后在互联网金融、第三方支付公司担任高级 Java 工程师、架构师、技术经理、技"
+ "术负责人等职务。在互联网金融工作期间,从零参与公司技术平台建设,组织平台进行过四次大架"
+ "构升级。目前在一家第三方支付公司做技术总监,负责支付公司微服务架构实践和大数据平台建设。";
helloService.sendSimpleMail("mail_test416@163.com", "这是第一封邮件",str);
}
@Test
public void sendHtmlMailTest() {
String str = "<html lang=\"en\">\r\n" +
" <head>\r\n" +
" </head>\r\n" +
" <body>\r\n" +
" <h3>Java并发编程高阶技术-高性能并发框架源码解析与实战</h3>\r\n" +
" </body>\r\n" +
"</html>";
try {
helloService.sendHtmlMail("mail_test416@163.com", "Spring Boot 发送邮件", str);
} catch (Exception e) {
e.printStackTrace();
};
}
@Test
public void sendFileMailTest() {
String str = "为了您能及时收到会员专属信息,请将 vip@dailydeals.vip.com加入您的邮件地址列表";
try {
helloService.sendAttachmentMail("mail_test416@163.com", "POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务", str, "D:\\Images/1.jpg");
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
public void sendInLineResourceMailTest() {
String rscPath = "D:/Images/1.jpg";
String rscId = "neo001";
String content = "<html lang=\"en\">\r\n" +
" <head>\r\n" +
" </head>\r\n" +
" <body>\r\n" +
" <h3>Java并发编程高阶技术-高性能并发框架源码解析与实战</h3>\r\n" +
"<img src='cid:"+rscId+"'/>"+
" </body>\r\n" +
"</html>";
String to = "mail_test416@163.com";
String subject = "慕课网_来慕课网学前端框架_上班族提升必备2222222222222";
try {
helloService.sendInlineResourceMail(to, subject, content, rscPath, rscId);
} catch (MessagingException e) {
e.printStackTrace();
}
}
@Test
public void testTemplateMailTest() {
Context context = new Context();
context.setVariable("id", "007");
String emailContent = templateEngine.process("emailTemplate", context);
String to = "mail_test416@163.com";
String subject = "文明交流互鉴推动构建人类命运共同体";
try {
helloService.sendHtmlMail(to, subject, emailContent);
} catch (Exception e) {
e.printStackTrace();
}
}
}
3.4 发送结果成功图
比较忙,等闲暇会将源码发布上来,目前需要源码的可以评论留邮箱,或者发我邮箱讲明要源码,我是Eclipse开发的。邮箱:liurui416@qq.com
有不对的地方欢迎指正!!!
个人反感收费积分,但另一方面必须尊重知识产权,有积分的可以下载,没有的可以私我邮箱,下载地址:https://download.csdn.net/download/liurui50/11071712