springboot 整合邮件发送功能

网友投稿 662 2022-11-13

springboot 整合邮件发送功能

pom依赖

org.springframework.boot

spring-boot-starter-mail

代码实现

import org.springframework.core.io.FileSystemResource;

import org.springframework.mail.javamail.MimeMessageHelper;

import javax.mail.Authenticator;

import javax.mail.PasswordAuthentication;

import javax.mail.Session;

import javax.mail.Transport;

import javax.mail.internet.InternetAddress;

import javax.mail.internet.MimeMessage;

import java.io.File;

import java.util.Properties;

public class Email {

/**

* 发送邮件

*/

private static Boolean sendMail() throws Exception {

try {

// 获取系统属性

Properties properties = System.getProperties();

properties.setProperty("mail.smtp.host", "smtp.163.com");// 设置邮件服务器

properties.setProperty("mail.smtp.auth", "true");// 打开认证

properties.setProperty("mail.smtp.port", "25"); //设置端口

properties.setProperty("mail.debug", "false"); // 是否打印日志

properties.setProperty("mail.smtp.connectiontimeout", "15000");//SMTP服务器连接超时时间

properties.setProperty("mail.smtp.timeout", "60000");//发送邮件超时时间

//设置密码需要实现抽象类Authenticator 重写 getPasswordAuthentication方法 原方法返回null

// AuthenticatorCheck authenticatorCheck = new AuthenticatorCheck("gaoph_dcits@163.com", "VKGHGACJBXFMURWZ");

Authenticator authenticatorCheck = new Authenticator() {

@Override

public PasswordAuthentication getPasswordAuthentication() {

return new PasswordAuthentication("gaoph_dcits@163.com", "VKGHGACJBXFMURWZ");

}

};

Session session = Session.getDefaultInstance(properties, authenticatorCheck);

MimeMessage message = new MimeMessage(session);

MimeMessageHelper messageHelper = new MimeMessageHelper(message, true, "UTF-8");

messageHelper.setFrom(new InternetAddress("gaoph_dcits@163.com", "央行流动性邮件发送服务", "UTF-8"));

messageHelper.setTo(new String[]{"m18110024862@163.com"});

messageHelper.setSubject("提醒");

messageHelper.setText("我去,hahh......" +

"

这是红色

"设置span内字体大小为24px" +

"

"

"
百度 ", true);

FileSystemResource fileSystemResource = new FileSystemResource(new File("D:\\order.xlsx"));

// 设置附件

messageHelper.addAttachment("11.xlsx", fileSystemResource);

FileSystemResource image = new FileSystemResource(new File("C:\\Users\\ext.hanyang3\\Pictures\\Camera Roll\\22.jpg"));

// 设置内联图片

messageHelper.addInline("22.jpg", image);

Transport.send(message);

// 插入邮件记录

} catch (Exception e) {

// 插入异常邮件记录

// 发送异常邮件

throw e;

}

return true;

}

public static void main(String[] args) throws Exception {

Email.sendMail();

}

}

Session常用函数

下面的表格中列出了Session常用的方法。包括获取Transport、Stored对象等

public static getInstance(java.util.Properties)

public static getInstance(java.util.Properties ,Authenticator)

获取Session对象的方法,由于Session的构造方法是私有的因此只能通过静态方法获取

public static getDefalutInstance(java.util.Properties)

public static getDefaultInstance(Properties ,Authenticator)

getTransport()

getTransport(java.lang.String protocol) 默认根据mail.transport.protocol属性中的协议创建。

返回实现了指定的具体邮件发送协议的Transport对象。Transport是抽象类,两个方法返回的都是实现某种协议的Transport的子类

getStore()

getStore(java.lang.String protocol) 默认根据mail.store.protocol属性中的协议创建接收邮件对象。

返回实现了指定具体邮件接收协议的Store对象。Store也是抽象类,两个方法返回的都是实现某种协议的Store的子类

setDebug(boolean debug) 当设置为true时,JavaMail AP就会将其运行过程和邮件服务器的交互命令信息输出到运行窗口中,用于JavaMail的调试有用。

PS:getInstance和getDefaultInstance方法的区别在于:getDefaultInstance方法返回一个Session对象后,将这个Session对象安装为默认的Session对象,

以后每次调用getDefaultInstance方法都将返回这个默认Session对象;而getInstance方法则是每次调用都返回一个新的Session对象。

参数Properties

对Properties类中可以通过setProperty(String key, String value)方法进行设置,其取值包括 :

参数Authenticator

在JavaMail中除了可以通过Transport.connect(host, user, passqord)方法在连接SMTP服务器是直接传递用户认证信息还可以借助Authenticator类来 获取用户认证信息

当使用Session的getInstance(properties, Authenticator)来创建Session对象时,会将Authenticator对象注册到该Session。以后这个Session对象的JavaMail客户端程序要向邮件服务器提交认证信息时,将调用该Session对象中注册的Authenticator对象,从中获取用户认证信息后传递给邮件服务器。

Authenticator类最常用的一个方法是:

http://protected PasswordAuthentication getPasswordAuthentication();

Authenticator类是抽象类,传递给getInstance方法的Authenticator对象只能是其子类的实例对象。Authenticator对定义的该方法的返回值为null,因此其子类必须覆盖该方法,由邮件开发人来实现。

PasswordAuthentication类中的方法有:

public PasswordAuthentication(String userName, String password)

public String getUserName();

public String getPassword();

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Redis-持久化
下一篇:如何监控海外服务器性能
相关文章

 发表评论

暂时没有评论,来抢沙发吧~