SpringBoot发送邮件总结

引入依赖

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

配置文件 

spring.mail.host=smtp.126.com
spring.mail.username=126邮箱账号
spring.mail.password=126客户端授权密码
spring.mail.default-encoding=UTF-8

文本邮件

文本邮件服务类

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

@Service
public class MailService {
	
        //谁发的
	@Value("${spring.mail.username}")
	private String from;
	
	@Autowired
	private JavaMailSender mailSender;

        /**
         * 
         * @param to  发给谁
         * @param subject  邮件名称
         * @param content  邮件内容
         */
	public void sendSimpleMail(String to, String subject, String content) {

		SimpleMailMessage message=new SimpleMailMessage();
		message.setFrom(from);
                message.setTo(to);
		message.setSubject(subject);
		message.setText(content);
		
		mailSender.send(message);
	}
}

文本邮件测试类

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.travelsky.email.service.MailService;

@RunWith(SpringRunner.class)
@SpringBootTest
public class EmailApplicationTests {
	
	@Autowired
	private MailService mailService;

	@Test
	public void sendSimpleMail() {
		mailService.sendSimpleMail("这填写接收邮件的帐号", "这是第一封邮件", "大家好,这是我的第一封邮件");
	}

}

HTML邮件

邮件服务类添加发送HTML邮件的方法即可

public void sendHTMLMail(String to, String subject, String content) throws MessagingException {
		MimeMessage message =mailSender.createMimeMessage();
		
		MimeMessageHelper helper =new MimeMessageHelper(message,true);
		helper.setFrom(from);
		helper.setTo(to);
		helper.setSubject(subject);
		helper.setText(content,true);
			
		mailSender.send(message);	
}

HTML邮件测试类

添加一个测试方法

@Test
public void sendHTMLMail() throws MessagingException{
	String content="<html><body><h1 style='color:red'>红色的字</h1><hr/></body></html>";
	mailService.sendHTMLMail("这填写接收邮件的帐号", "这是第一封HTML邮件", content);
}

带附件邮件

邮件服务类添加发送带附件邮件的方法即可

public void sendAttachmentMail(String to, String subject, String content,
			String filePath) throws MessagingException {
		MimeMessage message = mailSender.createMimeMessage();

		MimeMessageHelper helper = new MimeMessageHelper(message, true);
		helper.setFrom(from);
		helper.setTo(to);
		helper.setSubject(subject);
		helper.setText(content, true);
		
		FileSystemResource file=new FileSystemResource(new File(filePath));
		String fileName=file.getFilename();
		helper.addAttachment(fileName, file);
		
		mailSender.send(message);
}

带附件邮件测试方法

@Test
public void sendAttachmentMail() throws MessagingException{
	String filePath="D:/WorkSpace/email.zip";
	mailService.sendAttachmentMail("这里填写接受邮件的帐号", "这是第一封带附件的邮件", "这是第一封带附件的邮件", filePath);
}

带图片邮件

public void sendInlineResourceMail(String to, String subject,
			String content, String rscPath, String rscId)
			throws MessagingException {
		MimeMessage message = mailSender.createMimeMessage();

		MimeMessageHelper helper = new MimeMessageHelper(message, true);
		helper.setFrom(from);
		helper.setTo(to);
		helper.setSubject(subject);
		helper.setText(content, true);

		FileSystemResource file = new FileSystemResource(new File(rscPath));
		helper.addInline(rscId, file);

		mailSender.send(message);
}

带图片邮件测试方法

@Test
public void sendInlineResourceMail() throws MessagingException{
	String rscPath="D:/WorkSpace/test.png";
	String rscId="jc001";
	String content="<html><body><h1 style='color:red'>有图片的邮件</h1><img src=\'cid:"+rscId+"\'></img></body></html>";
	mailService.sendInlineResourceMail("这里填写接受邮件的账号", "这是第一封带图片的邮件", content, rscPath, rscId);
}

模版邮件


@Resource 
private TemplateEngine templateEngine;
	

@Test
public void sendTemplateMail() throws MessagingException{
	Context context=new Context();
        //设置模版中的传参
	context.setVariable("id", "001");
	//emailContent是模版名称,这里用的模版是thymeleaf
	String emailContent=templateEngine.process("emailTemplate", context);
	mailService.sendHTMLMail("这里填写接收邮件的帐号", "这是一个模版邮件", emailContent);
}

异常处理----异常最好不要向上抛出,在自己的方法中处理掉.记录日志.

猜你喜欢

转载自blog.csdn.net/weixin_40196043/article/details/82774134