使用springboot实现邮箱发送

一. 我们需要导入发送邮件 的依赖jar包

[html]  view plain  copy
  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  
  2.   <modelVersion>4.0.0</modelVersion>  
  3.   <groupId>cn.et</groupId>  
  4.   <artifactId>SPRINGBOOT-SENDMAIL</artifactId>  
  5.   <version>0.0.1-SNAPSHOT</version>  
  6.   <parent>  
  7.     <groupId>org.springframework.boot</groupId>  
  8.     <artifactId>spring-boot-starter-parent</artifactId>  
  9.     <version>1.5.9.RELEASE</version>  
  10. </parent>  
  11. <dependencies>  
  12.     <dependency>  
  13.         <groupId>org.springframework.boot</groupId>  
  14.         <artifactId>spring-boot-starter-web</artifactId>  
  15.     </dependency>  
  16.      <dependency>  
  17.         <groupId>org.springframework.boot</groupId>  
  18.         <artifactId>spring-boot-starter-mail</artifactId>  
  19.     </dependency>  
  20. </dependencies>  
  21. </project>  


二.   创建一个application.properties配置文件  配置访问邮箱的参数

[html]  view plain  copy
  1. spring.mail.default-encoding=UTF-8  
  2. spring.mail.host=smtp.163.com  
  3. spring.mail.password=密码 授权码  
  4. spring.mail.port=25  
  5. spring.mail.protocol=smtp  
  6. spring.mail.username=用户名  


三. 创建一个controller 发送邮件的业务

[java]  view plain  copy
  1. package cn.et;  
  2.   
  3. import org.springframework.beans.factory.annotation.Autowired;  
  4. import org.springframework.mail.SimpleMailMessage;  
  5. import org.springframework.mail.javamail.JavaMailSender;  
  6. import org.springframework.web.bind.annotation.GetMapping;  
  7. import org.springframework.web.bind.annotation.RequestMapping;  
  8. import org.springframework.web.bind.annotation.RequestMethod;  
  9. import org.springframework.web.bind.annotation.RestController;  
  10.   
  11. @RestController  
  12. public class MailController {  
  13.     @Autowired  
  14.     private JavaMailSender jms;  
  15.         <strong><span style="color:#cc0000;">@GetMapping("/send")</span></strong>  
  16.         public String send(){  
  17.             SimpleMailMessage mailMessage = new SimpleMailMessage();  
  18.             //谁发的  
  19.             mailMessage.setFrom("*******@163.com");  
  20.             //发给谁  
  21.             mailMessage.setTo("********@qq.com");  
  22.             //标题  
  23.             mailMessage.setSubject("我");  
  24.             //内容  
  25.             mailMessage.setText("逗你玩");  
  26.             jms.send(mailMessage);  
  27.             return "1";  
  28.         }  
  29. }  

四.
 创建一个启动类  启动项目

[java]  view plain  copy
  1. package cn.et;  
  2.   
  3. import org.springframework.boot.SpringApplication;  
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;  
  5.   
  6. @SpringBootApplication  
  7. public class Main {  
  8.     public static void main(String[] args) {  
  9.         SpringApplication.run(Main.class, args);  
  10.     }  
  11. }  


五.   在浏览器中访问 地址 http://localhost:8080/send  返回 "1" 说明发送成功
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值