java鬼混笔记:springboot 8、springboot发邮件mail
来源:互联网 发布:php读取数据库并显示 编辑:程序博客网 时间:2024/05/16 18:24
注册一般都有邮箱注册的,短信就得花钱了,,,所以把这发邮件功能做个笔记,直接上代码了
application.properties
spring.mvc.view.prefix: /WEB-INF/jsp/spring.mvc.view.suffix: .jsp
#smtp知识这次不做笔记
spring.mail.host=smtp.qq.comspring.mail.port=25spring.mail.username=526614527@qq.comspring.mail.password=**********(这里是你开通QQ邮箱smtp功能时提供的密码,要发1条短信才能开通)spring.mail.properties.mail.smtp.auth=truespring.mail.defaultEncoding=UTF-8spring.mail.protocol=smtpspring.mail.properties.mail.smtp.starttls.enable=truespring.mail.properties.mail.smtp.connectiontimeout=5000spring.mail.properties.mail.smtp.timeout=5000spring.mail.properties.mail.smtp.writetimeout=5000mail.sender=526614527@qq.com#这个是说明是谁发的,要spring.mail.username一样,不然后台报错说认证不一致.mail.sender可以写成别名字,在java代码里照着拿就行了
pom.xml加上这两个依赖文件
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-mail</artifactId></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context-support</artifactId><version>4.0.1.RELEASE</version></dependency>
java代码:
EmailController.java
package sample.jsp;import java.io.File;import javax.mail.internet.InternetAddress;import javax.mail.internet.MimeMessage;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Value;import org.springframework.mail.javamail.JavaMailSender;import org.springframework.mail.javamail.MimeMessageHelper;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;@Controllerpublic class EmailController {@Autowiredprivate JavaMailSender sender;@Value("${mail.sender}")// 拿到配置文件里的名字 private String from;@RequestMapping("/email.do")@ResponseBodyString home() {try {sendEmail();return "Email Sent!";} catch (Exception ex) {return "Error in sending email: " + ex;}}private void sendEmail(){try {MimeMessage message = sender.createMimeMessage();MimeMessageHelper helper = new MimeMessageHelper(message, true, "utf-8");//MimeMessageHelper(message);helper.setValidateAddresses(true);helper.setFrom(new InternetAddress(from));helper.setTo("13719249618@163.com");helper.setText("我是中国人");// 普通的文字//helper.setText("<font color='red'>爱我中华</font>", true);// 带html格式的文字//helper.addAttachment("ShadowsocksR-4.6.1-win.7z", new File("F:"+File.separator+"ShadowsocksR-4.6.1-win.7z"));helper.setSubject("我是中国人");sender.send(message);} catch (Exception e) {e.printStackTrace();}}}
OK,就这么简单!!!
阅读全文
0 0
- java鬼混笔记:springboot 8、springboot发邮件mail
- java鬼混笔记:springboot 1、第一个springboot项目例子
- java鬼混笔记:springboot 2、springboot修改访问端口
- java鬼混笔记:springboot 4、springboot热部署
- SpringBoot发邮件-注意事项
- springboot定时发邮件
- java鬼混笔记:springboot之thymeleaf 3:th:each遍历
- java鬼混笔记:springboot之thymeleaf 6:内联模式
- java鬼混笔记:springboot之redis保存对象
- java鬼混笔记:springboot 3、springboot创建web项目和打包
- java鬼混笔记:springboot 5、springboot的Scheduled定时器:fixedDelay和fixedRate区别
- java鬼混笔记:springboot 6、springboot整合mybatis(支持多数源)
- java鬼混笔记:springboot 7、springboot整合mybatis后再加上druid数据库连接池
- java鬼混笔记:springboot 9、springboot整合mybatis加上分页功能
- java鬼混笔记:springboot 10、springboot logback-spring.xml简单的日志配置
- java mail 发邮件
- springboot mail
- Springboot mail邮件服务功能学习
- Ubuntu安装tplink wn822n网卡驱动
- c语言(三)字符串、数组
- C#基础(5)——三元表达式
- Redis的服务端安装
- 筛选法
- java鬼混笔记:springboot 8、springboot发邮件mail
- 妖梦拼木棒(组合数学)
- Java练习--继承(6)
- Python数据类型转换
- 将本地代码托管到github
- Unity3D 大型游戏 最后一站 源码 部分重点 GameView-BaseWindow(16)
- String类
- 顺序查找
- spring注解配置拦截器