基于SpringBoot實(shí)現(xiàn)發(fā)送帶附件的郵件
這篇文章主要介紹了基于SpringBoot實(shí)現(xiàn)發(fā)送帶附件的郵件,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
<!--發(fā)送email依賴--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency>
service
package com.ykmimi.job.service; import org.springframework.stereotype.Service; import java.util.Map; @Service public interface MailService { /** * TODO 發(fā)送帶附件的郵件 , 需要進(jìn)行重載方法 */ Map<String, Object> sendAttachmentsMail(String to, String subject, String content, String filePath); }
service實(shí)現(xiàn)
package com.ykmimi.job.service.impl; import com.ykmimi.job.service.MailService; import org.springframework.beans.factory.annotation.Value; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.mail.javamail.MimeMessageHelper; import org.springframework.stereotype.Service; import org.springframework.util.ResourceUtils; import javax.annotation.Resource; import javax.mail.MessagingException; import javax.mail.internet.MimeMessage; import java.io.File; import java.io.FileNotFoundException; import java.util.HashMap; import java.util.Map; @Service public class MailServiceImpl implements MailService { @Resource private JavaMailSender mailSender; //發(fā)送者 @Value("${mail.fromMail.addr}") private String from; //TODO 設(shè)置發(fā)送郵件重載方式 @Override public Map<String, Object> sendAttachmentsMail(String to, String subject, String content, String filePath) { System.out.println("in sendAttachmentsMail"); System.out.println(filePath); MimeMessage message = mailSender.createMimeMessage(); Map<String, Object> map = new HashMap<>(); try { MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setFrom(from); helper.setTo(to); helper.setSubject(subject); helper.setText(content, true); // FileSystemResource file = new FileSystemResource(new File(filePath)); // 發(fā)送附件 File file = new File(filePath); file = ResourceUtils.getFile(file.getAbsolutePath()); String fileName = filePath.substring(filePath.lastIndexOf(File.separator)); // String fileName = filePath.substring(filePath.lastIndexOf("/")); // String fileName = "附件"; System.out.println(fileName); //添加多個(gè)附件可以使用多條 helper.addAttachment(fileName,file); //helper.addAttachment(fileName,file); helper.addAttachment(fileName, file); mailSender.send(message); map.put("code", 1); map.put("message", "發(fā)送成功"); return map; } catch (MessagingException e) { e.printStackTrace(); map.put("code",0); map.put("message","發(fā)送失敗"); return map; } catch (FileNotFoundException e) { e.printStackTrace(); map.put("code",-1); map.put("message","沒有文件"); return map; } finally { } } }
或?qū)⑧]件內(nèi)容及郵件地址等封裝為EmailContent的bean實(shí)體
通過Controll而接受.
下面是我的郵件主機(jī)配置,大家可以拿去用
##上傳文件限制大小 spring.servlet.multipart.max-file-size=10MB spring.servlet.multipart.max-request-size=10MB ##發(fā)送email設(shè)置 spring.application.name=spring-boot-mail spring.mail.host=smtp.126.com spring.mail.username=hostinbj@126.com spring.mail.password=1314520jy spring.mail.default-encoding=UTF-8 ##郵件主機(jī)地址 mail.fromMail.addr=hostinbj@126.com spring.mail.properties.mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory #郵件端口 spring.mail.properties.mail.smtp.socketFactory.port=465 spring.mail.properties.mail.smtp.auth=true spring.mail.properties.mail.smtp.starttls.enable=true spring.mail.properties.mail.smtp.starttls.required=true
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- SpringBoot使用FreeMarker模板發(fā)送郵件
- SpringBoot集成E-mail發(fā)送各種類型郵件
- SpringBoot實(shí)現(xiàn)發(fā)送郵件功能
- SpringBoot發(fā)送郵件功能 驗(yàn)證碼5分鐘過期
- Spring Boot整合郵件發(fā)送與注意事項(xiàng)
- Spring Boot中利用JavaMailSender發(fā)送郵件的方法示例(附源碼)
- Spring Boot實(shí)戰(zhàn)之發(fā)送郵件示例代碼
- Spring Boot實(shí)現(xiàn)郵件發(fā)送功能
- Springboot實(shí)現(xiàn)郵件發(fā)送功能
- SpringBoot實(shí)現(xiàn)郵件發(fā)送功能的姿勢(shì)分享
相關(guān)文章
java 重載(overload)與重寫(override)詳解及實(shí)例
這篇文章主要介紹了java 重載(overload)與重寫(override)詳解及實(shí)例的相關(guān)資料,并附實(shí)例代碼,需要的朋友可以參考下2016-10-10SpringSecurity從數(shù)據(jù)庫中獲取用戶信息進(jìn)行驗(yàn)證的案例詳解
這篇文章主要介紹了SpringSecurity從數(shù)據(jù)庫中獲取用戶信息進(jìn)行驗(yàn)證的案例詳解,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01Java底層基于鏈表實(shí)現(xiàn)集合和映射--集合Set操作詳解
這篇文章主要介紹了Java底層基于鏈表實(shí)現(xiàn)集合和映射集合Set操作,結(jié)合實(shí)例形式詳細(xì)分析了Java使用鏈表實(shí)現(xiàn)集合和映射相關(guān)原理、操作技巧與注意事項(xiàng),需要的朋友可以參考下2020-03-03Springboot和bootstrap實(shí)現(xiàn)shiro權(quán)限控制配置過程
這篇文章主要介紹了Springboot和bootstrap實(shí)現(xiàn)shiro權(quán)限控制,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04簡(jiǎn)單捋捋@RequestParam 和 @RequestBody的使用
這篇文章主要介紹了簡(jiǎn)單捋捋@RequestParam 和 @RequestBody的使用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12SpringBoot集成JWT的工具類與攔截器實(shí)現(xiàn)方式
這篇文章主要介紹了SpringBoot集成JWT的工具類與攔截器實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01Spring事務(wù)失效的一種原因關(guān)于this調(diào)用的問題
這篇文章主要介紹了Spring事務(wù)失效的一種原因關(guān)于this調(diào)用的問題,本文給大家分享問題原因及解決辦法,通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2021-10-10