概述
发送邮件,一个发送,一个接收,在软件中一般都是使用发送。
发送需要一个邮箱,这时候需要去那邮箱进行授权。
邮箱类型 |
SMTP服务器地址 |
端口号 |
QQ邮箱 |
smtp.qq.com |
465或587 |
sina邮箱 |
smtp.sina.cn |
465或587 |
126邮箱 |
smtp.126.com |
465或994 |
aliyun邮箱 |
smtp.aliyun.com |
465或994 |
163邮箱 |
smtp.163.com |
465或994 |
yeah邮箱 |
smtp.yeah.net |
465或994 |
使用
依赖
1 2 3 4 5
| <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency>
|
配置文件
1 2 3 4 5 6 7 8
| spring.mail.host=smtp.163.com spring.mail.username=xxx@163.com spring.mail.password=授权码 spring.mail.port=465 spring.mail.properties.mail.smtp.starttls.enbale=true spring.mail.properties.mail.smtp.starttls.required=true spring.mail.properties.mail.smtp.ssl.enable=true spring.mail.properties.mail.debug=true
|
类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
| import java.io.Serializable;
public class MailRequest implements Serializable {
private String sendTo;
private String subject;
private String text;
private String filePath;
public String getSendTo() { return sendTo; }
public void setSendTo(String sendTo) { this.sendTo = sendTo; }
public String getSubject() { return subject; }
public void setSubject(String subject) { this.subject = subject; }
public String getText() { return text; }
public void setText(String text) { this.text = text; }
public String getFilePath() { return filePath; }
public void setFilePath(String filePath) { this.filePath = filePath; } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| public interface SendMailService {
void sendSimpleMail(MailRequest mailRequest);
void sendHtmlMail(MailRequest mailRequest); }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
| import org.slf4j.Logger; import org.slf4j.LoggerFactory; 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 org.springframework.util.Assert; import org.springframework.util.StringUtils;
import javax.mail.MessagingException; import javax.mail.internet.MimeMessage; import java.io.File; import java.util.Date;
@Service public class SendMailServiceImpl implements SendMailService {
@Autowired private JavaMailSender javaMailSender;
@Value("${spring.mail.username}") private String sendMailer;
private static final Logger logger = LoggerFactory.getLogger(SendMailServiceImpl.class);
public void checkMail(MailRequest mailRequest) { Assert.notNull(mailRequest,"邮件请求不能为空"); Assert.notNull(mailRequest.getSendTo(), "邮件收件人不能为空"); Assert.notNull(mailRequest.getSubject(), "邮件主题不能为空"); Assert.notNull(mailRequest.getText(), "邮件收件人不能为空"); }
@Override public void sendSimpleMail(MailRequest mailRequest) { SimpleMailMessage message = new SimpleMailMessage(); checkMail(mailRequest); message.setFrom(sendMailer); message.setTo(mailRequest.getSendTo().split(",")); message.setSubject(mailRequest.getSubject()); message.setText(mailRequest.getText()); message.setSentDate(new Date());
javaMailSender.send(message); logger.info("发送邮件成功:{}->{}",sendMailer,mailRequest.getSendTo()); }
@Override public void sendHtmlMail(MailRequest mailRequest) { MimeMessage message = javaMailSender.createMimeMessage(); checkMail(mailRequest); try { MimeMessageHelper helper = new MimeMessageHelper(message,true); helper.setFrom(sendMailer); helper.setTo(mailRequest.getSendTo().split(",")); helper.setSubject(mailRequest.getSubject()); helper.setText(mailRequest.getText(),true); helper.setSentDate(new Date());
String filePath = mailRequest.getFilePath(); if (StringUtils.hasText(filePath)) { FileSystemResource file = new FileSystemResource(new File(filePath)); String fileName = filePath.substring(filePath.lastIndexOf(File.separator)); helper.addAttachment(fileName,file); } javaMailSender.send(message); logger.info("发送邮件成功:{}->{}",sendMailer,mailRequest.getSendTo()); } catch (MessagingException e) { logger.error("发送邮件时发生异常!",e); } } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;
@RestController @RequestMapping("/send-mail") public class SendMailController { @Autowired private SendMailService sendMailService;
@PostMapping("/simple") public void SendSimpleMessage(@RequestBody MailRequest mailRequest) { sendMailService.sendSimpleMail(mailRequest); }
@PostMapping("/html") public void SendHtmlMessage(@RequestBody MailRequest mailRequest) { sendMailService.sendHtmlMail(mailRequest);} }
|
测试
http://localhost:8080/send-mail/simple
1 2 3 4 5
| { "sendTo": "xxx@sina.com", "subject": "验证码", "text": "您的验证码是123456" }
|
http://localhost:8080/send-mail/html
1 2 3 4 5
| { "sendTo": "xxx@163.com", "subject": "验证码", "text": "<h1>您的验证码是123456</h1>" }
|