springboot发送邮件

<?xml version="1.0"?>
<project
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
	xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>com.lance</groupId>
		<artifactId>spring-boot-parent</artifactId>
		<version>1.1</version>
		<relativePath>../spring-boot-parent/pom.xml</relativePath>
	</parent>


	<artifactId>spring-boot-email</artifactId>
	<packaging>war</packaging>
	<name>spring-boot-email</name>
	<url>http://maven.apache.org</url>


	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-mail</artifactId>
		</dependency>
		<dependency>
			<groupId>com.google.guava</groupId>
			<artifactId>guava</artifactId>
			<version>19.0</version>
		</dependency>
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>fastjson</artifactId>
			<version>1.2.12</version>
		</dependency>
		<!-- common -->
		<dependency>
			<groupId>org.apache.commons</groupId>
			<artifactId>commons-lang3</artifactId>
			<version>3.2.1</version>
		</dependency>
	</dependencies>


	<build>
		<finalName>spring-boot-email</finalName>
	</build>
</project>


//application.properties
# IDENTITY (ContextIdApplicationContextInitializer)
spring.application.index=ActiveMQ.v1.1
spring.application.name=ActiveMQ Boot


#Server
server.port=80
server.jsp-servlet.class-name=org.apache.jasper.servlet.JspServlet
server.error.whitelabel.enabled=true


#LOG
logging.config=classpath:log4j2.xml


# Email (MailProperties)
spring.mail.default-encoding=UTF-8
spring.mail.host=smtp.qq.com
spring.mail.password=123456
spring.mail.port=25
spring.mail.protocol=smtp
spring.mail.test-connection=false
spring.mail.username=server1@qq.com
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.transport.protocol=smtps
spring.mail.properties.mail.smtps.quitwait=false


package com.lance.email;

import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;

@Component("emailSender")
public class EmailSender {
	private Logger logger = LogManager.getLogger(getClass());
	private String defaultFrom = "server1@qq.com";
	@Autowired
	private JavaMailSender javaMailSender;
	
	/**
	 * 发送邮件
	 * @param to			收件人地址
	 * @param subject		邮件主题
	 * @param content		邮件内容
	 * @author lance
	 */
	public boolean sender(String to, String subject, String content) {
		return sender(to, subject, content, true);
	}
	
	/**
	 * 发送邮件
	 * @param to			收件人地址
	 * @param subject		邮件主题
	 * @param content		邮件内容
	 * @param html			是否格式内容为HTML
	 * @author lance
	 */
	public boolean sender(String to, String subject, String content, boolean html){
		if(StringUtils.isBlank(to)) {
			logger.error("邮件发送失败:收件人地址不能为空.");
			return false;
		}
		return sender(new String[]{to}, subject, content, html);
	}
	
	/**
	 * sender message
	 * @param to
	 * @param subject
	 * @param content
	 * @param html
	 * @return
	 */
	public boolean sender(String[] to, String subject, String content, boolean html){
		if(to == null || to.length == 0) {
			logger.error("批量邮件发送失败:收件人地址不能为空.");
			return false;
		}
		
		SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
		simpleMailMessage.setFrom(defaultFrom);
		simpleMailMessage.setTo(to);
		simpleMailMessage.setSubject(subject);
		simpleMailMessage.setText(content);
		
		try {
			javaMailSender.send(simpleMailMessage);
			return true;
		} catch (MailException e) {
			logger.error("发送邮件错误:{}, TO:{}, Subject:{},Content:{}.", e, to, subject, content);
			return false;
		}
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值