ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

springboot项目创建笔记30 之《发送邮件服务》

2021-11-03 15:32:43  阅读:159  来源: 互联网

标签:springboot sendTo 30 mail springframework org import 邮件 String


1、pom文件添加依赖

<!-- 邮件发送 -->
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-mail</artifactId>
</dependency>

2、添加邮件服务类MailService.java
建立包:com.example.mail

package com.example.mail;

import java.io.File;
import java.util.Map;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

import org.springframework.beans.factory.annotation.Autowired;
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.Component;

/**
 * 邮件服务类
 *
 */

@Component
public class MailService {

	@Autowired
	private JavaMailSender mailSender;

	/**
	 * 发送纯文本邮件
	 * 
	 * @param sendFrom
	 * @param sendTo
	 * @param titel
	 * @param content
	 */
	public void sendSimpleMail(String sendFrom, String sendTo, String titel, String content) {
		try {
			SimpleMailMessage message = new SimpleMailMessage();
			message.setFrom(sendFrom);
			message.setTo(sendTo.contains(";") ? sendTo.split(";") : new String[]{sendTo});
			message.setSubject(titel);
			message.setText(content);
			mailSender.send(message);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	/**
	 * 发送富文本邮件
	 * 
	 * @param sendFrom
	 * @param sendTo
	 * @param titel
	 * @param content
	 * @param attachmentMap
	 */
	public void sendAttachmentsMail(String sendFrom, String sendTo, String titel, String content,
			Map<String, String> attachmentMap) {

		MimeMessage mimeMessage = mailSender.createMimeMessage();

		try {
			MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true); // 默认为false,显示原始html代码,无效果
			helper.setFrom(sendFrom);
			helper.setTo(sendTo.contains(";") ? sendTo.split(";") : new String[]{sendTo});
			helper.setSubject(titel);
			helper.setText(content);

			if (attachmentMap != null) {
				attachmentMap.entrySet().stream().forEach(entrySet -> {

					File file = new File(entrySet.getValue());
					if (file.exists()) {
						try {
							helper.addAttachment(entrySet.getKey(), new FileSystemResource(file));
						} catch (MessagingException e) {
							e.printStackTrace();
						}
					}
				});
			}

			mailSender.send(mimeMessage);
		} catch (MessagingException ex) {
			ex.printStackTrace();
		}
	}

}

3、测试文件MailTest.java

package myboot;

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;

import com.example.mail.MailService;
import com.example.myboot.MybootApplication;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = MybootApplication.class)
public class MailTest {

	@Value("${test.mail.from}")
	private String mailFrom;

	@Value("${test.mail.to}")
	private String mailTo;

	@Autowired
	MailService mailService;

	@Test
	public void mailTest() {
		mailService.sendSimpleMail(mailFrom, mailTo, "myboot测试邮件", "你好,这是测试邮件");
	}
}

4、application-dev.yml添加

#邮件发送
spring:
    mail:
      username: xxx@xxx.com
      password: 123456
      host: smtp.exmail.qq.com
      port: 465
      properties:
        mail:
          transport:
            protocol: smtp
          smtp:
            socketFactory:
              class: javax.net.ssl.SSLSocketFactory
            port: ${spring.mail.port}
            auth: true
            starttls:
              enable: true
              required: true

#test对账结果通知邮箱地址
test:
  mail:
    #发件人邮箱
    from: aaa@aaa.com
    #收件人邮箱(支持同时发多个,用分号分隔)
    to: bbb@bbb.com

5、执行测试方法
 

标签:springboot,sendTo,30,mail,springframework,org,import,邮件,String
来源: https://blog.csdn.net/csj50/article/details/121121627

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有