SpringBoot-10 发送邮件(含附件)

版权声明:本博客为自己总结亦或在网上发现的技术博文的转载。 如果文中有什么错误,欢迎指出。以免更多的人被误导。 邮箱:[email protected] 版权声明:本文为博主原创文章,博客地址:https://blog.csdn.net/ChinaMuZhe,未经博主允许不得转载。 https://blog.csdn.net/ChinaMuZhe/article/details/83654732

步骤一 添加依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-mail</artifactId>
</dependency>

步骤二 在application.properties中添加配置

spring.mail.default-encoding=UTF-8
#163邮箱配置
spring.mail.host=smtp.163.com
#qq
#spring.mail.host=smtp.qq.com
#126
#spring.mail.host=smtp.126.com
#授权码(不是密码)
spring.mail.password=授权码
#端口(smtp协议默认是25)
spring.mail.port=25
#协议
spring.mail.protocol=smtp
#发送方的邮箱
[email protected]

spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true

  根据自己的实际情况,使用163,126,qq服务器。这三者都需要开启pop3,smtp协议。然后获取授权码。否则会报535错误。

步骤三 编写工具类

/**
 * 邮件发送工具类
 * 
 * @author yyb
 *
 */
public class MailUtil {

	/**
	 * 发送邮件
	 * 
	 * @param from
	 *            邮件发送者,[email protected]等
	 * @param to
	 *            邮件接受者,[email protected]等
	 * @param content
	 *            邮件内容
	 * @param subject
	 *            邮件主题
	 * @param filePath
	 *            附件文件地址,无附件传null即可
	 * @throws MessagingException 
	 */
	public static void sendMail(JavaMailSender javaMailSender, String from, String[] to, String content, String subject,
			String filePath) throws MessagingException {
		if (StringUtils.isEmpty(filePath)) {
			sendMailNoAttachments(javaMailSender, from, to, content, subject);
		} else {
			MimeMessage mimeMessage = javaMailSender.createMimeMessage();
			MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, true);
			messageHelper.setTo(to);
			messageHelper.setFrom(from);
			messageHelper.setSubject(subject);
			messageHelper.setText(content);
			FileSystemResource file = new FileSystemResource(new File(filePath));
			String fileName = filePath.substring(filePath.lastIndexOf(File.separator));
			messageHelper.addAttachment(fileName, file);
			javaMailSender.send(mimeMessage);
		}
	}

	/**
	 * 发送无附件的邮件
	 * 
	 * @param to
	 * @param content
	 * @param sunject
	 * @param filePath
	 */
	private static void sendMailNoAttachments(JavaMailSender javaMailSender, String from, String[] to, String content,
			String subject) {
		// 建立邮件消息
		SimpleMailMessage mailMessage = new SimpleMailMessage();
		// 发送者
		mailMessage.setFrom(from);
		// 接收者
		mailMessage.setTo(to);
		// 发送的标题
		mailMessage.setSubject(subject);
		// 发送的内容
		mailMessage.setText(content);
		javaMailSender.send(mailMessage);
	}

}

通过以上三个步骤,就完成了发送邮件工具类的编写。在需要的服务中,直接调用工具类即可。需要注意的是:

1)from(邮件发送者)需要和application.properties中的发送者一致,否则会报错。501:mail from address must be same as authorization user。

2)JavaMailSender 直接在服务中注入后传参即可。

3)对filePath非空判断改为自己使用的方式就行

演示

@Resource
private JavaMailSender javaMailSender;

@RequestMapping("/send")
@ResponseBody
public String send() throws Exception {
	String[] to = { "[email protected]" };
	MailUtil.sendMail(javaMailSender, "[email protected]", to, "你好", "主题", null);
	return "success";
}

   只是用来测试,所以就在controller层进行了调用。

猜你喜欢

转载自blog.csdn.net/ChinaMuZhe/article/details/83654732