标签:ati require required help 引入 服务 ide stack 设置
第一步添加mail的依赖
<!--引入mail的依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
第二步编写邮箱的
yml配置文件
spring:
#邮箱配置
mail:
host: smtp.qq.com
username: 2631245486@qq.com
#QQ邮箱的授权码
password: 授权码
default-encoding: UTF-8
properties:
mail:
smtp:
auth: true
starttls:
enable: true
required: true
properties的配置文件
#qq邮箱配置
# JavaMailSender 邮件发送的配置
spring.mail.host=smtp.qq.com
spring.mail.username=用户qq邮箱
#QQ邮箱的授权码
spring.mail.password=授权码
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true
spring.mail.default-encoding=UTF-8
#163邮箱配置
spring.mail.host=smtp.163.com
spring.mail.username=用户163邮箱
spring.mail.password=邮箱密码
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true
spring.mail.default-encoding=UTF-8
编写两个发送邮件的接口
package www.it.com.server;
import java.io.File;
/**
* @author wangjie:
* @version 创建时间:2019年8月27日 上午10:13:08
* @Description 类描述:
*/
public interface MailServer {
/**
* @param sendUser 邮件接收人
* @param title 邮件的标题
* @param text 邮件的内容
*/
void sendMailServer(String sendUser,String title,String text);
/**
* 带有附件邮箱的发送
* @param sendUser
* @param title
* @param text
* @param file
*/
void sendFileMail(String sendUser,String title,String text,File file);
}
接口的实现
package www.it.com.server.impl;
import java.io.File;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import org.apache.logging.log4j.message.SimpleMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
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.stereotype.Service;
import www.it.com.server.MailServer;
/**
* @author wangjie:
* @version 创建时间:2019年8月27日 上午10:13:58
* @Description 类描述:
*/
@Service
public class MailServerImpl implements MailServer {
@Value("${spring.mail.username}")
private String fromUser;
@Autowired
private JavaMailSender javaMailSender;
public String getFromUser() {
return fromUser;
}
public void setFromUser(String fromUser) {
this.fromUser = fromUser;
}
@Override
public void sendMailServer(String sendUser, String title, String text) {
//创建邮件的实体 用于封装发送邮件需要的信息
SimpleMailMessage simpleMailMessage=new SimpleMailMessage();
//邮件的发送人
simpleMailMessage.setFrom(fromUser);
//邮件接收人
simpleMailMessage.setTo(sendUser);
//邮件的标题
simpleMailMessage.setSubject(title);
//邮件的内容
simpleMailMessage.setText(text);
//发送邮件
javaMailSender.send(simpleMailMessage);
}
@Override
public void sendFileMail(String sendUser, String title, String text, File file) {
MimeMessage mimeMessage = null;
try {
mimeMessage =javaMailSender.createMimeMessage();
//创建mimeMessageHelper对象用于处理带有附件的邮件信息
MimeMessageHelper mimeMessageHelper=new MimeMessageHelper(mimeMessage,true);
mimeMessageHelper.setFrom(fromUser);
mimeMessageHelper.setTo(sendUser);
mimeMessageHelper.setSubject(title);
mimeMessageHelper.setText(text);
FileSystemResource r = new FileSystemResource(file);
//添加附件
mimeMessageHelper.addAttachment("附件", r);
javaMailSender.send(mimeMessage);
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
controller编码
package www.it.com.controller;
import java.io.File;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import www.it.com.server.MailServer;
/**
* @author wangjie:
* @version 创建时间:2019年8月27日 上午9:52:30
* @Description 类描述:邮件发送的controller
*/
@RestController()
@RequestMapping("/mail")
public class MailController {
@Autowired
private MailServer mailServer;
/**
* 简单邮件的发送
* @return
*/
@RequestMapping("/send")
public String sendMail() {
//2694433816
mailServer.sendMailServer("2631245486@qq.com", "你好", "明天去你家玩");
return "success";
}
/**
* 发送带有附件的邮件
*/
@RequestMapping("/sendFile")
public String sendFileMail() {
File file=new File("C://Users//DELL//Desktop//学习资料.txt");
mailServer.sendFileMail("2631245486@qq.com", "你好dsf", "这是第二封带有附件的邮件", file);
return "success";
}
}
授权码生成的步骤
登录邮箱选择设置
选择账户
滑动到下面开启相应的服务 选择生成授权码
标签:ati require required help 引入 服务 ide stack 设置
原文地址:https://www.cnblogs.com/wang66a/p/12069301.html