spring boot 发送邮件
来源:互联网 发布:网络社交的利与弊论点 编辑:程序博客网 时间:2024/06/05 12:48
配置application.properties 文件
spring.mail.host=smtp.qq.comspring.mail.username=770960436@qq.comspring.mail.password=xxxxxspring.mail.properties.mail.smtp.auth=truespring.mail.properties.mail.smtp.starttls.enable=truespring.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); }}
阅读全文
0 0
- spring boot 发送邮件
- spring boot 发送邮件
- spring boot 发送邮件
- spring boot 发送邮件
- spring boot发送邮件
- spring boot 发送邮件
- Spring Boot 发送邮件
- 47. Spring Boot发送邮件
- spring-boot mail 邮件发送
- 47. Spring Boot发送邮件
- Spring Boot实战之发送邮件
- Spring Boot中使用JavaMailSender发送邮件
- Spring Boot中使用JavaMailSender发送邮件
- Spring Boot 配置 JavaMailSender发送邮件
- Spring Boot中使用JavaMailSender发送邮件
- Spring Boot (教程十四:发送邮件)
- Spring Boot中使用JavaMailSender发送邮件
- Spring Boot中使用JavaMailSender发送邮件
- android中的BroadcastReceiver简单用法
- struts2 环境配置 (1)
- 根据underscore精简的函数(数组篇)
- Android中ScrollView和ListView嵌套使用时,进入界面不是从起始位置也即是不是从顶部显示问题
- vue2.0 动态路由传参方法
- spring boot 发送邮件
- 学习Javascript闭包(Closure),从外部读取局部变量
- 如何给SNMP Trap通道加上同步机制
- Bootstrap学习之四:表单
- maven 找不到符号 或者总是报编译错误
- 动态范围规划(调整)Dynamic Range Control的一些心得(一)
- U-Box of Bricks
- cocoapods安装以及使用(图文详解)
- [一起学Hive]之二–Hive函数大全-完整版