java实现邮件发送详解

网友投稿 292 2022-12-10


java实现邮件发送详解

java实现邮件发送逻辑并不复杂(不包含附件),只是根据官方调用官方提供的sdk,首先需要引入maven依赖:

javax.mail

com.sun.mail

javax.mail

1.6.0

然后构造发送邮件所需的实体类

package com.email;

import java.io.Serializable;

/**

* @Author zjt

* @Date 2019年03月07 10:37

*/

public class EmailEntity implements Serializable {

private static final long serialVersionUID = 1L;

//邮箱服务器地址

private String host;

//主机端口

private Integer port;

//发送者的邮箱账号

private String userName;

//发送者的密码

private String password;

//发送者的邮箱地址

private String fromAddress;

//接收者的邮箱地址

private String toAddress;

//设置邮件主题

private String subject;

//设置邮件内容

private String context;

//设置邮件类型

private String contextType;

public String getHost() {

return host;

}

public void setHost(String host) {

this.host = host;

}

public Integer getPort() {

return port;

}

public void setPort(Integer port) {

this.port = port;

}

public String getUserName() {

return userName;

}

public void setUserName(String userName) {

this.userName = userName;

}

public String getPassword() {

return password;

}

public void setPassword(String password) {

this.password = password;

}

public String getFromAddress() {

return fromAddress;

}

public void setFromAddress(String fromAddress) {

this.fromAddress = fromAddress;

}

public String getToAddress() {

return toAddress;

}

public void setToAddress(String toAddress) {

this.toAddress = toAddress;

}

public String getSubject() {

return subject;

}

public void setSubject(String subject) {

this.subject = subject;

}

public String getContext() {

return context;

}

public void setContext(String context) {

this.context = context;

}

public String getContextType() {

return contextType;

}

public void setContextType(String contextType) {

this.contextType = contextType;

}

}

其次,编写调用邮件发送方法

package com.email;

import javax.mail.*;

import javax.mail.internet.InternetAddress;

import javax.mail.internet.MimeMessage;

import java.util.*;

/**

* @Author zjt

* @Date 2019年03月07 10:38

*/

public class EmailSend {

public static boolean EmailSendTest(EmailEntity emailEntity){

try {

//配置文件

Properties properties = new Properties();

properties.put("mail.smtp.auth", "true");

properties.put("mail.smtp.host", emailEntity.getHost());

properties.put("mail.smtp.port", 25);

properties.put("mail.smtp.starrttls.enable", "true");

//创建会话

VerifyEmail verifyEmail = new VerifyEmail(emailEntity.getUserName(), emailEntity.getPassword());

Session mailSession = Session.getInstance(properties, verifyEmail);

mailSession.setDebug(true);

//创建信息对象

Message message = new MimeMessage(mailSession);

InternetAddress from = new InternetAddress(emailEntity.getFromAddress());

InternetAddress to = new InternetAddress(emailEntity.getToAddress());

//设置邮件信息的来源

message.setFrom(from);

//设置邮件的接收者

message.setRecipient(MimeMessage.RecipientType.TO, to);

message.setSubject(emailEntity.getSubject());

//设置邮件发送日期

message.setSentDate(new Date());

//设置邮件内容

message.setContent(emailEntity.getContext() , emailEntity.getContextTypeOTblT());

message.saveChanges();

//发送邮件

Transport transport = mailSession.getTransport("smtp");

transport.connect(emailEntity.getHost(), emailEntity.getUserName(), emailEntity.getPassword());

System.out.println("发送:" + transport);

transport.sendMessage(message, message.getAllRecipients());

System.out.println("success");

return true;

} catch (MessagingException e) {

e.printStackTrace();

System.out.println("fiaOTblTl...");

return false;

}

}

}

在调用邮件发送方法中使用到验证邮箱登录名和密码是否正确的方法

package com.email;

import javax.mail.Authenticator;

import javax.mail.PasswordAuthentication;

/**

* 验证邮箱

* @Author zjt

* @Date 2019年03月07 10:32

*/

public class VerifyEmail extends Authenticator {

//账号

private String userName;

//密码

private String password;

public String getUserName() {

return userName;

}

public void setUserName(String userName) {

this.userName = userName;

}

public String getPassword() {

return password;

}

public void setPassword(String password) {

this.password = password;

}

//构造方法

public VerifyEmail(){

super();

}

public VerifyEmail(String userName, String password) {

super();

this.userName = userName;

this.password = password;

}

protected PasswordAuthentication getPasswordAuthentication(){

return new PasswordAuthentication(userName, password);

}

}

编写测试类,测试邮件发送方法是否成功

package com.email;

import org.junit.jupiter.api.Test;

/**

* @Author zjt

* @Date 2019年03月07 10:26

*/

public class TestEmail {

@Test

public void test(){

EmailEntity email = new EmailEntity();

email.setUserName("*******@163.com");

email.setPassword("******");

email.setHost("smtp.163.com");

email.setPort(25);

email.setFromAddress("******@163.com");

email.setToAddress("******@163.com");

email.setSubject("这是一封测试邮件!!!!");

email.setContext("看看这是什么");

email.setContextType("text/html;charset=utf-8");

boolean flag = EmailSend.EmailSendTest(email);

System.err.println("邮件发送结果=="+flag);

}

}

在这里测试的163邮箱发送,需要注意的是,此处的密码不是登录密码呦,而是设置中客户端授权密码呦。

执行测试文件之后,可以登录邮箱看到发送的结果


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

上一篇:详解Java分布式IP限流和防止恶意IP攻击方案
下一篇:Java底层基于二叉搜索树实现集合和映射/集合Set功能详解
相关文章

 发表评论

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