Java发送邮件的五种方法 |
您所在的位置:网站首页 › 怎么用微信发邮件到别人邮箱 › Java发送邮件的五种方法 |
邮件发送其实是一个非常常见的需求,用户注册,找回密码等地方,都会用到,使用 JavaSE 代码发送邮件,步骤还是挺繁琐的,Spring Boot 中对于邮件发送,提供了相关的自动化配置类,使得邮件发送变得非常容易,本文我们就来一探究竟!看看使用 Spring Boot 发送邮件的 5 中姿势 邮件基础我们经常会听到各种各样的邮件协议,比如 SMTP、POP3、IMAP ,那么这些协议有什么作用,有什么区别?我们先来讨论一下这个问题。 SMTP 是一个基于 TCP/IP 的应用层协议,江湖地位有点类似于 HTTP,SMTP 服务器默认监听的端口号为 25 。看到这里,小伙伴们可能会想到既然 SMTP 协议是基于 TCP/IP 的应用层协议,那么我是不是也可以通过 Socket 发送一封邮件呢?回答是肯定的。 这三个步骤可以分别对应我们的邮件发送过程,假设从 [email protected] 发送邮件到 [email protected] : [email protected] 先将邮件投递到腾讯的邮件服务器 腾讯的邮件服务器将我们的邮件投递到网易的邮件服务器 [email protected] 登录网易的邮件服务器查看邮件 SMTP 协议全称为 Simple Mail Transfer Protocol,译作简单邮件传输协议,它定义了邮件客户端软件与 SMTP 服务器之间,以及 SMTP 服务器与 SMTP 服务器之间的通信规则。 也就是说 [email protected] 用户先将邮件投递到腾讯的 SMTP 服务器这个过程就使用了 SMTP 协议,然后腾讯的 SMTP 服务器将邮件投递到网易的 SMTP 服务器这个过程也依然使用了 SMTP 协议,SMTP 服务器就是用来收邮件。 而 POP3 协议全称为 Post Office Protocol ,译作邮局协议,它定义了邮件客户端与 POP3 服务器之间的通信规则,那么该协议在什么场景下会用到呢?当邮件到达网易的 SMTP 服务器之后, [email protected] 用户需要登录服务器查看邮件,这个时候就该协议就用上了:邮件服务商都会为每一个用户提供专门的邮件存储空间,SMTP 服务器收到邮件之后,就将邮件保存到相应用户的邮件存储空间中,如果用户要读取邮件,就需要通过邮件服务商的 POP3 邮件服务器来完成。 开启 SMTP、POP3点进QQ邮箱,点击设置点击账户,往下面拉,开启SMTP服务 加入web依赖,和发送邮件的依赖 每个配置项,我都添加了注释的 #配置 SMTP 服务器地址 spring.mail.host=smtp.qq.com #SMTP 服务器的端口 spring.mail.port=465 #配置邮箱用户名 [email protected] #配置密码,注意,不是真正的密码,而是刚刚申请到的授权码 spring.mail.password=************* #默认的邮件编码 spring.mail.default-encoding=UTF-8 #配饰 SSL 加密工厂 spring.mail.properties.mail.smtp.socketFactoryClass=javax.net.ssl.SSLSocketFactory #表示开启 DEBUG 模式,这样,邮件发送过程的日志会在控制台打印出来,方便排查错误 spring.mail.properties.mail.debug=true #官方建议使用 465 端口,而 465 端口是 SSL 协议的,所以不仅要换端口, #还需要进行 SSL 协议替换。下面是在 application.properties 进行的邮件发送相关配置, spring.mail.protocol=smtp spring.mail.properties.mail.smtp.ssl.enable=true spring.mail.properties.mail.smtp.socketFactory.port=465下面开始发送邮件 第一种、发送简单邮件建立测试类,idea在创建项目的时候在test文件夹下面创建了一个,我用的就是这个 @Autowired JavaMailSender javaMailSender; @Test public void sendSimpleMail() { SimpleMailMessage message = new SimpleMailMessage(); message.setSubject("这是一封测试邮件"); message.setFrom("[email protected]"); message.setTo("1559***[email protected]"); // message.setCc("[email protected]"); // message.setBcc("[email protected]"); message.setSentDate(new Date()); message.setText("这是测试邮件的正文"); javaMailSender.send(message); }效果如下 hello 大家好,这是一封测试邮件,这封邮件包含两种图片,分别如下 第一张图片: 第二张图片: 这里的邮件 text 是一个 HTML 文本,里边涉及到的图片资源先用一个占位符占着,setText 方法的第二个参数 true 表示第一个参数是一个 HTML 文本。 setText 之后,再通过 addInline 方法来添加图片资源。 最后执行该方法,发送邮件,效果如下: 引入Freemarker依赖 org.springframework.boot spring-boot-starter-freemarker在templatest文件夹下面创建mail.ftl文件,模板如下所示 Title hello 欢迎加入 xxx 大家庭,您的入职信息如下: 姓名 ${username} 工号 ${num} 薪水 ${salary} 一起努力创造辉煌接下来,将邮件模板渲染成 HTML ,然后发送即可。发送模板代码如下, // Freemarker 作邮件模板 @Test public void sendFreemarkerMail() throws MessagingException, IOException, TemplateException { MimeMessage mimeMessage = javaMailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true); helper.setSubject("这是一封测试邮件"); helper.setFrom("[email protected]"); helper.setTo("[email protected]"); // helper.setCc("[email protected]"); // helper.setBcc("[email protected]"); helper.setSentDate(new Date()); //构建 Freemarker 的基本配置 Configuration configuration = new Configuration(Configuration.VERSION_2_3_0); // 配置模板位置,启动类的位置JhjmailApplication ClassLoader loader = JhjmailApplication.class.getClassLoader(); configuration.setClassLoaderForTemplateLoading(loader, "templates"); //加载模板 Template template = configuration.getTemplate("mail.ftl"); User user = new User(); user.setUsername("蒋皓洁"); user.setNum(1); user.setSalary( 99999); StringWriter out = new StringWriter(); //模板渲染,渲染的结果将被保存到 out 中 ,将out 中的 html 字符串发送即可 template.process(user, out); helper.setText(out.toString(),true); javaMailSender.send(mimeMessage); }需要注意的是,虽然引入了 Freemarker 的自动化配置,但是我们在这里是直接 new Configuration 来重新配置 Freemarker 的,所以 Freemarker 默认的配置这里不生效,因此,在填写模板位置时,值为 templates 。 当然里面涉及到一个new一个User对象,则要新建一个User类 public class User { private String username; private int num; private int Salary; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public int getNum() { return num; } public void setNum(int num) { this.num = num; } public int getSalary() { return Salary; } public void setSalary(int salary) { Salary = salary; } }调用sendFreemarkerMail发送email如下所示 推荐在 Spring Boot 中使用 Thymeleaf 来构建邮件模板。因为 Thymeleaf 的自动化配置提供了一个 TemplateEngine,通过 TemplateEngine 可以方便的将 Thymeleaf 模板渲染为 HTML ,同时,Thymeleaf 的自动化配置在这里是继续有效的 。 首先,引入 Thymeleaf 依赖: org.springframework.boot spring-boot-starter-thymeleafThymeleaf 模板如下 Title hello 欢迎加入 xxx 大家庭,您的入职信息如下: 姓名 工号 薪水 一起努力创造辉煌发送邮件如下 @Autowired TemplateEngine templateEngine; @Test public void sendThymeleafMail() throws MessagingException { MimeMessage mimeMessage = javaMailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true); helper.setSubject("这是一封测试邮件"); helper.setFrom("[email protected]"); helper.setTo("[email protected]"); // helper.setCc("[email protected]"); // helper.setBcc("[email protected]"); helper.setSentDate(new Date()); Context context = new Context(); context.setVariable("username", "javaboy"); context.setVariable("num","000001"); context.setVariable("salary", "99999"); String process = templateEngine.process("mail.html", context); helper.setText(process,true); javaMailSender.send(mimeMessage); }注意一点,下面这个Context类不是自定义的,是thymeleaf.context.Context中的 Context context = new Context();所有要引入下面这个 import org.thymeleaf.context.Context;结果如下所示 共同学习,共同进步 |
今日新闻 |
点击排行 |
|
推荐新闻 |
图片新闻 |
|
专题文章 |
CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭 |