其他分享
首页 > 其他分享> > springboot项目创建笔记30 之《发送邮件服务》

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

作者:互联网

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