SSM整合框架实现发送邮件功能
生活随笔
收集整理的這篇文章主要介紹了
SSM整合框架实现发送邮件功能
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
SSM整合框架實現發送郵件功能
1.導入發送郵件的依賴
<!-- 發送郵件jar包--><!--spring支持--><dependency><groupId>org.springframework</groupId><artifactId>spring-context-support</artifactId><version>5.0.0.RELEASE</version></dependency><!--郵件發送--><dependency><groupId>com.sun.mail</groupId><artifactId>javax.mail</artifactId><version>1.6.1</version></dependency>2.發送郵件的賬號開啟POP3/SMTP/IMAP/SMIP服務
3.spring-mybatis.xml配置文件中添加發送郵件所需的配置信息
<!--郵件配置--><context:property-placeholder location="classpath:email.properties" ignore-unresolvable="true"/><!--配置郵件接口--><bean id="javaMailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl"><property name="host" value="${mail.smtp.host}"/><property name="username" value="${mail.smtp.username}"/><property name="password" value="${mail.smtp.password}"/><property name="defaultEncoding" value="${mail.smtp.defaultEncoding}"/><property name="javaMailProperties"><props><prop key="mail.smtp.auth">${mail.smtp.auth}</prop><prop key="mail.smtp.timeout">${mail.smtp.timeout}</prop></props></property></bean>4.創建email.properties 接上配置郵件接口
#服務器主機名 smtp.xx.com mail.smtp.host=smtp.qq.com mail.smtp.username=開啟服務的郵箱號@qq.com #密碼/客戶端授權碼 mail.smtp.password=開啟服務的郵箱號的密鑰 #編碼字符 mail.smtp.defaultEncoding=utf-8 #是否進行用戶名密碼校驗 mail.smtp.auth=true #設置超時時間 mail.smtp.timeout=200005.在控制層測試
package com.mvo.controller;import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.io.FileSystemResource; import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.mail.javamail.MimeMessageHelper; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;import javax.mail.MessagingException; import javax.mail.internet.MimeMessage; import java.io.File; import java.io.IOException; import java.util.Properties;/*** 測試郵件發送controller*/ @Controllerpublic class SendMailController {@Autowiredprivate JavaMailSender javaMailSender;//在spring中配置的郵件發送的bean@RequestMapping("sendMailTest")public Object sendMailTest(){MimeMessage mMessage=javaMailSender.createMimeMessage();//創建郵件對象MimeMessageHelper mMessageHelper;Properties prop = new Properties();String from;try {//從配置文件中拿到發件人郵箱地址prop.load(this.getClass().getResourceAsStream("/email.properties"));from = prop.get("mail.smtp.username")+"";mMessageHelper=new MimeMessageHelper(mMessage,true);mMessageHelper.setFrom(from);//發件人郵箱mMessageHelper.setTo("收件人郵箱號@qq.com");//收件人郵箱mMessageHelper.setSubject("ssm框架測試郵件發送");//郵件的主題mMessageHelper.setText("<p>這是使用spring,springmvc,mybatis整合框架的郵件功能發送的一封郵件,測試</p>",true);//郵件的文本內容,true表示文本以html格式打開File file=new File("C:\\Users\\lcl\\Pictures\\Saved Pictures\\blog.csdn.net_Mr__Viking_article_details_81090046.png");//在郵件中添加一張圖片 FileSystemResource resource=new FileSystemResource(file);//mMessageHelper.addInline("fengye", resource);//這里指定一個id,在上面引用 //mMessageHelper.addAttachment("QQ截圖20200721221932.png", resource);//在郵件中添加一個附件 javaMailSender.send(mMessage);//發送郵件} catch (MessagingException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}return "發送成功";} }啟動項目 --> 輸入地址測試
6.效果圖
總結
以上是生活随笔為你收集整理的SSM整合框架实现发送邮件功能的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 第四范式携手智源研究院 共推全球最大
- 下一篇: mybatis获取表名——mybatis