91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

java實現郵件發送詳解

發布時間:2020-09-28 04:46:47 來源:腳本之家 閱讀:163 作者:瑾年丶深藍 欄目:編程語言

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

东城区| 卓尼县| 吉隆县| 宜丰县| 临西县| 九江市| 绥滨县| 丰原市| 石嘴山市| 逊克县| 菏泽市| 桦川县| 兰坪| 丽江市| 修武县| 涿州市| 琼海市| 阳城县| 南川市| 中阳县| 商南县| 遵化市| 南京市| 凉城县| 桦南县| 福安市| 东海县| 宁河县| 九龙城区| 奇台县| 西贡区| 宜城市| 东港市| 柏乡县| 商南县| 松滋市| 芮城县| 灵山县| 建平县| 微山县| 汉川市|