SpringBoot中发送Email(基于QQ邮箱版的)

环境:JDK1.8、MAVEN 3.6.1 、eclipse

1.添加email发送的依赖

当前的pom文件的内容:

    <properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.22.RELEASE</version>
		<relativePath />
	</parent>


	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<!-- 添加mail的支持 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-mail</artifactId>
		</dependency>
		<!-- junit配置 -->
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<configuration>
					<source>1.8</source>
					<target>1.8</target>
				</configuration>
			</plugin>
		</plugins>
	</build>

2.查看文档

发现官网上有这样一句话:
Spring Framework提供了一个使用 JavaMailSender界面发送电子邮件的简单抽象 ,Spring Boot为它提供了自动配置以及启动器模块。
所以:可以使用JavaMailSender发送邮箱查看配置

Email (MailProperties)email邮箱的配置

  • spring.mail.default-encoding=UTF-8 # Default MimeMessage encoding.
  • spring.mail.host= # SMTP server host. For instance smtp.example.com
  • spring.mail.jndi-name= # Session JNDI name. When set, takes - precedence to others Session settings.
  • spring.mail.password= # Login password of the SMTP server.
  • spring.mail.port= # SMTP server port.
  • spring.mail.properties.*= # Additional JavaMail Session properties.
  • spring.mail.protocol=smtp # Protocol used by the SMTP server.
  • spring.mail.test-connection=false # Test that the mail server is available on startup.
  • spring.mail.username= # Login user of the SMTP server.

3.配置QQ邮箱的信息

当前application.properties中的内容为:
spring.mail.host=smtp.qq.com
spring.mail.username= xxx@qq.com
spring.mail.password=//your password
spring.mail.default-encoding=UTF-8
spring.mail.port=465
spring.mail.properties.mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory
spring.mail.properties.mail.debug=true

4.编写工具类

当前的EmailMessageUtils内容如下

/**
 * @description 发送邮件的工具类
 * @author hy
 * @date 2019-08-13
 */
public class EmailMessageUtils {
	/**
	 * @description 创建简单的邮件对象
	 * @param context  邮件的内容
	 * @param sendTo   邮件的接收者
	 * @param sendFrom 发送邮件的人的邮箱
	 * @return 简单的邮件实例
	 */
	public static SimpleMailMessage write(String context, String sendTo, String sendFrom,String title) {
		SimpleMailMessage simpleMessage = new SimpleMailMessage();
		simpleMessage.setTo(sendTo);//邮件的接收者
		simpleMessage.setFrom(sendFrom);//发送邮件的人的邮箱
		simpleMessage.setText(context);//设置内容
		simpleMessage.setSubject(title);//设置标题
		return simpleMessage;
	}
}

5.编写入口类

当前的Application类中的内容:

/**
 * @description 使用SpringBoot发送Email
 * @author hy
 * @date 2019-08-13
 */
@RestController
@SpringBootApplication
public class Application {

	@Autowired
	JavaMailSender mailSender;

	@RequestMapping("/sendEmail")
	public String sendEmail(String sendFrom, String context, String sendTo, String title) {
		SimpleMailMessage mailMessage = EmailMessageUtils.write(context, sendTo, sendFrom, title);
		// 通过查询源码获得当前的send可以使用SimpleMailMessage发送一个简单的邮件
		mailSender.send(mailMessage);
		return "【发送邮箱成功!】";
	}

	public static void main(String[] args) {
			SpringApplication.run(Application.class, args);
	}
}

6.测试

结果:成功,但是需要使用自己的账号和密码,并且开启那个许可才能使用。网上有就不发了!

7.总结

1.当前发送邮箱的时候需要开启一定的东西,并且需要自己的用户名和密码,还有对应的邮箱的端口

2.发送邮箱可以使用JavaMailSender来发送邮箱,发送的邮件可以使用SimpleMailMessage来创建

以上纯属个人见解,如有问题请联系本人!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值