温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

java实现邮件发送详解

发布时间:2020-09-28 04:46:47 来源:脚本之家 阅读:164 作者:瑾年丶深蓝 栏目:编程语言

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实现邮件发送详解

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

java实现邮件发送详解

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持亿速云。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI