當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Spring Boot笔记-发送纯字符串邮件及带附件邮件
生活随笔
收集整理的這篇文章主要介紹了
Spring Boot笔记-发送纯字符串邮件及带附件邮件
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
首先引入Maven包:
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-mail</artifactId> </dependency>這里以QQ郵箱為例,要開通smtp的服務,這里還有要注意的。
盡量不要用剛剛注冊的郵箱,不然會被供應商退回的。
application.properties如下:
spring.mail.host=smtp.qq.com spring.mail.username=xxxxxxxxx@qq.com spring.mail.password=xxxxxxxxx spring.mail.default-encoding=utf-8發送代碼如下:
@Service public class MailService {@AutowiredJavaMailSender javaMailSender;public void sendTest(){//建立郵件消息SimpleMailMessage mainMessage = new SimpleMailMessage();//發送者mainMessage.setFrom("和spring.mail.username一樣的");//接收者mainMessage.setTo("目標郵箱");//發送的標題mainMessage.setSubject("這是您需要的文件");//發送的內容mainMessage.setText("請看附件");javaMailSender.send(mainMessage);}public void sendFileTest(ResourcesInfoVo resourcesInfo) throws MessagingException, IOException {MimeMessage mimeMessage = javaMailSender.createMimeMessage();MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);helper.setSubject("這是您需要的文件");//發送者helper.setFrom("和spring.mail.username一樣的");//接收者helper.setTo("目標郵箱");helper.setSentDate(new Date());helper.setText("需要的文件在附件中,歡迎下次繼續使用本系統。");InputStream inputStream = new ByteArrayInputStream(resourcesInfo.getContent());helper.addAttachment(resourcesInfo.getFileName(), new ByteArrayResource(IOUtils.toByteArray(inputStream)));javaMailSender.send(mimeMessage);} }這里要注意的,不同供應商的郵箱,發送附件和郵件數是有限的。這個要注意!
總結
以上是生活随笔為你收集整理的Spring Boot笔记-发送纯字符串邮件及带附件邮件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java笔记-spring-rabbit
- 下一篇: DM工作笔记-dimp及dexp的使用及