后端消息消息-邮件
cmyang1. 开通邮箱发送权限
可以使用163,qq等邮箱,一般是打开邮箱设置页面,找到POP3/SMTP
服务,并开启服务,记录授权码
2. 发送邮件
2.1 简单发送
1 2 3 4
| <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency>
|
1 2 3 4 5 6 7
| spring: mail: host: smtp.qq.com port: 465 username: 969312613@qq.com password: xxxxxx protocol: smtps
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| @Data public class SendMailDTO {
private String receiver;
private String title;
private String content; }
|
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
| @Service @Slf4j public class MailServiceImpl implements MailService {
@Resource private JavaMailSender javaMailSender;
@Override public void sendMail(SendMailDTO sendMailDTO) { try { MimeMessage mimeMessage = javaMailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true); helper.setFrom(((JavaMailSenderImpl)javaMailSender).getUsername()); helper.setTo(sendMailDTO.getReceiver()); helper.setSubject(sendMailDTO.getTitle()); helper.setText(sendMailDTO.getContent()); javaMailSender.send(mimeMessage); } catch (Exception e) { log.error("发送邮件异常,异常信息:{}, 参数:{}", e.getMessage(), sendMailDTO.getTitle()); e.printStackTrace(); throw new RuntimeException(e); } } }
|
2.2 通过spring-mail发送带附件的邮件
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
| @Data public class SendMailDTO {
private String host;
private Integer port;
private String name;
private String username;
private String password;
private String[] receivers;
private String[] cc;
private String title;
private String content;
private List<EmailAttachmentDTO> attachments; }
|
1 2 3 4 5 6 7 8 9 10 11 12 13
| @Data public class EmailAttachmentDTO {
private String fileName;
private InputStreamSource inputStreamSource; }
|
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
| @Service @Slf4j public class MailServiceImpl implements MailService {
@Override public void sendMail(SendMailDTO sendMailDTO) { try { JavaMailSender javaMailSender = initMailSender(sendMailDTO); MimeMessage mimeMessage = javaMailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true); helper.setFrom(new InternetAddress(sendMailDTO.getUsername(), MimeUtility.encodeText(sendMailDTO.getName(), "UTF-8", "B"))); helper.setTo(sendMailDTO.getReceivers()); helper.setCc(sendMailDTO.getCc()); helper.setSubject(sendMailDTO.getTitle()); helper.setText(sendMailDTO.getContent(), true); List<EmailAttachmentDTO> attachments = sendMailDTO.getAttachments(); for (EmailAttachmentDTO attachment : attachments) { helper.addAttachment(MimeUtility.encodeWord(attachment.getFileName(),"UTF-8","B"), attachment.getInputStreamSource()); }
javaMailSender.send(mimeMessage); } catch (Exception e) { log.error("发送邮件异常,异常信息:{}, 参数:{}", e.getMessage(), sendMailDTO.getTitle()); e.printStackTrace(); throw new RuntimeException(e); } }
private JavaMailSender initMailSender(SendMailDTO sendMailDTO) { System.setProperty("mail.mime.splitlongparameters", "false"); JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl(); javaMailSender.setHost(sendMailDTO.getHost()); javaMailSender.setPort(sendMailDTO.getPort()); javaMailSender.setPassword(sendMailDTO.getPassword()); javaMailSender.setUsername(sendMailDTO.getUsername()); javaMailSender.setDefaultEncoding("UTF-8"); Properties properties = new Properties(); properties.setProperty("mail.smtp.starttls.enable", "true"); properties.setProperty("mail.smtp.starttls.required", "true"); properties.setProperty("mail.smtp.auth", "true"); properties.setProperty("mail.smtp.timeout", "100000"); properties.setProperty("mail.smtp.connectiontimeout", "100000"); properties.setProperty("mail.smtp.writetimeout", "100000"); properties.setProperty("mail.mime.splitlongparameters", "false"); javaMailSender.setJavaMailProperties(properties); return javaMailSender; } }
|