springboot發送郵件,以及攜帶郵件附件簡單使用


可以通過springboot官方文檔中Sending Email,找到類似如下java mail的使用文檔

https://docs.spring.io/spring/docs/5.1.9.RELEASE/spring-framework-reference/integration.html#mail

一、導入java mail相關starter

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

二、發送不帶附件和帶附件郵件

1、發送郵件邏輯,再上面的java mail文檔中,可以找到不帶附件的郵件和帶附件的郵件使用的發送郵件的bean不同,前者為JavaMailSender,后者為JavaMailSenderImpl,但通過查看集成關系可知,JavaMailSender的實現類其實只有JavaMailSenderImpl,所以,不管兩者使用的是哪個,最終發送郵件調用的都是JavaMailSenderImpl中的send方法。並且在JavaMailSenderImpl中添加了一些配置屬性,比如host,username等,使用JavaMailSender是獲取不到這些屬性的,故我們直接注入了JavaMailSenderImpl。通過測試得知,在springboot配置中,spring.mail.properties.開頭的配置都會被放入到JavaMailSenderImpl中的javaMailProperties屬性中,故有些額外的郵件發送參數,我們都可以通過spring.mail.properties.additional_attribute的方式來設置,比如設置郵件接收人:spring.mail.properties.receiver=xxx@163.com,xxxx@qq.com

package com.springbootdemo.mail;

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

import javax.annotation.Resource;
import javax.mail.internet.MimeMessage;

import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

@Component
public class MailSenderBusi {

    @Resource
    private JavaMailSenderImpl mailSender;

    public void sendMail() {
        Properties mailProperties = mailSender.getJavaMailProperties();
        SimpleMailMessage msg = new SimpleMailMessage();
        msg.setFrom(mailSender.getUsername());
        msg.setTo(mailProperties.getProperty("receiver").split(",")); // 可以是一個數組
        msg.setText("nice day");
        msg.setSubject("測試");

        this.mailSender.send(msg);
    }

    public void sendMailAttach() throws Exception {
        Properties mailProperties = mailSender.getJavaMailProperties();
        MimeMessage message = mailSender.createMimeMessage();

        MimeMessageHelper helper = new MimeMessageHelper(message, true);
        helper.setFrom(mailSender.getUsername());
        helper.setTo(mailProperties.getProperty("receiver").split(","));
       // 第二個參數為是否支持html,如果為true,則表示支持,此時如果寫入<h2>nice</h2>,則nice會被加粗,默認為false
helper.setText("nice day2", true); helper.setSubject("測試2");      // 或者使用new FiledataSource FileSystemResource file = new FileSystemResource(new File("F:\\learn\\workplace\\webtest.zip")); helper.addAttachment("webdemo.zip", file); mailSender.send(message); } }

2、觸發郵件發送

這里我們只是簡單做測試,故簡單實現了CommandLineRunner,在程序啟動后就開始發送郵件。

package com.springbootdemo;

import javax.annotation.Resource;

import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import com.springbootdemo.mail.MailSenderBusi;

@Component
public class InitMailSender implements CommandLineRunner{

    @Resource
    private MailSenderBusi mailSenderBusi;
    
    @Override
    public void run(String... args) throws Exception {
        
        mailSenderBusi.sendMail();
        mailSenderBusi.sendMailAttach();
    }
}

三、添加springboot配置

spring.mail.protocol=smtp
spring.mail.host=smtp.mxhichina.com
spring.mail.port=25
spring.mail.username=xxxxx@example.com
spring.mail.password=password

spring.mail.properties.receiver=xxxx@163.com,xxxxx@qq.com
spring.mail.properties.mail.smtp.auth=true

spring.mail.default-encoding=UTF-8

可能對於有些情況,需要使用到如下兩個參數,但在本例中是不需要的

spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true

通過以上簡單三步,就可正常發送郵件了,具體深入的其他參數設置,后續再研究

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM