很简单 步骤走起->

1.需要一个邮箱账号,我以163邮箱为例,先开启第三方服务后获得密码,后面用来邮箱登录

2.加入mail 依赖

3.properties配置账号和第三方服务密码(不是邮箱密码,是第一步中的密码)

4.简单看下项目结构:

5.直接上serviceImpl

package com.idress.inter.impl;

import com.idress.inter.MailService;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.beans.factory.annotation.Value;

import org.springframework.core.io.FileSystemResource;

import org.springframework.mail.SimpleMailMessage;

import org.springframework.mail.javamail.JavaMailSender;

import org.springframework.mail.javamail.MimeMessageHelper;

import org.springframework.stereotype.Component;

import javax.mail.MessagingException;

import javax.mail.internet.MimeMessage;

import java.io.File;

@Component

public class MailServiceImpl implements MailService {

private final Logger logger = LoggerFactory.getLogger(this.getClass());

@Autowired

    private JavaMailSender mailSender;

@Value("${spring.mail.from.addr}")

    private String from;//由谁发出邮件 is my

@Override

    public void sendSimpleMail(String to, String subject, String content) {

        SimpleMailMessage message = new SimpleMailMessage();

        message.setFrom(from);

        message.setTo(to);

        message.setSubject(subject);

        message.setText(content);

try {

            mailSender.send(message);

            logger.info("简单邮件已经发送。");

        } catch (Exception e) {

            logger.error("发送简单邮件时发生异常!", e);

        }

}

//发送html格式邮件

    @Override

    public void sendHtmlMail(String to, String subject, String content) {

        MimeMessage message = mailSender.createMimeMessage();

        try {

            //true表示需要创建一个multipart message

            MimeMessageHelper helper = new MimeMessageHelper(message, true);

            helper.setFrom(from);

            helper.setTo(to);

            helper.setSubject(subject);

            helper.setText(content, true);

mailSender.send(message);

            logger.info("html邮件发送成功");

        } catch (MessagingException e) {

            logger.error("发送html邮件时发生异常!", e);

        }

    }

//发送带附件的邮件

    @Override

    public void sendAttachmentsMail(String to, String subject, String content, String filePath) {

        MimeMessage message = mailSender.createMimeMessage();

try {

            MimeMessageHelper helper = new MimeMessageHelper(message, true);

            helper.setFrom(from);

            helper.setTo(to);

            helper.setSubject(subject);

            helper.setText(content, true);

FileSystemResource file = new FileSystemResource(new File(filePath));

            String fileName = filePath.substring(filePath.lastIndexOf(File.separator));

            helper.addAttachment(fileName, file);

mailSender.send(message);

            logger.info("带附件的邮件已经发送。");

        } catch (MessagingException e) {

            logger.error("发送带附件的邮件时发生异常!", e);

        }

    }

/**

     * 嵌入静态资源的邮件

     */

    @Override

    public void sendInlineResourceMail(String to, String subject, String content, String rscPath, String rscId) {

        MimeMessage message = mailSender.createMimeMessage();

try {

            MimeMessageHelper helper = new MimeMessageHelper(message, true);

            helper.setFrom(from);

            helper.setTo(to);

            helper.setSubject(subject);

            helper.setText(content, true);

FileSystemResource res = new FileSystemResource(new File(rscPath));

            helper.addInline(rscId, res);

mailSender.send(message);

            logger.info("嵌入静态资源的邮件已经发送。");

        } catch (MessagingException e) {

            logger.error("发送嵌入静态资源的邮件时发生异常!", e);

        }

    }

}

6.junt测试走起

package com.idress;

import com.idress.inter.MailService;
import com.sun.xml.internal.bind.CycleRecoverable;
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.core.env.Environment;
import org.springframework.test.context.junit4.SpringRunner;
import org.thymeleaf.context.Context;
import org.thymeleaf.spring4.SpringTemplateEngine; @RunWith(SpringRunner.class)
@SpringBootTest
public class Sb10MailApplicationTests { @Autowired
private MailService mailService; @Test
public void contextLoads() {
mailService.sendSimpleMail("2473538988@qq.com","恭喜你的简历被我们查看"," 请您....");
} @Test
public void testHtmlMail() throws Exception{
for (int i = 0; i < 25; i++) { String content = "<html>\n" +
"<body>\n" +
" <h3>hello world ! 这是一封Html邮件!</h3>\n" +
"</body>\n" +
"</html>";
mailService.sendHtmlMail("whatarey0206@qq.com", "test simple mail", content);
} }
//发送待附件的邮件
@Test
public void sendAttachmentsMail() {
String filePath = "E:\\img01.PNG";
mailService.sendAttachmentsMail("2633992679@qq.com", "主题:带附件的邮件", "有附件,请查收!", filePath);
} @Test
public void sendInlineResourceMail() {
String rscId = "neo006";
String content="<html><body>这是有图片的邮件:<img src='cid:" + rscId + "' ></body></html>";
String imgPath = "E:\\img01.PNG"; mailService.sendInlineResourceMail("1544303828@qq.com", "主题:这是有图片的邮件", content, imgPath, rscId);
} //使用邮件模板 @Autowired
private SpringTemplateEngine templateEngine; @Test
public void sendTemplateMail() {
//创建邮件正文
Context context = new Context();
context.setVariable("id", "006");
String emailContent = templateEngine.process("emailTemplate", context); mailService.sendHtmlMail("1544303828@qq.com","主题:这是模板邮件",emailContent);
}
}

7.邮箱模板(thymeleaf)

转载http://www.ityouknow.com/springboot/2017/05/06/springboot-mail.html

一位大神 纯洁的微笑

springboot-mail发邮件,不需要邮件服务器的更多相关文章

  1. 关于java mail 发邮件的问题总结(转)

    今天项目中有需要用到java mail发送邮件的功能,在网上找到相关代码,代码如下: import java.io.IOException; import java.util.Properties; ...

  2. 带着新人学springboot的应用10(springboot+定时任务+发邮件)

    接上一节,环境一样,这次来说另外两个任务,一个是定时任务,一个是发邮件. 1.定时任务 定时任务可以设置精确到秒的准确时间去自动执行方法. 我要一个程序每一秒钟说一句:java小新人最帅 于是,我就写 ...

  3. spring boot(16)-mail发邮件

    上一篇讲了如何处理异常,并且异常最终会写入日志.但是日志是写在服务器上的,我们无法及时知道.如果能够将异常发送到邮箱,我们可以在第一时间发现这个异常.当然,除此以外,还可以用来给用户发验证码以及各种离 ...

  4. shell中mail发邮件的问题

    今天为了监控一下脚本,按照网上说的利用mail 发邮件,mail -s "error预警2" peien@1221.qq.com<'邮件内容',发现出现cc,不知道啥问题,也 ...

  5. Django的邮件发送以及云服务器上遇到的问题

    邮件发送 首先我们的邮箱要开通smtp服务,然后就可以在settings中配置了 EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBacken ...

  6. ios 设置亮度、声音;调用发短信、邮件、打电话

    一,设置亮度 [[UIScreen mainScreen] setBrightness:0.5];//0.0~1.0 二,设置声音 1,添加 MediaPlayer.framework 框架 2,在需 ...

  7. IMAP(Internet Mail Access Protocol,Internet邮件访问协议)以前称作交互邮件访问协议(Interactive Mail Access Protocol)。

    IMAP(Internet Mail Access Protocol,Internet邮件访问协议)以前称作交互邮件访问协议(Interactive Mail Access Protocol).IMA ...

  8. Springboot 系列(十三)使用邮件服务

    在我们这个时代,邮件服务不管是对于工作上的交流,还是平时的各种邮件通知,都是一个十分重要的存在.Java 从很早时候就可以通过 Java mail 支持邮件服务.Spring 更是对 Java mai ...

  9. Android实例-打电话、发短信和邮件,取得手机IMEI号(XE8+小米2)

    结果: 1.不提示发短信卡住,点击没有反映,我猜想,可能是因为我用的是小米手机吧. 2.接收短信报错,我猜想可能是我改了里面的方法吧(哪位大神了解,求指教). 3.project -->opti ...

  10. Linux出现You have new mail in /var/spool/mail/root提示,关闭邮件提示清理内容的解决方案

    Linux出现You have new mail in /var/spool/mail/root提示,关闭邮件提示的解决方案 有的时候敲一下回车,就出来You have new mail in /va ...

随机推荐

  1. 案例八:shell自动化管理账本脚本

    该脚本目的帮助管理员创建账号.删除账号.锁定账号.解锁账号. #!/bin/bash #filename: #author: #date:2018-6-6 echo "用户管理程序" ...

  2. 将Android手机无线连接到Ubuntu实现唱跳Rap

    您想要将Android设备连接到Ubuntu以传输文件.查看Android通知.以及从Ubuntu桌面发送短信 – 你会怎么做?将文件从手机传输到PC时不要打电话给自己:使用GSConnect就可以. ...

  3. C#依赖注入-初步概念了解

    维基百科说:"依赖注入是一种软件设计模式,在这种模式下,一个或更多的依赖(或服务)被注入(或者通过引用传递)到一个独立的对象(或客户端)中,然后成为了该客户端状态的一部分.该模式分离了客户端 ...

  4. .net框架的详解

    .net运行时 :Core,Xamarin,Mono或Framework? Core:面向所有平台 window .liniux .苹果mac .net Framework4.8:面向微软系统  已经 ...

  5. linux mailx 发送邮件到qq邮箱

    POP3/SMTP服务默认是开启的,没开启的点开启 然后点击生成授权码,发送消息,就会给出授权码了 yum install mailx # centos sudo apt-get install he ...

  6. IntelliJ:下载第三方库

    学习自:(6条消息) IDEA中第三方软件包安装步骤_Li某人_初学者-CSDN博客 1.下载对应的软件包:以commons-logging为例 2.复制这个jar包到项目根目录下的lib目录下(li ...

  7. 通过Xshell或Xftp链接Windows10子系统Linux

    1.打开linux系统 2.切换到root角色 sudo -i //切换到root 并输入密码 3.卸载安装ssh server sudo apt-get remove openssh-server ...

  8. C#中?和:?和??代表什么

    ?代表可空类型修饰符    引用类型可以使用空引用表示一个不存在的值,而值类型通常不能表示为空.为了使值类型也可为空,就可以使用可空类型?:带便三元表达式    int a=b>c?b:c 如果 ...

  9. LeetCode-074-搜索二维矩阵

    搜索二维矩阵 题目描述:编写一个高效的算法来判断 m x n 矩阵中,是否存在一个目标值.该矩阵具有如下特性: 每行中的整数从左到右按升序排列. 每行的第一个整数大于前一行的最后一个整数. 示例说明请 ...

  10. 安卓应用修改(高德.度盘.QQ浏览器.bili)

    软件介绍 高德地图修改版去广告精简版!核心功能如下: 语音包丰富.有大家最喜欢的小团团语音!测距,实时公交,足迹,限行等主流功能全都有.有完整的地铁图,且更新很快!杭州地铁3号线刚开通就更新了.有车道 ...