spring boot 发送邮件
2017-05-17 18:03
741 查看
配置application.properties 文件
spring.mail.host=smtp.qq.com spring.mail.username=770960436@qq.com spring.mail.password=xxxxx spring.mail.properties.mail.smtp.auth=true spring.mail.properties.mail.smtp.starttls.enable=true spring.mail.properties.mail.smtp.starttls.required=true
注意密码不是qq密码,也不是邮箱密码是授权码
授权码生成规则:1.登录qq邮箱 2.设置 3.POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务 4.开启服务(POP3、SMTP...服务) 5.点击生成授权码 6.发送短信 7.获取授权码
pom.xml 配置
<?xml version="1.0" encoding="UTF-8"?> <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.didispace</groupId> <artifactId>Chapter4-5-1</artifactId> <version>1.0.0</version> <packaging>jar</packaging> <name>Chapter4-5-1</name> <description>Spring Boot project</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.3.2.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-velocity</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
发送邮件测试
package com.didispace; import org.apache.commons.collections.map.HashedMap; import org.apache.velocity.app.VelocityEngine; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.SpringApplicationConfiguration; 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.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.ui.velocity.VelocityEngineUtils; import javax.mail.internet.MimeMessage; import java.io.File; import java.util.Map; @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = Application.class) public class ApplicationTests { public static String from = "770960436@qq.com"; public static String to = "borntofight@sina.com"; @Autowired private JavaMailSender mailSender; @Autowired private VelocityEngine velocityEngine; @Test public void sendSimpleMail() throws Exception { SimpleMailMessage message = new SimpleMailMessage(); message.setFrom(from); message.setTo(to); message.setSubject("主题:简单邮件"); message.setText("测试邮件内容"); mailSender.send(message); } @Test public void sendAttachmentsMail() throws Exception { MimeMessage mimeMessage = mailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true); helper.setFrom(from); helper.setTo(to); helper.setSubject("主题:有附件"); helper.setText("有附件的邮件"); FileSystemResource file = new FileSystemResource(new File("weixin.jpg")); helper.addAttachment("附件-1.jpg", file); helper.addAttachment("附件-2.jpg", file); mailSender.send(mimeMessage); } @Test public void sendInlineMail() throws Exception { MimeMessage mimeMessage = mailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true); helper.setFrom(from); helper.setTo(to); helper.setSubject("主题:嵌入静态资源"); helper.setText("<html><body><img src=\"cid:weixin\" ></body></html>", true); FileSystemResource file = new FileSystemResource(new File("weixin.jpg")); helper.addInline("weixin", file); mailSender.send(mimeMessage); } @Test public void sendTemplateMail() throws Exception { MimeMessage mimeMessage = mailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true); helper.setFrom(from); helper.setTo(to); helper.setSubject("主题:模板邮件"); Map<String, Object> model = new HashedMap(); model.put("username", "didi"); String text = VelocityEngineUtils.mergeTemplateIntoString( velocityEngine, "template.vm", "UTF-8", model); helper.setText(text, true); mailSender.send(mimeMessage); } }
相关文章推荐
- Spring Boot实战之发送邮件
- 47. Spring Boot发送邮件【从零开始学Spring Boot】
- 在Spring Boot中使用JavaMailSender发送各类邮件
- springboot中发送各种类型的邮件
- 使用job自动抓取网页数据发送邮件,以springboot服务启动
- spring boot发送邮件
- 使用Springboot Email实现邮件发送
- yx: SpringBoot发送邮件功能
- 47. Spring Boot发送邮件【从零开始学Spring Boot】
- Spring Boot 菜鸟教程 26 集成邮件发送
- Spring Boot中利用JavaMailSender发送邮件的方法示例(附源码)
- Spring Boot(十二)集成spring-boot-starter-mail发送邮件
- Spring Boot中使用JavaMailSender发送邮件
- springboot邮件发送功能和两个常见问题
- 在Spring Boot中使用JavaMailSender发送各类邮件
- spring boot 实际应用(三)发送邮件实现
- 47. Spring Boot发送邮件【从零开始学Spring Boot】
- Spring Boot实战之发送邮件示例代码
- SpringBoot发送邮件
- Spring Boot 揭秘与实战(七) 实用技术篇 - Java Mail 发送邮件