您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關基于SpringBoot如何實現發送帶附件的郵件的內容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
<!--發送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 發送帶附件的郵件 , 需要進行重載方法 */ Map<String, Object> sendAttachmentsMail(String to, String subject, String content, String filePath); }
service實現
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; //發送者 @Value("${mail.fromMail.addr}") private String from; //TODO 設置發送郵件重載方式 @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)); // 發送附件 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); //添加多個附件可以使用多條 helper.addAttachment(fileName,file); //helper.addAttachment(fileName,file); helper.addAttachment(fileName, file); mailSender.send(message); map.put("code", 1); map.put("message", "發送成功"); return map; } catch (MessagingException e) { e.printStackTrace(); map.put("code",0); map.put("message","發送失敗"); return map; } catch (FileNotFoundException e) { e.printStackTrace(); map.put("code",-1); map.put("message","沒有文件"); return map; } finally { } } }
或將郵件內容及郵件地址等封裝為EmailContent的bean實體
通過Controll而接受.
下面是我的郵件主機配置,大家可以拿去用
##上傳文件限制大小 spring.servlet.multipart.max-file-size=10MB spring.servlet.multipart.max-request-size=10MB ##發送email設置 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 ##郵件主機地址 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
感謝各位的閱讀!關于“基于SpringBoot如何實現發送帶附件的郵件”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。