怎么使用SpringBoot發送郵件?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。
SpringBoot項目引入郵件包
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency>
yml配置郵件相關
spring: mail: # 郵件服務地址 host: smtp.163.com # 端口,可不寫默認 port: 25 # 編碼格式 default-encoding: utf-8 # 用戶名 username: xxx@163.com # 授權碼,就是我們剛才準備工作獲取的代碼 password: xxx # 其它參數 properties: mail: smtp: # 如果是用 SSL 方式,需要配置如下屬性,使用qq郵箱的話需要開啟 ssl: enable: true required: true # 郵件接收時間的限制,單位毫秒 timeout: 10000 # 連接時間的限制,單位毫秒 connectiontimeout: 10000 # 郵件發送時間的限制,單位毫秒 writetimeout: 10000
針對于上面提的超時問題,捕獲超時異常就可解決。
郵件發送工具類
主要通過以下工具類就可以滿足發送java郵件的需要。當我們進行好 yml 配置后,SpringBoot會幫助我們自動配置 JavaMailSender 我們通過這個java類就可以實現操作java來發送郵件。以下列舉了幾種常用的郵件。
@Service
public class MailService {
private static final Logger logger = LoggerFactory.getLogger(MailServiceImpl.class);
@Autowired
private JavaMailSender mailSender;
private static final String SENDER = "xxx@163.com";
/**
* 發送普通郵件
*
* @param to 收件人
* @param subject 主題
* @param content 內容
*/
@Override
public void sendSimpleMailMessge(String to, String subject, String content) {
SimpleMailMessage message = new SimpleMailMessage();
message.setFrom(SENDER);
message.setTo(to);
message.setSubject(subject);
message.setText(content);
try {
mailSender.send(message);
} catch (Exception e) {
logger.error("發送簡單郵件時發生異常!", e);
}
}
/**
* 發送 HTML 郵件
*
* @param to 收件人
* @param subject 主題
* @param content 內容
*/
@Override
public void sendMimeMessge(String to, String subject, String content) {
MimeMessage message = mailSender.createMimeMessage();
try {
//true表示需要創建一個multipart message
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(SENDER);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(content, true);
mailSender.send(message);
} catch (MessagingException e) {
logger.error("發送MimeMessge時發生異常!", e);
}
}
/**
* 發送帶附件的郵件
*
* @param to 收件人
* @param subject 主題
* @param content 內容
* @param filePath 附件路徑
*/
@Override
public void sendMimeMessge(String to, String subject, String content, String filePath) {
MimeMessage message = mailSender.createMimeMessage();
try {
//true表示需要創建一個multipart message
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(SENDER);
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);
} catch (MessagingException e) {
logger.error("發送帶附件的MimeMessge時發生異常!", e);
}
}
/**
* 發送帶靜態文件的郵件
*
* @param to 收件人
* @param subject 主題
* @param content 內容
* @param rscIdMap 需要替換的靜態文件
*/
@Override
public void sendMimeMessge(String to, String subject, String content, Map<String, String> rscIdMap) {
MimeMessage message = mailSender.createMimeMessage();
try {
//true表示需要創建一個multipart message
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(SENDER);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(content, true);
for (Map.Entry<String, String> entry : rscIdMap.entrySet()) {
FileSystemResource file = new FileSystemResource(new File(entry.getValue()));
helper.addInline(entry.getKey(), file);
}
mailSender.send(message);
} catch (MessagingException e) {
logger.error("發送帶靜態文件的MimeMessge時發生異常!", e);
}
}
}發送郵件的demo
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringbooEmailDemoApplicationTests {
@Autowired
private MailService mailService;
private static final String TO = "xxx@qq.com";
private static final String SUBJECT = "測試郵件";
private static final String CONTENT = "test content";
/**
* 測試發送普通郵件
*/
@Test
public void sendSimpleMailMessage() {
mailService.sendSimpleMailMessge(TO, SUBJECT, CONTENT);
}
/**
* 測試發送html郵件
*/
@Test
public void sendHtmlMessage() {
String htmlStr = "<h2>Test</h2>";
mailService.sendMimeMessge(TO, SUBJECT, htmlStr);
}
/**
* 測試發送帶附件的郵件
* @throws FileNotFoundException
*/
@Test
public void sendAttachmentMessage() throws FileNotFoundException {
File file = ResourceUtils.getFile("classpath:test.txt");
String filePath = file.getAbsolutePath();
mailService.sendMimeMessge(TO, SUBJECT, CONTENT, filePath);
}
/**
* 測試發送帶附件的郵件
* @throws FileNotFoundException
*/
@Test
public void sendPicMessage() throws FileNotFoundException {
String htmlStr = "<html><body>測試:圖片1 <br> <img src=\'cid:pic1\'/> <br>圖片2 <br> <img src=\'cid:pic2\'/></body></html>";
Map<String, String> rscIdMap = new HashMap<>(2);
rscIdMap.put("pic1", ResourceUtils.getFile("classpath:pic01.jpg").getAbsolutePath());
rscIdMap.put("pic2", ResourceUtils.getFile("classpath:pic02.jpg").getAbsolutePath());
mailService.sendMimeMessge(TO, SUBJECT, htmlStr, rscIdMap);
}
}看完上述內容是否對您有幫助呢?如果還想對相關知識有進一步的了解或閱讀更多相關文章,請關注億速云行業資訊頻道,感謝您對億速云的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。