SpringBoot发送邮件

相关文章:
commons-email邮件工具类的基本使用
JavaMail-使用Java发送邮件


使用java发送邮件很简单,直接上代码

添加依赖:


<dependency>
    <groupId>org.springframework.bootgroupId>
    <artifactId>spring-boot-starter-mailartifactId>
dependency>

添加配置:

### SpringMail
spring.mail.host=smtp.163.com
[email protected]
spring.mail.password=xxx
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true
spring.mail.protocol=smtp
spring.mail.default-encoding=UTF-8

代码:

package com.example.demo.mail.boot;

import lombok.extern.slf4j.Slf4j;
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.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.test.context.junit4.SpringRunner;

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

/**
 * @author zzs
 * @date 2019/6/1 15:39
 */
@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest
public class TestSpringMail {

	//注入该对象
    @Autowired
    private JavaMailSender mailSender;

    /**
     * 一、发送简单文本邮件
     */
    @Test
    public void testSimpleMail() {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom("[email protected]");
        message.setTo("[email protected]");
        message.setSubject("主题:简单邮件");
        message.setText("测试邮件内容");

        this.mailSender.send(message);
    }

    /**
     * 二、发送html格式的邮件
     */
    @Test
    public void testHtmlMail() throws MessagingException {
        String content = "\n" +
                "\n" +
                "    

hello world ! 这是一封Html邮件!

\n"
+ "\n" + ""; MimeMessage message = mailSender.createMimeMessage(); //true表示需要创建一个multipart message MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setFrom("[email protected]"); helper.setTo("[email protected]"); helper.setSubject("html mail"); helper.setText(content, true); mailSender.send(message); } /** * 三、发送附件邮件 */ @Test public void testAttachmentMail() throws MessagingException { String filePath = "E:\\tmp\\img\\111.jpg"; MimeMessage message = mailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setFrom("[email protected]"); helper.setTo("[email protected]"); helper.setSubject("附件邮件"); helper.setText("这是一封带附件的邮件", true); FileSystemResource file = new FileSystemResource(new File(filePath)); String fileName = filePath.substring(filePath.lastIndexOf(File.separator)); helper.addAttachment(fileName, file); mailSender.send(message); log.info("带附件的邮件已经发送。"); } /** * 四、发送带图片的邮件 */ @Test public void testImgMail() throws MessagingException { String Id = "img123"; String content = "这是有图片的邮件: + Id + "\' >"; String imgPath = "E:\\tmp\\img\\111.jpg"; MimeMessage message = mailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setFrom("[email protected]"); helper.setTo("[email protected]"); helper.setSubject("这是有图片的邮件"); helper.setText(content, true); FileSystemResource res = new FileSystemResource(new File(imgPath)); helper.addInline(Id, res); mailSender.send(message); log.info("嵌入静态资源的邮件已经发送。"); } /** * 五、发送带图片以及附件的复杂邮件 */ @Test public void testImgAndAttachmentMail() throws MessagingException { String Id = "img123"; String content = "这是有图片的邮件: + Id + "\' >"; String imgPath = "E:\\tmp\\img\\111.jpg"; MimeMessage message = mailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setFrom("[email protected]"); helper.setTo("[email protected]"); helper.setSubject("这是有图片以及附件的邮件"); helper.setText(content, true); FileSystemResource res = new FileSystemResource(new File(imgPath)); helper.addInline(Id, res); String fileName = imgPath.substring(imgPath.lastIndexOf(File.separator)); helper.addAttachment(fileName, res); helper.addAttachment(fileName, res); helper.addAttachment(fileName, res); mailSender.send(message); log.info("嵌入静态资源并携带附件的邮件已经发送。"); } }

你可能感兴趣的:(mail)