Java实现邮件发送详解

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

javax.mail

<dependency >
  <groupId >com.sun.mail</groupId >
  <artifactId >javax.mail</artifactId >
  <version >1.6.0</version >
</dependency >

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

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.getContextType());
   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("fial...");
   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邮箱发送,需要注意的是,此处的密码不是登录密码呦,而是设置中客户端授权密码呦。

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

总结
篇幅有限!篇幅有限!关于Java实现邮件发送详解,就聊到这儿啦…啦…啦…
以上小编所介绍的全部相关的笔记资料都已整理成册,不论是Redis面试+Redis实战pdf,还是MongDB快速上手+MongDB集群安全等手写pdf笔记,想白嫖都很so easy!!
——如果你get到了,那就点个赞转发支持一波吧!
——整理不易,白嫖私信我领取源码学习资料,100%回复分享!
——也可以添加小助手vx:xcw18874131605获取源码学习资料哦
——祝前程似锦,offer不断,好好学习,天天向上!

猜你喜欢

转载自blog.csdn.net/p1830095583/article/details/115326396
今日推荐