基于springboot发邮件

本文详细介绍了如何在Spring Boot中配置和使用邮件发送功能,包括简单文本邮件、HTML邮件、带附件的邮件及包含图片的HTML邮件。同时,还展示了如何集成Thymeleaf模板引擎来发送模板邮件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.添加pom对邮件的支持

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

2.配置邮件参数

spring.mail.host=smtp.qq.com
spring.mail.username=227746885@qq.com
spring.mail.password=。。。。 //授权码
spring.mail.default-encoding=utf-8

3.发送简单文本邮件


	@Value("${spring.mail.username}")
	private String from; //发送人
	
	@Autowired
	private JavaMailSender mailSender;
	
	public void sendSimpleMail(String to,String subject,String content) {
	    SimpleMailMessage simpleMailMessage = new	SimpleMailMessage();
	    simpleMailMessage.setTo(to);//接收人
	    simpleMailMessage.setSubject(subject); //邮件主题
	    simpleMailMessage.setText(content);//邮件内容
	    simpleMailMessage.setFrom(from);//发送人
	    mailSender.send(simpleMailMessage);
	}

4.发送带html的邮件

public void sendHtmlMain(String to,String subject,String content) throws Exception {
		  MimeMessage mssage = mailSender.createMimeMessage();
		  MimeMessageHelper helper = new MimeMessageHelper(mssage,true);
		  helper.setTo(to);
		  helper.setSubject(subject);
		  helper.setText(content,true); //true为使用html
		  helper.setFrom(from);
		  mailSender.send(mssage);
	}


	@Test
	public void testHtmlMail() throws Exception {
		String to="813457063@qq.com";
	   String subject="html邮件";
	   String content="<html>\n"+
	   "<body>\n"+
			   "<h3>hello 你好 html 邮件</h3>\n"+
	   "</body>\n"+
		"</html>";
			  
		mailService.sendHtmlMain(to, subject, content);
	}
	

5.发送带有附件的邮件

 

	
	public void sendAttachmentsMail(String to,String subject,String content,String filePath) throws Exception {
		MimeMessage message = mailSender.createMimeMessage();
		MimeMessageHelper helper = new MimeMessageHelper(message,true);
		helper.setTo(to);
		helper.setSubject(subject);
		helper.setText(content,true);
		helper.setFrom(from);

       //filepath 为附件的路径
		FileSystemResource fileSystemResource = new FileSystemResource(new File(filePath));
		String fileName=fileSystemResource.getFilename();
		helper.addAttachment(fileName, fileSystemResource);
		mailSender.send(message);
	}


	
	//带附件的
	@Test
	public void testAttachmentsMail() throws Exception {
		String to="813457063@qq.com";
	   String subject="附件的邮件";
	   String content="<html>\n"+
			   "<body>\n"+
					   "<h3>hello 你好 附件 邮件</h3>\n"+
			   "</body>\n"+
				"</html>";
	   String filePath="F:\\99.GIF";
			  
		mailService.sendAttachmentsMail(to, subject, content,filePath);
	}

5.发送html里面有图片的邮件

public void sendInLinResourceMail(String to,String subject,String content,String rscPath,String rscId) throws Exception {
		MimeMessage message = mailSender.createMimeMessage();
		MimeMessageHelper helper = new MimeMessageHelper(message,true);
		helper.setTo(to);
		helper.setSubject(subject);
		helper.setText(content,true);
		helper.setFrom(from);
		
		FileSystemResource fileSystemResource = new FileSystemResource(new File(rscPath));
		helper.addInline(rscId, fileSystemResource);
		mailSender.send(message);
	}




	//图片在html中的
	@Test
	public void testInLinResourceMail() throws Exception {
		String to="813457063@qq.com";
	   String subject="图片在html中的邮件";
	   String rscId="wq001";
	   String filePath="F:\\99.GIF";
	   String content="<html>\n"+
			   "<body>\n"+
					   "<h3>hello 图片在html中的 邮件</h3>\n"+
			      "<img src='cid:"+rscId+"></img>"+
			   "</body>\n"+
				"</html>";
			  
		mailService.sendInLinResourceMail(to, subject, content,filePath,rscId);
	}

6. 和thymeleaf集成模板邮件

添加thymeleaf支持

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

templates文件夹下面新建 emailTemplate.html文件

 

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>邮件模板</title>
</head>
<body>
这是一份注册邮箱感谢支持,,。。。。。。。测试。。。。 点击下面的链接
<a href="#" th:href="@{http://www.baidu.com/{id}(id=${id})}">激活账号 </a>
</body>
</html>

test方法


	@Resource
	 TemplateEngine templateEngine;


	//模板邮件
	@Test
	public void testTmeplateMail() throws Exception {
		Context context = new Context();
		context.setVariable("id", "008");
		String emailContent=templateEngine.process("emailTemplate", context);
		
		String to="813457063@qq.com";
		   String subject="模板中的邮件";

		mailService.sendHtmlMain(to, subject, emailContent);;
		
		
	
	}

 

关于异常捕获:

private final Logger logger=(Logger) LoggerFactory.getLogger(this.getClass());

public void sendInLinResourceMail(String to,String subject,String content,String rscPath,String rscId)  {
		logger.info("开始发送邮件。。。。{},{},{},{},{}",to,subject,content,rscPath,rscId);
		MimeMessage message = mailSender.createMimeMessage();
		MimeMessageHelper helper;
		try {
			helper = new MimeMessageHelper(message,true);
			helper.setTo(to);
			helper.setSubject(subject);
			helper.setText(content,true);
			helper.setFrom(from);
			
			FileSystemResource fileSystemResource = new FileSystemResource(new File(rscPath));
			helper.addInline(rscId, fileSystemResource);
			mailSender.send(message);
			logger.info("发送邮件成功。。。。。。。。。");
		} catch (MessagingException e) {
			logger.error("发送邮件失败。。。。。。。。。");
		}
	
	}

 

邮件发送到此结束

 

以后可以考虑 做微服务,或者放队列 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值