springboot發送郵件的一些坑與代碼示例

最近玩了一個郵件發送的一個功能,原來一直想玩,一直沒有機會,這次玩了一下,遇到了不少坑,接下來說說遇到的坑吧

1、我是部署在阿里雲服務器上面的,阿里雲把郵件發送的默認端口25封了,需要改成SSL加密端口

2、發送HTML郵件的情況,需要避免使用浮動,定位的方式進行頁面佈局,不要使用CSS外聯及JS,建議多次進行兼容性調試,因爲郵件發送HTML會過濾部分屬性,導致HTML內容排版錯誤

3、另外關於第二個問題,我看了一下釘郵的發送是好的,查看了發送的源碼發現可能可以用編碼格式解決排版問題

4、另外部署後發現,發送郵件賊慢,雖然可能沒幾個人用,但以防出現問題,加個@Async異步處理一下

下面倆張圖是我發送的源碼與釘郵的源碼,發現釘郵把默認的quoted-printable編碼格式轉換成了base64編碼格式,並把text/html改成了text/plain,這個問題沒有解決,如有大佬看到,望告知,感謝

下面代碼發送的HTML的 純本文的更簡單點

pom文件添加依賴

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.mail.*;
import javax.mail.internet.*;
import java.util.Properties;

/**
 * @Description 發送HTML郵件
 * @Author Zhong Yanghao
 * @Date 2020/06/01 10:53:45
 * @Param to        郵件接收方
 * @param subject   郵件主題
 * @param text      郵件內容
 * @Return void
 */
@Async
public void sendTextMail(String to, String subject, String text) throws Exception {

    // 配置發送郵件的環境屬性
    final Properties props = new Properties();
    // 表示SMTP發送郵件,需要進行身份驗證
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.host", host);
    // 如果使用ssl,則去掉使用25端口的配置,進行如下配置,
    props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
    props.put("mail.smtp.socketFactory.port", "465");
    props.put("mail.smtp.port", "465");
    // 發件人的賬號,填寫控制檯配置的發信地址,比如[email protected]
    props.put("mail.user", username);
    // 訪問SMTP服務時需要提供的密碼(在控制檯選擇發信地址進行設置)
    props.put("mail.password", password);

    // 構建授權信息,用於進行SMTP進行身份驗證
    Authenticator authenticator = new Authenticator() {
        @Override
        protected PasswordAuthentication getPasswordAuthentication() {
        // 用戶名、密碼
        String userName = props.getProperty("mail.user");
        String password = props.getProperty("mail.password");
        return new PasswordAuthentication(userName, password);
        }
    };

    // 使用環境屬性和授權信息,創建郵件會話
    Session mailSession = Session.getInstance(props, authenticator);
    // 創建郵件消息
    MimeMessage message = new MimeMessage(mailSession){};

    try {
        // 設置發件人郵件地址和名稱。填寫控制檯配置的發信地址,比如[email protected]。和上面的mail.user保持一致。名稱用戶可以自定義填寫。
        InternetAddress from = new InternetAddress(props.getProperty("mail.user"), "LDSJ");
        message.setFrom(from);
        // 設置收件人郵件地址,比如[email protected]
        InternetAddress toAddress = new InternetAddress(to);
        message.setRecipient(MimeMessage.RecipientType.TO, toAddress);
        // 設置郵件標題
        message.setSubject(subject);
        // 設置郵件的內容體
        message.setContent(text, "text/html;charset=UTF-8");
        // 發送郵件
        Transport.send(message);
        logger.info(new StringBuilder().append("發送至").append(to).append("成功!").toString());
    }
    catch (MessagingException e) {
        logger.error("郵件發送失敗" + e.getMessage());
        throw new Exception("郵件發送失敗!");
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章