当前位置:网站首页>idea发送邮件
idea发送邮件
2022-07-13 17:37:00 【懒起来】
先导入依赖
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.4</version>
</dependency><dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>1.6.2</version>
</dependency>邮箱测试:
这里使用的阿里云的邮箱,由于qq邮箱需要一个授权码,这我不知道放在哪里,后续了解到后我会来补充。
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
MailAccount account = new MailAccount();
account.setHost("smtp.aliyun.com");
account.setPort(25);
account.setAuth(true);
account.setFrom("发送的邮箱账户");
//这里是发送邮箱账户
account.setUser("发送的邮箱账户");
//这里是发送邮箱密码
account.setPass("发送的邮箱密码");
//这里是需要待发送的邮箱账号
ArrayList<String> toMaill = CollUtil.newArrayList("需要待发送的邮箱账号");
MailUtil.send(account,toMaill , "测试", "邮件来自Hutool测试", false);
}
}
这个是可以发送成功的,现在我们来进行发送一个四位数的随机码来给邮箱
这里加入了随机数生成以及hutool的 StrUtil.format("您的验证码是:{}", c)字符串拼接
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
MailAccount account = new MailAccount();
account.setHost("smtp.aliyun.com");
account.setPort(25);
account.setAuth(true);
account.setFrom("发送的邮箱账户");
//这里是发送邮箱账户
account.setUser("发送的邮箱账户");
//这里是发送邮箱密码
account.setPass("发送的邮箱密码");
//这里是需要待发送的邮箱账号
ArrayList<String> toMaill = CollUtil.newArrayList("需要待发送的邮箱账号");
int c = RandomUtil.randomInt(1000,9999);
String format = StrUtil.format("您的验证码是:{}", c);
MailUtil.send(account,toMaill , "验证码", format, false);
}
}现在我们进行将发送邮箱的方法给抽离成service方法,这样,以后我们使用发送邮箱的时候就可以直接拷贝到我们自己的代码中直接使用。
抽离过程:
先建立一个MailService类
加入注解@Service然后使用@Value注解将写入配置文件的数据写入
@Service
public class MailService {
@Value("${demo.mail.host}")
private String mailHost;
@Value("${demo.mail.port:25}")
private Integer mailPort;
@Value("${demo.mail.from}")
private String mailFrom;
@Value("${demo.mail.user}")
private String mailUser;
@Value("${demo.mail.pwd}")
private String mailPwd;
@Value("${demo.mail.to}")
private List<String> mailTo;
public void send() {
MailAccount account = new MailAccount();
account.setHost(this.mailHost);
account.setPort(this.mailPort);
account.setAuth(true);
account.setFrom(this.mailFrom);
account.setUser(this.mailUser);
account.setPass(this.mailPwd);
int code = RandomUtil.randomInt(1000, 9999);
String body = StrUtil.format("您的验证码为:{}",code);
MailUtil.send(account, this.mailTo, "验证码", body, false);
}
}
配置文件
demo.mail.host = smtp.aliyun.com
demo.mail.port = 25
demo.mail.from = 邮箱账户
demo.mail.user = 邮箱账户
demo.mail.pwd = 邮箱密码
demo.mail.to = 待发送的邮箱账户App调用
@SpringBootApplication
public class App {
public static void main(String[] args) {
ConfigurableApplicationContext run = SpringApplication.run(App.class, args);
MailService bean = run.getBean(MailService.class);
bean.send();
}
}边栏推荐
- The 13th provincial competition of Bluebridge cup single chip microcomputer
- PV operation procedure questions
- 初识c语言(二)
- Range installation and attacker configuration for penetration testing
- Idea自动导出数据库中表的sql语句
- Use of command line client
- Changement de style TW du script singe huileux
- 【黄啊码】PHP实现文件下载,支持断点续传
- FTP 上传文件脚本说明
- Oil monkey script changes TW style
猜你喜欢

Teacher Wan Jianfeng dry goods sharing July 13, 2022

MVN clean or MVN clean package did not detect the test file

37.js--对象的成员操作和原型对象的操作(主要是相关程序举例)
c语言中的输入输出函数之printf函数

Aunt Qian's real-time risk control practice based on Flink

ES6--类(class)

Solve the problem of unable to load authentication plugin 'caching' when idea2020 connects to MySQL 8_ sha2_ Password 'exception

memo、useMemo、useCallback总结

ES6--解构赋值(重点)
![[tensorflow2] implementation of gradient inversion layer (GRL) and domain antagonism training neural network (Dann)](/img/c4/1e1d68a69cb41da8ebb6463a35ca82.png)
[tensorflow2] implementation of gradient inversion layer (GRL) and domain antagonism training neural network (Dann)
随机推荐
数据操作—查询篇
创建/删除 表空间/用户
万建峰老师 干货分享2022年7月13日
c语言中的输入输出函数之scanf函数
反射获取成员方法和成员变量
JS字符串内置功能
ES6内置对象的新方法新特性
ES6--Set
Adaptive batch job scheduler: automatically derive parallelism for Flink batch jobs
Svelte 官方入门教程(1)—— 简介
Data operation - query
【黄啊码】fastadmin接入微信支付和支付宝支付
SNMP起步啦
Picture definition problem
性能测试分类
ES6--arr(数组)-map-set的转换
ES6--类(class)
Ardunio -- interaction between touch sensor and light -- judge whether the light is on according to the touch screen
ES6的高级赋值方法——解构赋值
在线多行文本批量正则替换添加后缀工具