SpringBoot 集成邮件功能解析

无情 阅读:230 2021-03-31 20:57:17 评论:0

1、项目结构:

2、核心代码:

package com.zzg.email.entity; 
 
import org.springframework.core.io.FileSystemResource; 
 
/** 
 *  
 * @ClassName:  EmailBean    
 * @Description: 邮件实体对象  
 * @author: *** -zzg 
 * @date:   2019年4月10日 上午9:11:33    
 *      
 * @Copyright: 2019 www.digipower.cn  
 * 注意:本内容仅限于***开发有限公司内部使用,禁止用于其他的商业目的 
 */ 
public class EmailBean { 
	// 发送人 
	private String from; 
	// 接收人 
	private String to; 
	// 主题 
	private String subject; 
	// 内容 
	private String text; 
	// 附件 
	private FileSystemResource file; 
	// 附件名称 
	private String attachmentFilename; 
	// 邮件类型 
	private Integer type; 
	 
	// set 和  get 方法 
	public String getFrom() { 
		return from; 
	} 
	public void setFrom(String from) { 
		this.from = from; 
	} 
	public String getTo() { 
		return to; 
	} 
	public void setTo(String to) { 
		this.to = to; 
	} 
	public String getSubject() { 
		return subject; 
	} 
	public void setSubject(String subject) { 
		this.subject = subject; 
	} 
	public String getText() { 
		return text; 
	} 
	public void setText(String text) { 
		this.text = text; 
	} 
	public FileSystemResource getFile() { 
		return file; 
	} 
	public void setFile(FileSystemResource file) { 
		this.file = file; 
	} 
	public String getAttachmentFilename() { 
		return attachmentFilename; 
	} 
	public void setAttachmentFilename(String attachmentFilename) { 
		this.attachmentFilename = attachmentFilename; 
	} 
	public Integer getType() { 
		return type; 
	} 
	public void setType(Integer type) { 
		this.type = type; 
	} 
	 
} 
package com.zzg.email.config.entity; 
 
import org.springframework.boot.context.properties.ConfigurationProperties; 
import org.springframework.context.annotation.Configuration; 
import org.springframework.context.annotation.PropertySource; 
import org.springframework.stereotype.Component; 
 
@Component 
@Configuration 
@ConfigurationProperties(prefix = "com.zzg.email")  
@PropertySource("classpath:email.properties") 
public class EmailConfigEntity { 
	private String host; 
	private String username; 
	private String password; 
	private String auth; 
	private String enable; 
	private String required; 
	 
	// set 和 get 方法 
	public String getHost() { 
		return host; 
	} 
	public void setHost(String host) { 
		this.host = host; 
	} 
	public String getUsername() { 
		return username; 
	} 
	public void setUsername(String username) { 
		this.username = username; 
	} 
	public String getPassword() { 
		return password; 
	} 
	public void setPassword(String password) { 
		this.password = password; 
	} 
	public String getAuth() { 
		return auth; 
	} 
	public void setAuth(String auth) { 
		this.auth = auth; 
	} 
	public String getEnable() { 
		return enable; 
	} 
	public void setEnable(String enable) { 
		this.enable = enable; 
	} 
	public String getRequired() { 
		return required; 
	} 
	public void setRequired(String required) { 
		this.required = required; 
	} 
} 
package com.zzg.email.config; 
 
import java.util.Properties; 
 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.context.annotation.Bean; 
import org.springframework.context.annotation.Configuration; 
import org.springframework.mail.javamail.JavaMailSenderImpl; 
 
import com.zzg.email.config.entity.EmailConfigEntity; 
import com.zzg.email.util.EmailUtil; 
 
@Configuration 
public class EmailConfig { 
	@Autowired 
	private EmailConfigEntity entity; 
	 
	//获取JavaMailSenderImpl 对象 
	@Bean 
	public JavaMailSenderImpl getJavaMailSender(){ 
		JavaMailSenderImpl sender = new JavaMailSenderImpl(); 
		sender.setHost(entity.getHost().trim()); 
		sender.setUsername(entity.getUsername().trim()); 
		sender.setPassword(entity.getPassword().trim()); 
		 
		Properties properties = new Properties(); 
		properties.setProperty("mail.smtp.auth", entity.getAuth().trim()); 
		properties.setProperty("mail.smtp.starttls.enable", entity.getEnable().trim()); 
		properties.setProperty("mail.smtp.starttls.required", entity.getRequired().trim()); 
		sender.setJavaMailProperties(properties); 
		return sender; 
	} 
	 
	// 实列化 email 工具 
	@Bean 
	public EmailUtil getEmailUtil(){ 
		EmailUtil emailUtil = new EmailUtil(); 
		emailUtil.setSender(getJavaMailSender()); 
		return emailUtil; 
	} 
	 
	 
 
} 
package com.zzg.email.util; 
 
import javax.mail.internet.MimeMessage; 
 
import org.slf4j.Logger; 
import org.slf4j.LoggerFactory; 
import org.springframework.mail.SimpleMailMessage; 
import org.springframework.mail.javamail.JavaMailSenderImpl; 
import org.springframework.mail.javamail.MimeMessageHelper; 
import com.zzg.email.entity.EmailBean; 
 
public class EmailUtil{ 
	// 日志记录 
	private final static Logger logger = LoggerFactory.getLogger(EmailUtil.class); 
	 
	private JavaMailSenderImpl sender; 
 
	public JavaMailSenderImpl getSender() { 
		return sender; 
	} 
 
	public void setSender(JavaMailSenderImpl sender) { 
		this.sender = sender; 
	} 
	 
	public void send(EmailBean bean){ 
		try{ 
			switch(bean.getType()){ 
			case 1: 
				// 简单文本邮件 
				SimpleMailMessage text = new SimpleMailMessage(); 
				text.setFrom(bean.getFrom()); 
				text.setTo(bean.getTo()); 
				text.setSubject(bean.getSubject()); 
				// 简单文本核心代码 
				text.setText(bean.getText()); 
				 
				sender.send(text); 
				break; 
			case 2: 
				// HTML邮件 
				MimeMessage  html = sender.createMimeMessage(); 
				MimeMessageHelper helper = new MimeMessageHelper(html, true); 
				helper.setFrom(bean.getFrom()); 
				helper.setTo(bean.getTo()); 
				helper.setSubject(bean.getSubject()); 
				// HTML 核心代码 
				helper.setText(bean.getText(),true); 
				 
				sender.send(html); 
				break; 
			case 3: 
				// 附件邮件 
				MimeMessage attach = sender.createMimeMessage(); 
				MimeMessageHelper attachHelper = new MimeMessageHelper(attach, true); 
				attachHelper.setFrom(bean.getFrom()); 
				attachHelper.setTo(bean.getTo()); 
				attachHelper.setSubject(bean.getSubject()); 
				attachHelper.setText(bean.getText()); 
				// 附件核心代码 
				attachHelper.addAttachment(bean.getAttachmentFilename(), bean.getFile()); 
				 
				sender.send(attach); 
				break; 
			} 
		}catch (Exception e){ 
			logger.error(e.getMessage()); 
		} 
	} 
} 
package com.zzg.email.abstr; 
 
import com.zzg.email.util.EmailUtil; 
 
/** 
 *  
 * @ClassName:  EmailUtilAbstr    
 * @Description: 邮件发送功能拓展    
 * @author: **** -zzg 
 * @date:   2019年4月10日 上午10:12:31    
 *      
 * @Copyright: 2019 www.digipower.cn  
 * 注意:本内容仅限于****开发有限公司内部使用,禁止用于其他的商业目的 
 */ 
public abstract class EmailUtilAbstr { 
	 
	private EmailUtil util; 
	 
	public EmailUtil getUtil() { 
		return util; 
	} 
	 
	public void setUtil(EmailUtil util) { 
		this.util = util; 
	} 
 
	public EmailUtilAbstr(EmailUtil util){ 
		this.util = util; 
	} 
	public abstract void customSend(); 
} 

3、功能测试:

email.properties

com.zzg.email.host=smtp.163.com 
com.zzg.email.username=zhou****gang@163.com 
# 163邮箱密码 修改为授权码 
com.zzg.email.password=**** 
com.zzg.email.auth=true 
com.zzg.email.enable=true 
com.zzg.email.required=true
package com.zzg.controller; 
 
import java.io.File; 
 
import org.slf4j.Logger; 
import org.slf4j.LoggerFactory; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.core.io.FileSystemResource; 
import org.springframework.stereotype.Controller; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 
import org.springframework.web.bind.annotation.ResponseBody; 
 
import com.zzg.email.entity.EmailBean; 
import com.zzg.email.util.EmailUtil; 
 
@Controller 
@RequestMapping("/email") 
public class EmailController { 
	// 引入日志 
	private final static Logger logger = LoggerFactory.getLogger(MongodbControlller.class); 
	 
	@Autowired 
	private EmailUtil util; 
	 
	@RequestMapping(value="/sendText", method=RequestMethod.GET) 
	@ResponseBody 
	public void sendText(){ 
		// 简单文本邮件发送 
		EmailBean text = new EmailBean(); 
		text.setFrom("zhouzhiwengang@163.com"); 
		text.setTo("zhouzhigang@digipower.cn"); 
		text.setSubject("简单文本邮件发送"); 
		text.setText("简单文本邮件发送"); 
		text.setType(Integer.valueOf(1)); 
		util.send(text); 
		logger.error("简单文本邮件发送成功"); 
	} 
	 
	@RequestMapping(value="/sendHTML", method=RequestMethod.GET) 
	@ResponseBody 
	public void sendHTML(){ 
		// 简单文本邮件发送 
		EmailBean text = new EmailBean(); 
		text.setFrom("zhouzhiwengang@163.com"); 
		text.setTo("zhouzhigang@digipower.cn"); 
		text.setSubject("HTML邮件发送"); 
		StringBuffer sb = new StringBuffer(); 
        sb.append("<h1>HTML邮件发送</h1>") 
                .append("<p style='color:#F00'>红色字</p>") 
                .append("<p style='text-align:right'>右对齐</p>"); 
         
		text.setText(sb.toString()); 
		text.setType(Integer.valueOf(2)); 
		util.send(text); 
		logger.error("HTML邮件发送成功"); 
	} 
	 
	@RequestMapping(value="/sendAttach", method=RequestMethod.GET) 
	@ResponseBody 
	public void sendAttach(){ 
		// 简单文本邮件发送 
		EmailBean text = new EmailBean(); 
		text.setFrom("zhouzhiwengang@163.com"); 
		text.setTo("zhouzhigang@digipower.cn"); 
		text.setSubject("邮件附件发送"); 
		text.setText("邮件附件发送"); 
		FileSystemResource file = new FileSystemResource(new File("C:\\image\\1.png")); 
		text.setFile(file); 
		text.setAttachmentFilename("1.png"); 
		text.setType(Integer.valueOf(3)); 
		util.send(text); 
		logger.error("HTML邮件发送成功"); 
	} 
	 
} 

效果展示:

声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

发表评论
搜索
排行榜
KIKK导航

KIKK导航

关注我们