Springboot发送邮件

  • 邮件依赖包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
  • 配置邮件:application.yml
#邮件配置
spring
    mail:
      host: smtp.163.com
      username: tong17318879979@163.com #邮箱账号
      password: tong915222 #发送者的邮箱授权码
      default-encoding: UTF-8

1.发送文本邮件

@Service
@Slf4j
public class MailService {

    @Autowired
    private JavaMailSender mailSender;

    @Value("${spring.mail.username}")
    private String from;

    /**
     * 发送文本邮件
     * @param to 发送给谁
     * @param subject 主题
     * @param content 内容
     */
    public void sendSimpleMail(String to,String subject,String content){
        SimpleMailMessage mailMessage = new SimpleMailMessage();
        mailMessage.setTo(to);
        mailMessage.setSubject(subject);
        mailMessage.setText(content);
        mailMessage.setFrom(from);//从谁发送
        mailSender.send(mailMessage);
    }
}
@Test
public void sendSimpleMail() {
    mailService.sendSimpleMail("tong17318879979@163.com","这是第一封邮件","邮件内容test");
}

2.发送HTML邮件

/**
 * 发送HTML邮件
 * @param to 发送给谁
 * @param subject 主题
 * @param content 内容
 * @throws MessagingException
 */
public void sengHtmlMail(String to,String subject,String content) throws MessagingException {
    MimeMessage mimeMessage = mailSender.createMimeMessage();
    MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);
    helper.setTo(to);
    helper.setSubject(subject);
    helper.setText(content,true);
    helper.setFrom(from);
    mailSender.send(mimeMessage);
}
@Test
public void sendHtmlEmail()  throws MessagingException {
    String content = "<html>\n"+
            "<body>\n"+
            "<h3>hello world,这是一封HTML邮件!</h3>\n"+
            "</body>\n"+
            "</html>";
    mailService.sengHtmlMail("tong17318879979@163.com","这是第二封邮件",content);
}

3.发送带附近的邮件

/**
 * 发送带附件邮件
 * @param to 发送给谁
 * @param subject 主题
 * @param content 内容
 * @param filePath   附件的地址
 * @throws MessagingException
 */
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="E:\\testMial.txt";
    mailService.sendAttachmentMail("tong17318879979@163.com","这是一封带附件的邮件","这是一封带附件的邮件",filePath);
}

4.发送图片邮件

/**
 * 发送图片邮件
 * @param to 发送给谁
 * @param subject 主题
 * @param content 内容
 * @param rscPath   路径
 * @param rscId
 * @throws MessagingException
 */
public void sendInlinResourceMail(String to,String subject,String content,String rscPath,String rscId) {
    log.info("发送静态邮件,开始:{},{},{},{},{}",to,subject,content,rscPath,rscId);
    MimeMessage message = mailSender.createMimeMessage();
    MimeMessageHelper helper = null;
    try {
        helper = new MimeMessageHelper(message,true);
        helper.setFrom(from);
        helper.setTo(to);
        helper.setSubject(subject);
        helper.setText(content,true);
        FileSystemResource res = new FileSystemResource(new File(rscPath));
        helper.addInline(rscId,res);
        mailSender.send(message);
        log.info("发送静态邮件成功");
    } catch (MessagingException e) {
        log.info("发送静态邮件失败:",e);
        e.printStackTrace();
    }
}
@Test
public void sendInlinResourceMail()  throws MessagingException{
    String imgPath="E:\\1.jpg";
    String rscId ="img001";
    String content = "<html><body> 这是有图片的邮件:<img src=\'cid:"+rscId
            + "\'/></body></html>";
    mailService.sendInlinResourceMail("tong17318879979@163.com","这是一封带图片的邮件",content,imgPath,rscId);
}

5.发送模板邮件

<!--thymeleaf-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!DOCTYPE html>
<html lang="en" xmlns:th = "http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>邮件模板</title>
</head>
<body>
您好,感谢您的注册,这是一封验证邮件,请点击下面的链接完成注册,感谢您的支持!</br>
<a href = "#" th:href="@{http:///www.baidu.com/register/{id}(id=${id})}">激活账号:</a>
</body>
</html>
@Autowired
private TemplateEngine templateEngine;

@Test
public void testTemplateMail() throws MessagingException{
    Context context = new Context();
    context.setVariable("id","006");
    String emailContent = templateEngine.process("emilTemplate",context); //前面那个对应的就是模板页面
    mailService.sengHtmlMail("tong17318879979@163.com","这是一个模板邮件",emailContent);

}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值