赞
踩
SMTP:简单邮件传输协议,用于发送电子邮件的协议
POP3 ( Post Office Protocol - Version3) :用于接收电子邮件的标准协议
IMAP ( Internet Mail Access Protocol) :互联网消息协议,是POP3的替代协议
第一步,引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
第二步 写接口与实现类
public interface MailService {
void sendSimpleMail(String from,String to,String cc,String subject,String content);
}
import com.example.rabbitmq.service.MailService; import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.stereotype.Service; import javax.annotation.Resource; @Service public class tomailimpl implements MailService { @Resource JavaMailSender javaMailSender; /** * 最简单的邮件发送 * @param from 发送方 * @param to 接收方 * @param cc 第三方(非必填) * @param subject 标题 * @param content 内容 */ @Override public void sendSimpleMail(String from, String to, String cc, String subject, String content){ SimpleMailMessage simpleMailMessage = new SimpleMailMessage(); simpleMailMessage.setFrom(from); simpleMailMessage.setTo(to); simpleMailMessage.setCc(cc); simpleMailMessage.setSubject(subject); simpleMailMessage.setText(content); javaMailSender.send(simpleMailMessage); } }
第三步 配置文件
spring:
mail:
host: smtp.qq.com
port: 465
username: 发送方@qq.com
password: QQ邮箱秘钥 不是QQ密码
default-encoding: UTF-8
properties:
mail:
smtp:
socketFactory:
class: javax.net.ssl.SSLSocketFactory
debug: true
测试
import com.example.rabbitmq.service.MailService; import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest; import javax.annotation.Resource; @SpringBootTest class RabbitmqApplicationTests { @Resource MailService mailService; @Test public void sendSimpleMail(){ mailService.sendSimpleMail("自己@qq.com","发送方@qq.com","发送方@qq.com","标题111","Hello World"); } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。