SpringBoot: springboot集成JMS

环境依赖:

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-mail</artifactId>
		</dependency>
	</dependencies>

 

配置:

spring.mail.host=xxx
spring.mail.username=xxx
spring.mail.password=xxx
spring.mail.port=25 
spring.mail.protocol=smtp 
spring.mail.default-encoding=UTF-8

发邮件test类:

若要做为工具类可以使用MailBean进行封装

package com.forezp;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;

import javax.mail.internet.MimeMessage;
import java.io.File;

@Service  
public class MailSenderService {  

	@Autowired
	private JavaMailSenderImpl mailSender;

	/**
	 * 发送文本的邮件
	 */
	public void sendTxtMail() {
		SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
		// 设置收件人,寄件人
		simpleMailMessage.setTo(new String[] {"xxx@sina.com"});
		simpleMailMessage.setFrom("xxx@sina.com");
		simpleMailMessage.setSubject("Spring Boot Mail邮件测试");
		simpleMailMessage.setText("test文本。");
		// 发送邮件
		mailSender.send(simpleMailMessage);
	}

	/**
	 * 发送包含HTML文本的邮件
	 * @throws Exception
	 */
	public void sendHtmlMail() throws Exception {
		MimeMessage mimeMessage = mailSender.createMimeMessage();
		MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage);
		mimeMessageHelper.setTo("xxx@sina.com");
		mimeMessageHelper.setFrom("xxx@sina.com");
		mimeMessageHelper.setSubject("Spring Boot Mail邮件测试");

		StringBuilder sb = new StringBuilder();
		sb.append("<html><head></head>");
		sb.append("<body><h1>spring 邮件测试</h1></body>");
		sb.append("</html>");

		// 启用html
		mimeMessageHelper.setText(sb.toString(), true);
		// 发送邮件
		mailSender.send(mimeMessage);

	}

	/**
	 * 发送包含图片的邮件
	 * @throws Exception
	 */
	public void sendAttachedImageMail() throws Exception {
		MimeMessage mimeMessage = mailSender.createMimeMessage();
		// multipart模式
		MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);
		mimeMessageHelper.setTo("xxx@sina.com");
		mimeMessageHelper.setFrom("xxx@sina.com");
		mimeMessageHelper.setSubject("Spring Boot Mail邮件测试");

		StringBuilder sb = new StringBuilder();
		sb.append("<html><head></head>");
		sb.append("<body><h1>spring 邮件测试</h1>");
		// cid为固定写法,imageid指定一个唯一标识
		sb.append("<img src=\"cid:imageid\"/></body>");
		sb.append("</html>");

		// 启用html
		mimeMessageHelper.setText(sb.toString(), true);

		// 设置imageId
		FileSystemResource img = new FileSystemResource(new File("D:/xxx.jpg"));
		mimeMessageHelper.addInline("imageid", img);

		// 发送邮件
		mailSender.send(mimeMessage);
	}

	/**
	 * 发送包含附件的邮件
	 * @throws Exception
	 */
	public void sendAttendedFileMail() throws Exception {
		MimeMessage mimeMessage = mailSender.createMimeMessage();
		// multipart模式
		MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true, "utf-8");
		mimeMessageHelper.setTo("xxx@sina.com");
		mimeMessageHelper.setFrom("xxx@sina.com");
		mimeMessageHelper.setSubject("Spring Boot Mail 邮件测试");

		StringBuilder sb = new StringBuilder();
		sb.append("<html><head></head>");
		sb.append("<body><h1>spring 邮件测试</h1></body>");
		sb.append("</html>");

		// 使用html
		mimeMessageHelper.setText(sb.toString(), true);
		// 载入附件
		FileSystemResource img = new FileSystemResource(new File("D:/xxx.jpg"));
		mimeMessageHelper.addAttachment("image.jpg", img);

		// 发送邮件
		mailSender.send(mimeMessage);
	}
}

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值