概述
springboot实现邮件发送功能:
1.整体目录结构:
2.新建一个springboot项目:在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>org.example</groupId>
<artifactId>springbootmail</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<artifactId>spring-boot-starter-parent</artifactId>
<groupId>org.springframework.boot</groupId>
<version>2.1.4.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
</dependencies>
</project>
3.在resources文件目录下写application.yml配置文件如下:
server:
port: 8999
spring:
application:
name: spring-boot-mail
mail:
host: smtp.qq.com
username: XX@qq.com
# 记得在qq邮箱,设置-》账户-》POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务开启pop3/smtp服务,获得授权码
password: qomvhauwjtvkjeaa
default-encoding: utf-8
#如果不写下面这3句,会报530错误
properties:
mail:
smtp:
auth: true
starttls:
enable: true
required: true
# 表面发送者
mail:
fromMail:
addr: xx@qq.com
4.编写发送邮件服务接口,实现发送邮件功能:
package com.nt.service;
/**
* 邮件服务接口
*/
public interface MailService {
/**
* 发送简单邮件
* @param to
* @param subject
* @param content
*/
public void sendMail(String to,String subject,String content);
}
}
package com.nt.service.impl;
import com.nt.service.MailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
/**
* 邮件服务类
*/
@Service
public class MailServiceImpl implements MailService {
@Autowired
private JavaMailSender mailSender;
@Value("${mail.fromMail.addr}")
private String form;
/**
发送简单邮件
@param to
接受者
@param subject
主题
@param content
内容
*/
@Override
public void sendMail(String to, String subject, String content) {
SimpleMailMessage mailMessage=new SimpleMailMessage();
mailMessage.setFrom(form);//发起者
mailMessage.setTo(to);//接受者
//如果发送给多个人的
//mailMessage.setTo("1xx.com","2xx.com","3xx.com");
mailMessage.setSubject(subject);
mailMessage.setText(content);
try {
mailSender.send(mailMessage);
System.out.println("发送简单邮件");
}catch (Exception e){
System.out.println("发送简单邮件失败");
}
}
}
5.编写邮件测试类:
import com.nt.Application;
import com.nt.service.MailService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
public class MailTest {
@Autowired
private MailService mailService;
@Value("${mail.fromMail.addr}")
private String form;
@Test
public void sendSimpleMail(){
mailService.sendMail(form,"简单邮件","SpringBoot实现邮件发送");
}
}
6.记得在根目录下编写个springboot的启动类,如果没写的,会报错哦。
package com.nt;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class,args);
}
}
7.run测试类MailTest:,查看结果:
实现定时器发送:
1.创建springboot项目:
编写定时器,可以看下:springboot实现定时器
编写邮件服务类:可以看下:springboot发送简单邮件
2.启动类启用定时:
@SpringBootApplication
@EnableScheduling//开启定时
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class,args);
}
}
3.主要业务代码:
package com.nt.util;
import com.nt.service.MailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
/**
定时任务类,实现定时发送邮件
*/
//要声明为bean,没有声明启动类启动无法实现定时效果
@Component
public class SchedulerTask {
@Autowired
private MailService mailService;
@Value("${mail.fromMail.addr}")
private String form;
private int count=1;
//表示每隔6秒发送一次邮件
@Scheduled(cron = "*/6 * * * * ?")
private void process(){
String content="springboot整合定时器实现定时邮件发送,这是第"+(count++)+"封邮件";
mailService.sendMail(form,"简单邮件",content);
System.out.println("发送定时邮件成功");
}
}
package com.nt.service.impl;
import com.nt.service.MailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
/**
邮件服务类
*/
@Service
public class MailServiceImpl implements MailService {
@Autowired
private JavaMailSender mailSender;
@Value("${mail.fromMail.addr}")
private String form;
/**
* 发送简单邮件
* @param to
接受者
* @param subject
主题
* @param content
内容
*/
@Override
public void sendMail(String to, String subject, String content) {
SimpleMailMessage mailMessage=new SimpleMailMessage();
mailMessage.setFrom(form);//发起者
mailMessage.setTo(to);//接受者
//如果发送给多个人的
//mailMessage.setTo("1xx.com","2xx.com","3xx.com");
mailMessage.setSubject(subject);
mailMessage.setText(content);
try {
mailSender.send(mailMessage);
System.out.println("发送简单邮件");
}catch (Exception e){
System.out.println("发送简单邮件失败");
}
}
}
4.run启动类application.class,测试效果:
注:有可能邮件会发送失败的原因:邮件发送过于频繁,网络异常等。
最后
以上就是爱笑睫毛为你收集整理的springboot实现邮件发送功能的全部内容,希望文章能够帮你解决springboot实现邮件发送功能所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复