Java通過stmp協(xié)議發(fā)送郵件
更新時(shí)間:2020年02月11日 14:29:21 作者:tomato先生
這篇文章主要為大家詳細(xì)介紹了Java通過stmp協(xié)議發(fā)送郵件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
本文實(shí)例為大家分享了Java通過stmp協(xié)議發(fā)送郵件的具體代碼,供大家參考,具體內(nèi)容如下
pom.xml 導(dǎo)入包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
<version>2.1.7.RELEASE</version>
</dependency>
application.properties 配置信息
## 郵箱配置 #spring.mail.host=smtp.163.com ## 你的163郵箱 #spring.mail.username=18603@163.com ## 注意這里不是郵箱密碼,而是SMTP授權(quán)密碼 #spring.mail.password=***** #spring.mail.port=25 #spring.mail.default-encoding=UTF-8 #spring.mail.from=18603@163.com
代碼
package com.youjia.found.manager;
import com.youjia.found.common.util.Check;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.io.File;
/**
* <P>郵件處理類</P>
*
* @author eric
* @date 2020/2/6 11:08 AM
* @since
*/
@Component
public class MailManager {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
@Value("${spring.mail.from}")
private String from;
@Autowired
private JavaMailSender mailSender;
/**
* 帶附件的郵件
* @param to 收件人
* @param subject 主題
* @param content 內(nèi)容
* @param filePath 附件
*/
public boolean sendMail(String to, String subject, String content, String filePath) {
logger.info("stmp郵件發(fā)送 to:{}, subject:{}, content:{},filePath:{}", to, subject, content,filePath);
boolean isOK=false;
MimeMessage message = mailSender.createMimeMessage();
try {
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(from);
//支持多個(gè)收件人
InternetAddress[] internetAddressTo = InternetAddress.parse(to);
helper.setTo(internetAddressTo);
helper.setSubject(subject);
helper.setText(content, true);
//附件
if(Check.notEmpty(filePath)){
FileSystemResource file = new FileSystemResource(new File(filePath));
String fileName = filePath.substring(filePath.lastIndexOf(File.separator));
helper.addAttachment(fileName, file);
}
mailSender.send(message);
isOK= true;
} catch (Exception e) {
logger.error(e.getMessage(),e);
isOK= false;
}
return isOK;
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- java工具類SendEmailUtil實(shí)現(xiàn)發(fā)送郵件
- Android使用Javamail發(fā)送Email群發(fā)加附件
- java發(fā)送email一般步驟(實(shí)例講解)
- SSH框架網(wǎng)上商城項(xiàng)目第25戰(zhàn)之使用java email給用戶發(fā)送郵件
- Java HtmlEmail 郵件發(fā)送的簡(jiǎn)單實(shí)現(xiàn)代碼
- java.mail實(shí)現(xiàn)發(fā)送郵件
- Java通過exchange協(xié)議發(fā)送郵件
- 淺析JavaMail發(fā)送郵件后再通過JavaMail接收格式問題
- Spring框架JavaMailSender發(fā)送郵件工具類詳解
- SpringBoot JavaMailSender發(fā)送郵件功能
- java實(shí)現(xiàn)發(fā)送email小案例
相關(guān)文章
解決Feign切換client到okhttp無法生效的坑(出現(xiàn)原因說明)
這篇文章主要介紹了解決Feign切換client到okhttp無法生效的坑(出現(xiàn)原因說明),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-02-02
Java中類與對(duì)象的相關(guān)知識(shí)點(diǎn)總結(jié)
對(duì)象是類實(shí)例化出來的,對(duì)象中含有類的屬性,類是對(duì)象的抽象,下面這篇文章主要給大家介紹了關(guān)于Java中類與對(duì)象的一些相關(guān)知識(shí)點(diǎn),需要的朋友可以參考下2021-11-11
java 根據(jù)前端返回的字段名進(jìn)行查詢數(shù)據(jù)
本文介紹了如何在Java中使用SpringDataJPA實(shí)現(xiàn)動(dòng)態(tài)查詢功能,以便根據(jù)前端傳遞的字段名動(dòng)態(tài)構(gòu)建查詢語句,通過創(chuàng)建實(shí)體類、Repository接口、構(gòu)建動(dòng)態(tài)查詢、在Service層和Controller中使用動(dòng)態(tài)查詢,實(shí)現(xiàn)了前后端分離架構(gòu)中的靈活查詢需求2024-11-11
解決從Map、JSONObject取不存在鍵值對(duì)時(shí)的異常情況
這篇文章主要介紹了解決從Map、JSONObject取不存在鍵值對(duì)時(shí)的異常情況,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07

