Springboot项目实现各种邮件发送

有时在开发中我们可能会需要对用户邮箱进行验证,这个时候就需要发送邮箱验证了,这里使用
springboot实现邮件发送。
1.先在Spring Boot的工程中的pom.xml中引入spring-boot-starter-mail依赖:

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

2.在完成依赖引入后,可在application.yml中添加配置属性内容。

spring:
  mail:
    host: smtp.qq.com
    username: xxxxxx
    password: xxxxxxx
    port: 25
    protocol: smtp
    default-encoding: UTF-8

3.接下来就可以创建一个工具类来实现发送各种邮件。

import java.io.File;
import java.util.Date;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Message.RecipientType;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import lombok.extern.log4j.Log4j2;

@Component
@Log4j2
public class MailSendUtil {
	@Autowired
	JavaMailSender javaMailSenderImpl;
	/**
	 * 发送简单html文本的邮箱验证码
	 * @param to
	 * @param code
	 */
	public boolean sendSimpleHtmlMail(String to,String code) {
		MimeMessage message=javaMailSenderImpl.createMimeMessage();
		try {
			//设置发送人
			message.setFrom(new InternetAddress(username));
			//设置收件人
			message.setRecipient(RecipientType.TO, new InternetAddress(to));
			 // 设置邮件标题
            message.setSubject("邮箱验证码");
            
			message.setContent("您正在对相关操作进行验证,非自我操作请忽略,验证码为:" + code + " 验证码十分钟内有效,请及时处理!","text/html;charset=UTF-8");
			message.setSentDate(new Date());
			javaMailSenderImpl.send(message);
		} catch (AddressException e) {
			log.error(e.getMessage());
			return false;
		} catch (MessagingException e) {
			log.error(e.getMessage());
			return false;
		}
		return true;
	}
	/**
	 * 发送带图片的邮件
	 * @param to
	 * @param subject
	 * @param content
	 * @param picPath
	 * @param picId
	 * @return
	 */
	public boolean sendPicMail(String to,String subject,String content,String picPath,String picId){
		MimeMessage message=javaMailSenderImpl.createMimeMessage();
		MimeMessageHelper helper=null;
		try {
			helper=new MimeMessageHelper(message, true);
			//设置发送人
			message.setFrom(new InternetAddress(username));
			//设置收件人
			message.setRecipient(RecipientType.TO, new InternetAddress(to));
			 // 设置邮件标题
            message.setSubject(subject);
            helper.setText(content, true);
            helper.addInline(picId, new FileSystemResource(new File(picPath)));
           // helper.setTo(new InternetAddress(to));
            message.setSentDate(new Date());
			javaMailSenderImpl.send(message);
		} catch (AddressException e) {
			log.error(e.getMessage());
			return false;
		} catch (MessagingException e) {
			log.error(e.getMessage());
			return false;
		}
		return true;
	}
	/**
	 * 发送带附件的邮件
	 * @param to
	 * @param subject
	 * @param content
	 * @param filePath
	 * @return
	 */
	public boolean sendEnclosureMail(String to,String subject,String content,String filePath){
		MimeMessage message=javaMailSenderImpl.createMimeMessage();
		MimeMessageHelper helper=null;
		try {
			helper=new MimeMessageHelper(message, true);
			//设置发送人
			message.setFrom(new InternetAddress(username));
			//设置收件人
			message.setRecipient(RecipientType.TO, new InternetAddress(to));
			 // 设置邮件标题
            message.setSubject(subject);
                       
            FileSystemResource file = new FileSystemResource(new File(filePath));          
            helper.addAttachment(file.getFilename(), file);
            message.setSentDate(new Date());
            helper.setText(content, true);
			javaMailSenderImpl.send(message);
		} catch (AddressException e) {
			log.error(e.getMessage());
			return false;
		} catch (MessagingException e) {
			log.error(e.getMessage());
			return false;
		}
		return true;
	}
	/**
	 * 发送带附件和图片的邮件
	 * @param to
	 * @param subject
	 * @param content
	 * @param filePath
	 * @return
	 */
	public boolean sendPicEnclosureMail(String to,String subject,String content,String filePath,String picPath,String picId){
		MimeMessage message=javaMailSenderImpl.createMimeMessage();
		MimeMessageHelper helper=null;
		try {
			helper=new MimeMessageHelper(message, true);
			//设置发送人
			message.setFrom(new InternetAddress(username));
			//设置收件人
			message.setRecipient(RecipientType.TO, new InternetAddress(to));
			 // 设置邮件标题
            message.setSubject(subject);
            
            FileSystemResource file = new FileSystemResource(new File(filePath));
            //添加图片
            helper.addInline(picId, new FileSystemResource(new File(picPath)));																																
            //添加附件
            helper.addAttachment(file.getFilename(), file);
            message.setSentDate(new Date());
            helper.setText(content, true);
			javaMailSenderImpl.send(message);
		} catch (AddressException e) {
			log.error(e.getMessage());
			return false;
		} catch (MessagingException e) {
			log.error(e.getMessage());
			return false;
		}
		return true;
	}
}

springboot中使用JavaMailSender发送邮件可以说非常方便了。
4.最后可以通过简单的单元测试来发送邮件了。

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import com.suntree.utils.MailSendUtil;

@RunWith(SpringRunner.class)
@SpringBootTest
public class DemoApplicationTests {
	@Autowired
	MailSendUtil mailSendUtil;
	@Test
	public void sendMail(){
		mailSendUtil.sendSimpleHtmlMail("153xxxxx@qq.com", "123456");
	}
}

有兴趣的可以试一试。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

夜空下的星

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值