项目中用到Spring Boot发QQ邮箱给女朋友

栏目: Java · 发布时间: 6年前

spring.mail.host=smtp.qq.com
spring.mail.protocol=smtp
spring.mail.username=1974627037@qq.com
spring.mail.default-encoding=UTF-8
spring.mail.port=465
spring.mail.properties.mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory
spring.mail.properties.mail.debug=true
spring.mail.password=hpqxqvacriwibdbe

复制代码

7.单元测试

@RunWith(SpringRunner.class)
@SpringBootTest
public class MailApplicationTests {
    @Autowired
    JavaMailSender mailSender;
    @Test
    public void contextLoads() {
        SimpleMailMessage msg = new SimpleMailMessage();
        msg.setTo("2579605371@qq.com");
        msg.setCc("757919476@qq.com");
        msg.setSubject("这是一封测试情书");
        msg.setFrom("1974627037@qq.com");
        msg.setSentDate(new Date());
        msg.setText("哈哈!你行不行啊");
        mailSender.send(msg);
    }
复制代码

8.用网页格式发送需要导pom.xml依赖

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
复制代码

9.写个main.html页

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <p>欢迎<span style="color: #ffff11;" th:text="${username}"></span> 加入
    XXX 大家庭</p>
    <div>你的入职信息如下:</div>
<table torder="1">
    <tr>
        <td>职位</td>
        <td>th:text="${position}"</td>

    </tr>
    <tr>
        <td>薪资</td>
        <td th:text="${salary}"></td>

    </tr>
</table>
</body>
</html>
复制代码

10.单元测试

package org.javaboy.mail;

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.MailSender;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMailMessage;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.test.context.junit4.SpringRunner;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import javax.mail.Message;
import javax.mail.MessageRemovedException;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.Date;

@RunWith(SpringRunner.class)
@SpringBootTest
public class MailApplicationTests {
    @Autowired
    JavaMailSender mailSender;

    @Autowired
    TemplateEngine templateEngine;
    @Test
    public void contextLoads() {
        SimpleMailMessage msg = new SimpleMailMessage();
        msg.setTo("2579605371@qq.com");

        msg.setSubject("这是一封情书");
        msg.setFrom("1974627037@qq.com");
        msg.setSentDate(new Date());
        msg.setText("约会吗");
        mailSender.send(msg);
    }

    @Test
    public void test1() throws MessagingException{
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage);
        helper.setCc("757919476@qq.com");
        helper.setCc("2579605371@qq.com");
        helper.setSubject("给我打万块钱");
        helper.setFrom("1974627037@qq.com");
        helper.setSentDate(new Date());
        Context context = new Context();
        context.setVariable("username","libobo");
        context.setVariable("position","老婆");
        context.setVariable("salary","10000");
        String mail = templateEngine.process("main", context);
        helper.setText(mail,true);
        mailSender.send(mimeMessage);
    }
}

复制代码

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

与孩子一起学编程

与孩子一起学编程

[美] 桑德Warren Sande、Carter Sande / 苏金国、姚曜 等 / 人民邮电出版社 / 2010-11 / 65.00元

一本老少咸宜的编程入门奇书!一册在手,你完全可以带着自己的孩子,跟随Sande父子组合在轻松的氛围中熟悉那些编程概念,如内存、循环、输入和输出、数据结构和图形用户界面等。这些知识一点儿也不高深,听起来备感亲切,书中言语幽默风趣而不失真义,让学习过程充满乐趣。细心的作者还配上了孩子们都喜欢的可爱漫画和经过运行测试的程序示例,教你用最易编写和最易理解的Python语言,写出你梦想中的游戏程序。 ......一起来看看 《与孩子一起学编程》 这本书的介绍吧!

MD5 加密
MD5 加密

MD5 加密工具

SHA 加密
SHA 加密

SHA 加密工具

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具