Springboot实现Java邮件任务过程解析
更新时间:2019年09月18日 09:00:34 作者:Jump233
这篇文章主要介绍了Springboot实现Java邮件任务过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
1.maven引入依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency>
2.application.properties配置发送邮箱
//用户邮箱 spring.mail.username=753029781@qq.com //QQ邮箱开通第三方登录的授权码 spring.mail.password=xxxxxxxxxxxx //主机地址 spring.mail.host=smtp.qq.com //开启安全的连接 spring.mail.properties.mail.smtp.enable=true
3.测试
import cn.kgc.elastic.vo.Book;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMailMessage;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.test.context.junit4.SpringRunner;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringBoot03ApplicationTests {
//注入邮件发送器
@Autowired
JavaMailSenderImpl mailSender;
@Test
public void sentMail(){
//发送简单的邮件
SimpleMailMessage message=new SimpleMailMessage();
//邮件设置
//标题
message.setSubject("注意");
//内容
message.setText("有内鬼,终止交易");
//发送人
message.setFrom("753029781@qq.com");
//收件人
message.setTo("jumpjiang233@gmail.com");
mailSender.send(message);
}
@Test
public void tesr01() throws MessagingException {
//复杂邮件发送
MimeMessage mimeMessage=mailSender.createMimeMessage();
//使用helper上传文件
MimeMessageHelper helper=new MimeMessageHelper(mimeMessage,true);
helper.setSubject("注意");
//可以HTML样式
helper.setText("<b style='color:blue'>有内鬼,终止交易</b>",true);
//上传文件,可以上传多个
helper.addAttachment("1.jpg",new File("C:\\Users\\Jump\\Pictures\\1.jpg"));
helper.setFrom("753029781@qq.com");
helper.setTo("2871382340@qq.com");
mailSender.send(mimeMessage);
}
@Test
public void contextLoads() {
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
相关文章
java调用chatgpt接口来实现专属于自己的人工智能助手
这篇文章主要介绍了用java来调用chatget的接口,实现自己的聊天机器人,对人工智能感兴趣的小伙伴可以参考阅读2023-03-03
往DAO类中注入@PersistenceContext和@Resource的区别详解
这篇文章主要介绍了往DAO类中注入@PersistenceContext和@Resource的区别详解,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教2022-02-02
Spring中的@Autowired、@Qualifier和@Primary注解详解
这篇文章主要介绍了Spring中的@Autowired、@Qualifier和@Primary注解详解,@Autowired 注解,可以对类成员变量、方法和构造函数进行标注,完成自动装配的工作,@Autowired 是默认根据 byType 进行自动装配的,需要的朋友可以参考下2023-11-11


最新评论