SpringBoot傳送郵件
這是博主自己本地記在有道雲的,現在都發出來,希望對你們有幫助哈。
話不多說,直接上程式碼~
一、依賴
<!-- javax.mail -->
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>1.6.0</version>
</dependency>
二、郵箱配置
mail.smtp.auth=true
mail.transport.protocol=smtp
mail.send.charset=UTF-8
mail.smtp.port=465
mail.is.ssl=true
mail.host=smtp.163.com
mail.auth.name=xx@163.com
mail.auth.password=
mail.smtp.timeout=5000
三、程式碼
工具類 EmailUtils
package cn.aduu.utils;
import com.sun.mail.util.MailSSLSocketFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.security.GeneralSecurityException;
import java.util.List;
import java.util.Map;
import java.util.Properties;
/**
* @author zh
* @ClassName cn.aduu.utils.EmailUtils
* @Description
*/
@Component
public class EmailUtils {
private static final Logger logger = LoggerFactory.getLogger(EmailUtils.class);
@Autowired
private Environment env;
private static String auth;
private static String host;
private static String protocol;
private static int port;
private static String authName;
private static String password;
private static boolean isSSL;
private static String charset ;
private static String timeout;
@PostConstruct
public void initParam () {
auth = env.getProperty("mail.smtp.auth");
host = env.getProperty("mail.host");
protocol = env.getProperty("mail.transport.protocol");
port = env.getProperty("mail.smtp.port", Integer.class);
authName = env.getProperty("mail.auth.name");
password = env.getProperty("mail.auth.password");
charset = env.getProperty("mail.send.charset");
isSSL = env.getProperty("mail.is.ssl", Boolean.class);
timeout = env.getProperty("mail.smtp.timeout");
}
/**
* 傳送郵件
* @param subject 主題
* @param toUsers 收件人
* @param ccUsers 抄送
* @param content 郵件內容
* @param attachfiles 附件列表 List<Map<String, String>> key: name && file
*/
public static boolean sendEmail(String subject, String[] toUsers, String[] ccUsers, String content, List<Map<String, String>> attachfiles) {
boolean flag = true;
try {
JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
javaMailSender.setHost(host);
javaMailSender.setUsername(authName);
javaMailSender.setPassword(password);
javaMailSender.setDefaultEncoding(charset);
javaMailSender.setProtocol(protocol);
javaMailSender.setPort(port);
Properties properties = new Properties();
properties.setProperty("mail.smtp.auth", auth);
properties.setProperty("mail.smtp.timeout", timeout);
if(isSSL){
MailSSLSocketFactory sf = null;
try {
sf = new MailSSLSocketFactory();
sf.setTrustAllHosts(true);
properties.put("mail.smtp.ssl.enable", "true");
properties.put("mail.smtp.ssl.socketFactory", sf);
} catch (GeneralSecurityException e) {
e.printStackTrace();
}
}
javaMailSender.setJavaMailProperties(properties);
MimeMessage mailMessage = javaMailSender.createMimeMessage();
MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage, true);
messageHelper.setTo(toUsers);
if (ccUsers != null && ccUsers.length > 0) {
messageHelper.setCc(ccUsers);
}
messageHelper.setFrom(authName);
messageHelper.setSubject(subject);
messageHelper.setText(content, true);
if (attachfiles != null && attachfiles.size() > 0) {
for (Map<String, String> attachfile : attachfiles) {
String attachfileName = attachfile.get("name");
File file = new File(attachfile.get("file"));
messageHelper.addAttachment(attachfileName, file);
}
}
javaMailSender.send(mailMessage);
} catch (Exception e) {
logger.error("傳送郵件失敗!", e);
flag = false;
}
return flag;
}
}
四、測試
package cn.aduu.web;
import cn.aduu.utils.EmailUtils;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author zh
* @ClassName cn.aduu.web.EmailController
* @Description
*/
@RestController
public class EmailController {
private static final Logger logger = LoggerFactory.getLogger(EmailController.class);
@RequestMapping("sendEmail")
public String sendEmail() throws JsonProcessingException {
boolean isSend = EmailUtils.sendEmail("這是一封測試郵件", new String[]{"3379218@qq.com"}, null, "<h3><a href='http://www.baidu.com'>百度一下,你就知道</a></h3>", null);
return "傳送郵件:" + isSend;
}
}
PS:如果覺得我的分享不錯,歡迎大家隨手點贊、轉發。
(完)
推薦閱讀
1. 突破這個四個階段年薪沒有50W,還好意思說是搞Java的?
2. GitHub上獲得26236個Star的Java面試專案,你想要麼?
Java團長
專注於Java乾貨分享
掃描上方二維碼獲取更多Java乾貨
相關文章
- SpringBoot整合Mail傳送郵件&傳送模板郵件Spring BootAI
- SpringBoot郵件傳送Spring Boot
- SpringBoot傳送郵件(二)Spring Boot
- SpringBoot傳送郵件(三)Spring Boot
- SpringBoot整合郵件傳送Spring Boot
- Springboot 自動傳送郵件Spring Boot
- springboot如何使用outlook傳送郵件Spring Boot
- SpringBoot實現傳送電子郵件Spring Boot
- Springboot+Javamail實現郵件傳送Spring BootJavaAI
- 郵件傳送
- 傳送郵件
- springboot新增郵件傳送及壓縮功能Spring Boot
- SpringBoot傳送電子郵件(附原始碼)Spring Boot原始碼
- springboot+kafka+郵件傳送(最佳實踐)Spring BootKafka
- java郵件傳送Java
- python傳送郵件Python
- Django——郵件傳送Django
- gmail傳送郵件AI
- phpcms傳送郵件PHP
- 郵件的傳送
- Laravel 傳送郵件Laravel
- thinkjs 傳送郵件JS
- Laravel傳送郵件Laravel
- PHP傳送郵件PHP
- nodejs 傳送郵件NodeJS
- SpringBoot開發案例之郵件多例項傳送Spring Boot
- Java Mail 郵件傳送(二):簡單封裝的郵件傳送JavaAI封裝
- 玩轉 SpringBoot2.x 之整合郵件傳送Spring Boot
- SpringBoot專案實現檔案上傳和郵件傳送Spring Boot
- golang傳送郵件(qq郵箱)Golang
- Android 快速傳送郵件Android
- 使用phpmailer傳送郵件PHPAI
- 定時傳送郵件
- 使用 smtplib 傳送郵件
- Python SMTP傳送郵件Python
- 郵件傳送API整理API
- php windows 傳送郵件PHPWindows
- Ubuntu的郵件傳送Ubuntu