当前位置: 移动技术网 > IT编程>开发语言>Java > spring boot 发送邮件

spring boot 发送邮件

2018年10月27日  | 移动技术网IT编程  | 我要评论

一、前期准备

1.包引入

<dependency>
            <groupid>org.springframework.boot</groupid>
            <artifactid>spring-boot-starter-mail</artifactid>
</dependency>

2.配置文件

spring.mail.protocol=smtp
spring.mail.host=smtp.163.com
spring.mail.username=zhaobaolintest@163.com
spring.mail.password=abc123456789

这里切记 password是授权密码 授权密码 授权密码  不是你的邮箱登陆密码

二、核心代码

import com.example.demo.dto.vo.mailsendvo;
import org.springframework.beans.factory.annotation.value;
import org.springframework.stereotype.component;
import org.springframework.util.stringutils;

import javax.mail.*;
import javax.mail.internet.internetaddress;
import javax.mail.internet.mimemessage;
import java.util.date;
import java.util.properties;

/**
 * @author:zhao-baolin
 * @description: 邮件发送类
 * @date:created in 2018/4/17
 * @modified by:
 */
@component
public class mailutil {

    @value("${spring.mail.username}")
    private string frommail;

    @value("${spring.mail.password}")
    private string frompassword;

    @value("${spring.mail.host}")
    private string host;

    /**
     * 邮件发送
     */
    public boolean send(mailsendvo sendmailvo) throws exception
    {
        system.out.println(sendmailvo.tostring());
        final string ssl_factory = "javax.net.ssl.sslsocketfactory";

        if(stringutils.isempty(sendmailvo.gettomail()) || stringutils.isempty(sendmailvo.gettitle()) || stringutils.isempty(sendmailvo.getcontent())){
            system.out.println("发送邮件参数为空 "+sendmailvo.tostring());
            return false;
        }
        // 1. 创建一封邮件
        properties props = new properties();
        props.setproperty("mail.smtp.host",host);
        props.setproperty("mail.smtp.auth", "true");

        props.setproperty("mail.smtp.socketfactory.class", ssl_factory);
        props.setproperty("mail.smtp.socketfactory.fallback", "false");

        props.setproperty("mail.smtp.port", "465");
        props.setproperty("mail.smtp.socketfactory.port", "465");

        authenticator auth = new authenticator() {
            @override
            public passwordauthentication getpasswordauthentication(){
                return new passwordauthentication(frommail, frompassword);
            }
        };
        session session = session.getinstance(props,auth);

        // 2.创建一个message,它相当于是邮件内容
        message message = new mimemessage(session);

        // 设置发送者
        message.setfrom(new internetaddress(frommail));

        // 设置收件人 抄送给自己 避免网易554错误
        message.setrecipient(mimemessage.recipienttype.cc, new internetaddress(frommail));
        message.setrecipient(mimemessage.recipienttype.to, new internetaddress(sendmailvo.gettomail()));

        // 邮件主题
        message.setsubject(sendmailvo.gettitle());
        // 邮件正文
        message.setcontent(sendmailvo.getcontent(), "text/html;charset=utf-8");

        // 设置显示的发件时间
        message.setsentdate(new date());

        // 保存前面的设置
        message.savechanges();

        try {
            // 创建 transport用于将邮件发送
            transport.send(message);

        }catch (exception e){
            system.out.println(sendmailvo.gettitle()+"发送邮件失败 "+e.getmessage());
            return false;
        }

        system.out.println("邮件发送结束");
        return true;

    }
    
}

里面有一个mailsendvo对象 这个对象封装了收件人、邮件标题、邮件内容三个属性 详情可以点进源码查看

三、源码分享

fork me on gitee

 

多说一句 邮件发送的速度与程序无关,千万不要跑程序里到处找bug,邮件发送只与网络有关。

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网