亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

java郵件通知工具類

 更新時(shí)間:2019年04月18日 10:17:15   作者:Leon04095  
這篇文章主要為大家詳細(xì)介紹了java郵件通知工具類,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

工作中很難避免會(huì)有一些需要郵件通知的情況,網(wǎng)絡(luò)上也有很多郵件模板和工具類,在此給出一個(gè)工具類供參考

 前面一篇文章重新封裝了工具給出更好的適應(yīng)項(xiàng)目需要但為提供原始工具類作參考這里給出。

下面引入架包

工具類

package com.leon.isoftstoneweb.commen.utils.email;
 
import java.util.Date;
import java.util.Properties;
 
import javax.mail.Address;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
 
/**
 * @author leon
 * @createDate 2018年6月22日 上午9:32:05
 * @version v1.0
 * @classRemarks 郵件工具類
 */
public class SendEmailUtil {
 
 
 
 /**
  * 以文本格式發(fā)送郵件
  *
  * @param mailInfo
  *   待發(fā)送的郵件的信息
  */
 public boolean sendTextMail(MailBody mailInfo) throws Exception {
  // 判斷是否需要身份認(rèn)證
  MailAuthenticator authenticator = null;
  Properties pro = mailInfo.getProperties();
  if (mailInfo.isValidate()) {
   // 如果需要身份認(rèn)證,則創(chuàng)建一個(gè)密碼驗(yàn)證器
   authenticator = new MailAuthenticator(mailInfo.getUserName(), mailInfo.getPassword());
  }
  // 根據(jù)郵件會(huì)話屬性和密碼驗(yàn)證器構(gòu)造一個(gè)發(fā)送郵件的session
  Session sendMailSession = Session.getDefaultInstance(pro, authenticator);
  // logBefore(logger, "構(gòu)造一個(gè)發(fā)送郵件的session");
 
  // 根據(jù)session創(chuàng)建一個(gè)郵件消息
  Message mailMessage = new MimeMessage(sendMailSession);
  // 創(chuàng)建郵件發(fā)送者地址
  Address from = new InternetAddress(mailInfo.getFromAddress());
  // 設(shè)置郵件消息的發(fā)送者
  mailMessage.setFrom(from);
  // 創(chuàng)建郵件的接收者地址,并設(shè)置到郵件消息中
  Address to = new InternetAddress(mailInfo.getToAddress());
  mailMessage.setRecipient(Message.RecipientType.TO, to);
  // 設(shè)置郵件消息的主題
  mailMessage.setSubject(mailInfo.getSubject());
  // 設(shè)置郵件消息發(fā)送的時(shí)間
  mailMessage.setSentDate(new Date());
  // 設(shè)置郵件消息的主要內(nèi)容
  String mailContent = mailInfo.getContent();
  mailMessage.setText(mailContent);
  // 發(fā)送郵件
  Transport.send(mailMessage);
  // System.out.println("發(fā)送成功!");
  return true;
 }
 
 /**
  * 以HTML格式發(fā)送郵件
  *
  * @param mailInfo
  *   待發(fā)送的郵件信息
  */
 public boolean sendHtmlMail(MailBody mailInfo) throws Exception {
  // 判斷是否需要身份認(rèn)證
  MailAuthenticator authenticator = null;
  Properties pro = mailInfo.getProperties();
  // 如果需要身份認(rèn)證,則創(chuàng)建一個(gè)密碼驗(yàn)證器
  if (mailInfo.isValidate()) {
   authenticator = new MailAuthenticator(mailInfo.getUserName(), mailInfo.getPassword());
  }
  // 根據(jù)郵件會(huì)話屬性和密碼驗(yàn)證器構(gòu)造一個(gè)發(fā)送郵件的session
  Session sendMailSession = Session.getDefaultInstance(pro, authenticator);
 
  // 根據(jù)session創(chuàng)建一個(gè)郵件消息
  Message mailMessage = new MimeMessage(sendMailSession);
  // 創(chuàng)建郵件發(fā)送者地址
  Address from = new InternetAddress(mailInfo.getFromAddress());
  // 設(shè)置郵件消息的發(fā)送者
  mailMessage.setFrom(from);
  // 創(chuàng)建郵件的接收者地址,并設(shè)置到郵件消息中
  Address to = new InternetAddress(mailInfo.getToAddress());
  // Message.RecipientType.TO屬性表示接收者的類型為TO
  mailMessage.setRecipient(Message.RecipientType.TO, to);
  // 設(shè)置郵件消息的主題
  mailMessage.setSubject(mailInfo.getSubject());
  // 設(shè)置郵件消息發(fā)送的時(shí)間
  mailMessage.setSentDate(new Date());
  // MiniMultipart類是一個(gè)容器類,包含MimeBodyPart類型的對(duì)象
  Multipart mainPart = new MimeMultipart();
  // 創(chuàng)建一個(gè)包含HTML內(nèi)容的MimeBodyPart
  BodyPart html = new MimeBodyPart();
  // 設(shè)置HTML內(nèi)容
  html.setContent(mailInfo.getContent(), "text/html; charset=utf-8");
  mainPart.addBodyPart(html);
  // 將MiniMultipart對(duì)象設(shè)置為郵件內(nèi)容
  mailMessage.setContent(mainPart);
  // 發(fā)送郵件
  Transport.send(mailMessage);
  return true;
 }
 
 /**
  * @param SMTP
  *   郵件服務(wù)器
  * @param PORT
  *   端口
  * @param EMAIL
  *   本郵箱賬號(hào)
  * @param PAW
  *   本郵箱密碼
  * @param toEMAIL
  *   對(duì)方箱賬號(hào)
  * @param TITLE
  *   標(biāo)題
  * @param CONTENT
  *   內(nèi)容
  * @param TYPE
  *   1:文本格式;2:HTML格式
  */
 public static boolean sendEmail(String SMTP, String PORT, String EMAIL, String PAW, String toEMAIL, String TITLE,
   String CONTENT, String TYPE) {
 
  // 這個(gè)類主要是設(shè)置郵件
  MailBody mailInfo = new MailBody();
 
  mailInfo.setMailServerHost(SMTP);
  mailInfo.setMailServerPort(PORT);
  mailInfo.setValidate(true);
  mailInfo.setUserName(EMAIL);
  mailInfo.setPassword(PAW);
  mailInfo.setFromAddress(EMAIL);
  mailInfo.setToAddress(toEMAIL);
  mailInfo.setSubject(TITLE);
  mailInfo.setContent(CONTENT);
  // 這個(gè)類主要來發(fā)送郵件
 
  SendEmailUtil sms = new SendEmailUtil();
  try {
   if ("1".equals(TYPE)) {
    return sms.sendTextMail(mailInfo);
   } else {
    return sms.sendHtmlMail(mailInfo);
   }
  } catch (Exception e) {
   return false;
  }
 
 }
 
 public static void main(String[] args) {
  SendEmailUtil.sendEmail("smtp.isoftstone.com", "25", "441053249@qq.com",
    "whkay+11F", "441053249@qq.com", "系統(tǒng)郵件測(cè)試",
    "******審批流程 已審核!", "2");
 }
 
}

郵件需要使用的基本信息

package com.leon.isoftstoneweb.commen.utils.email;
 
import java.util.Properties;
 
 
/**
 * @author leon
 * @createDate 2018年6月22日 上午9:35:05
 * @version v1.0
 * @classRemarks 發(fā)送郵件需要使用的基本信息
 */
 
public class MailBody {
 
 // 發(fā)送郵件的服務(wù)器的IP和端口
 private String mailServerHost;
 private String mailServerPort = "25";
 // 郵件發(fā)送者的地址
 private String fromAddress;
 // 郵件接收者的地址
 private String toAddress;
 // 登陸郵件發(fā)送服務(wù)器的用戶名和密碼
 private String userName;
 private String password;
 // 是否需要身份驗(yàn)證
 private boolean validate = false;
 // 郵件主題
 private String subject;
 // 郵件的文本內(nèi)容
 private String content;
 // 郵件附件的文件名
 private String[] attachFileNames;
 
 /**
  * 獲得郵件會(huì)話屬性
  */
 public Properties getProperties() {
  Properties p = new Properties();
  p.put("mail.smtp.host", this.mailServerHost);
  p.put("mail.smtp.port", this.mailServerPort);
  p.put("mail.smtp.auth", validate ? "true" : "false");
  return p;
 }
 
 public String getMailServerHost() {
  return mailServerHost;
 }
 
 public void setMailServerHost(String mailServerHost) {
  this.mailServerHost = mailServerHost;
 }
 
 public String getMailServerPort() {
  return mailServerPort;
 }
 
 public void setMailServerPort(String mailServerPort) {
  this.mailServerPort = mailServerPort;
 }
 
 public boolean isValidate() {
  return validate;
 }
 
 public void setValidate(boolean validate) {
  this.validate = validate;
 }
 
 public String[] getAttachFileNames() {
  return attachFileNames;
 }
 
 public void setAttachFileNames(String[] fileNames) {
  this.attachFileNames = fileNames;
 }
 
 public String getFromAddress() {
  return fromAddress;
 }
 
 public void setFromAddress(String fromAddress) {
  this.fromAddress = fromAddress;
 }
 
 public String getPassword() {
  return password;
 }
 
 public void setPassword(String password) {
  this.password = password;
 }
 
 public String getToAddress() {
  return toAddress;
 }
 
 public void setToAddress(String toAddress) {
  this.toAddress = toAddress;
 }
 
 public String getUserName() {
  return userName;
 }
 
 public void setUserName(String userName) {
  this.userName = userName;
 }
 
 public String getSubject() {
  return subject;
 }
 
 public void setSubject(String subject) {
  this.subject = subject;
 }
 
 public String getContent() {
  return content;
 }
 
 public void setContent(String textContent) {
  this.content = textContent;
 }
}

郵件服務(wù)器身份認(rèn)證實(shí)體類

package com.leon.isoftstoneweb.commen.utils.email;
 
import javax.mail.*; 
 
/**
 * @author leon
 * @createDate 2018年6月22日 上午9:35:05
 * @version v1.0
 * @classRemarks 身份認(rèn)證實(shí)體類
 */
 
public class MailAuthenticator extends Authenticator{ 
 String userName=null; 
 String password=null; 
  
 public MailAuthenticator(){ 
 } 
 public MailAuthenticator(String username, String password) { 
  this.userName = username; 
  this.password = password; 
 } 
 protected PasswordAuthentication getPasswordAuthentication(){ 
  return new PasswordAuthentication(userName, password); 
 } 
} 

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • SpringBoot結(jié)合Redis配置工具類實(shí)現(xiàn)動(dòng)態(tài)切換庫

    SpringBoot結(jié)合Redis配置工具類實(shí)現(xiàn)動(dòng)態(tài)切換庫

    本文主要介紹了SpringBoot結(jié)合Redis配置工具類實(shí)現(xiàn)動(dòng)態(tài)切換庫,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-08-08
  • Spring Boot 配置和使用多線程池的實(shí)現(xiàn)

    Spring Boot 配置和使用多線程池的實(shí)現(xiàn)

    這篇文章主要介紹了Spring Boot 配置和使用多線程池的實(shí)現(xiàn),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-06-06
  • Struts2實(shí)現(xiàn)單文件或多文件上傳功能

    Struts2實(shí)現(xiàn)單文件或多文件上傳功能

    這篇文章主要為大家詳細(xì)介紹了Struts2實(shí)現(xiàn)單文件或多文件上傳功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-03-03
  • 關(guān)于在Java中反轉(zhuǎn)數(shù)組的4種詳細(xì)方法

    關(guān)于在Java中反轉(zhuǎn)數(shù)組的4種詳細(xì)方法

    這篇文章主要介紹了關(guān)于在Java中反轉(zhuǎn)數(shù)組的4種詳細(xì)方法,數(shù)組是一個(gè)固定長度的存儲(chǔ)相同數(shù)據(jù)類型的數(shù)據(jù)結(jié)構(gòu),數(shù)組中的元素被存儲(chǔ)在一段連續(xù)的內(nèi)存空間中,今天我們來學(xué)習(xí)一下如何反轉(zhuǎn)數(shù)組
    2023-05-05
  • Spring觀察者模式之事件發(fā)布訂閱實(shí)現(xiàn)和源碼詳解

    Spring觀察者模式之事件發(fā)布訂閱實(shí)現(xiàn)和源碼詳解

    這篇文章主要介紹了Spring觀察者模式之事件發(fā)布訂閱實(shí)現(xiàn)和源碼詳解,Spring認(rèn)為發(fā)布訂閱主題,其實(shí)可以理解為事件驅(qū)動(dòng)的編碼,先來實(shí)現(xiàn)以下Spring容器中的事件發(fā)布訂閱,需要的朋友可以參考下
    2024-01-01
  • 一文帶你學(xué)習(xí)Java中的線程

    一文帶你學(xué)習(xí)Java中的線程

    線程是系統(tǒng)調(diào)度的最小單元,一個(gè)進(jìn)程可以包含多個(gè)線程,線程是負(fù)責(zé)執(zhí)行二進(jìn)制指令的。本文將詳細(xì)給大家介紹一下Java中的線程,,需要的朋友可以參考下
    2023-05-05
  • Java通過反射注解賦值的方法詳解

    Java通過反射注解賦值的方法詳解

    這篇文章主要為大家詳細(xì)介紹了Java語言如何通過反射實(shí)現(xiàn)注解賦值,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,需要的可以參考一下
    2022-07-07
  • 淺談Java多進(jìn)程程序的運(yùn)行模式

    淺談Java多進(jìn)程程序的運(yùn)行模式

    這篇文章主要介紹了淺談Java多進(jìn)程程序的運(yùn)行模式,包括對(duì)進(jìn)程阻塞問題的討論等,需要的朋友可以參考下
    2015-11-11
  • SpringMVC通過RESTful結(jié)構(gòu)實(shí)現(xiàn)頁面數(shù)據(jù)交互

    SpringMVC通過RESTful結(jié)構(gòu)實(shí)現(xiàn)頁面數(shù)據(jù)交互

    RESTFUL是一種網(wǎng)絡(luò)應(yīng)用程序的設(shè)計(jì)風(fēng)格和開發(fā)方式,基于HTTP,可以使用XML格式定義或JSON格式定義。RESTFUL適用于移動(dòng)互聯(lián)網(wǎng)廠商作為業(yè)務(wù)接口的場(chǎng)景,實(shí)現(xiàn)第三方OTT調(diào)用移動(dòng)網(wǎng)絡(luò)資源的功能,動(dòng)作類型為新增、變更、刪除所調(diào)用資源
    2022-08-08
  • JAVA中的函數(shù)接口示例詳解

    JAVA中的函數(shù)接口示例詳解

    創(chuàng)建一個(gè)自定義的Sayable接口,這是一個(gè)使用@FunctionalInterface注解的函數(shù)式接口,這篇文章主要介紹了JAVA中的函數(shù)接口,你都用過嗎,需要的朋友可以參考下
    2023-11-11

最新評(píng)論