欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java實(shí)現(xiàn)系統(tǒng)捕獲異常發(fā)送郵件案例

 更新時間:2016年11月02日 16:09:20   作者:薔薇玫瑰  
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)系統(tǒng)捕獲異常發(fā)送郵件案例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

在實(shí)際運(yùn)用中,比如你淘寶購物,申請退款,這時你在郵箱中看到退款郵件,或者你注冊某個賬號,申請驗(yàn)證郵箱通知等等,這些都是郵件發(fā)送,這里將介紹下系統(tǒng)捕獲異常發(fā)送郵件案例。

準(zhǔn)備工作:
eclipse4.5 64位
jdk1.7 64位

郵件發(fā)送所需jar:

fastjson-1.1.24.jar,javax.mail-1.5.6.jar

類Developer:

枚舉類型,發(fā)送郵件人姓名和郵箱地址

package mail; 
/** 
 * @class:Developer 
 *@descript:枚舉類型,發(fā)送郵件人姓名和郵箱地址 
 *@date:2016年10月26日 下午8:07:50 
 *@author sanghaiqin 
 *@version:V1.0 
 */ 
public enum Developer { 
  zhoujing("周靜","405687038@qq.com"), 
  peiyuxiang("裴玉翔","498736875@qq.com"), 
  yipeng("乙鵬","729325112@qq.com"), 
  liuan("劉安","2211747233@qq.com"), 
  chenyuhao("陳宇豪","631604198@qq.com"), 
  wangdong("王棟","1217295649@qq.com"), 
  sanghaiqin("桑海芹","1522580013@qq.com"); 
   
  //發(fā)件人姓名 
  private String name; 
  //發(fā)件人email 
  private String mail; 
   
  private Developer() { 
     
  } 
   
  private Developer(String name, String mail) { 
    this.name = name; 
    this.mail = mail; 
  } 
   
  /** 
   * @descript:傳遞發(fā)件人姓名得到該發(fā)件人的郵箱 
   * @param name 發(fā)件人姓名 
   * @return 
   */ 
  public static String getMail(String name) { 
    for (Developer c : Developer.values()) { 
      if (c.getName().equals(name)) { 
        return c.mail; 
      } 
    } 
    return null; 
  } 
   
  public String getName() { 
    return name; 
  } 
  public void setName(String name) { 
    this.name = name; 
  } 
  public String getMail() { 
    return mail; 
  } 
  public void setMail(String mail) { 
    this.mail = mail; 
  } 
   
} 

類ExceptionInfo:發(fā)件人信息

package mail; 
/** 
 * @class:ExceptionInfo 
 *@descript:發(fā)件人信息 
 *@date:2016年10月26日 下午8:11:27 
 *@author sanghaiqin 
 *@version:V1.0 
 */ 
public class ExceptionInfo { 
 
  //發(fā)件人姓名 
  private String developer; 
  //發(fā)件人方法 
  private String method; 
  //發(fā)件人url 
  private String url; 
  //發(fā)件人捕獲異常信息 
  private Exception e; 
   
  /** 
   * @param developer 發(fā)件人姓名 
   * @param method 發(fā)件人方法 
   * @param url 發(fā)件人url 
   * @param e 發(fā)件人捕獲異常信息 
   */ 
  public ExceptionInfo(String developer, String method, String url, Exception e) { 
    super(); 
    this.developer = developer; 
    this.method = method; 
    this.url = url; 
    this.e = e; 
  } 
  public String getDeveloper() { 
    return developer; 
  } 
  public void setDeveloper(String developer) { 
    this.developer = developer; 
  } 
  public String getMethod() { 
    return method; 
  } 
  public void setMethod(String method) { 
    this.method = method; 
  } 
  public String getUrl() { 
    return url; 
  } 
  public void setUrl(String url) { 
    this.url = url; 
  } 
  public Exception getE() { 
    return e; 
  } 
  public void setE(Exception e) { 
    this.e = e; 
  } 
   
   
} 

類MailSenderInfo:發(fā)送郵箱信息

package mail; 
 
import java.util.Properties; 
/** 
 * @class:MailSenderInfo 
 *@descript:發(fā)送郵箱信息 
 *@date:2016年10月26日 下午8:14:22 
 *@author sanghaiqin 
 *@version:V1.0 
 */ 
public class MailSenderInfo { 
  //發(fā)送郵件的服務(wù)器的IP 
  private String mailServerHost; 
  //發(fā)送郵件的服務(wù)器的端口默認(rèn)為25 
  private String mailServerPort = "25"; 
  // 郵件發(fā)送者的地址 
  private String fromAddress; 
  // 郵件接收者的地址 
  private String toAddress; 
  // 登陸郵件發(fā)送服務(wù)器的用戶名 
  private String username; 
  // 登陸郵件發(fā)送服務(wù)器的密碼 
  private String password; 
  // 是否需要身份驗(yàn)證 
  private boolean validate = false; 
  // 郵件主題 
  private String subject; 
  // 郵件的文本內(nèi)容 
  private String content; 
  // 郵件附件的文件名 
  private String[] attachFileNames; 
   
  public MailSenderInfo() { 
    super(); 
  } 
 
  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; 
  } 
   
  /** 
   * @descript:獲得郵件會話屬性 
   * @return 
   */ 
  public Properties getProperties() { 
    PropertyUtil propertyUtil = new PropertyUtil(); 
    Properties properties =propertyUtil.readProperties(); 
    return properties; 
  } 
 
   
   
} 

類MyAuthenticator:用戶驗(yàn)證

package mail; 
 
import javax.mail.Authenticator; 
import javax.mail.PasswordAuthentication; 
/** 
 * @class:MyAuthenticator 
 *@descript:用戶驗(yàn)證 
 *@date:2016年10月26日 下午8:57:45 
 *@author sanghaiqin 
 *@version:V1.0 
 */ 
public class MyAuthenticator extends Authenticator { 
  //用戶名 
  String username = null; 
  //密碼 
  String password = null; 
 
  public MyAuthenticator() { 
     
  } 
 
  public MyAuthenticator(String username, String password) { 
    this.username = username; 
    this.password = password; 
  } 
 
  protected PasswordAuthentication getPasswordAuthentication() { 
    return new PasswordAuthentication(username, password); 
  } 
   
} 

類PropertyUtil:獲得properties文件工具類

package mail; 
 
import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.io.IOException; 
import java.io.InputStream; 
import java.util.Properties; 
/** 
 * @class:PropertyUtil 
 *@descript:獲得properties文件工具類 
 *@date:2016年10月26日 下午8:20:10 
 *@author sanghaiqin 
 *@version:V1.0 
 */ 
public class PropertyUtil { 
  /** 
   * @descript:加載資源文件 
   * @param resources 資源文件 
   * @return 
   * @throws FileNotFoundException 
   */ 
  private Properties loadProperties(String resources) { 
    InputStream inputstream = null; 
    Properties properties = new Properties(); 
    // 使用InputStream得到一個資源文件 
    try { 
      inputstream = new FileInputStream(resources); 
       // 加載配置文件 
       properties.load(inputstream); 
    } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
    } catch (IOException e) { 
      e.printStackTrace(); 
    }finally{ 
      if(inputstream!=null){ 
        try { 
          inputstream.close(); 
        } catch (IOException e) { 
          e.printStackTrace(); 
        }  
      } 
    } 
    return properties; 
  } 
   
  /** 
   * @descript:讀屬性文件 
   * @return 
   * @throws FileNotFoundException 
   */ 
  public Properties readProperties(){ 
    String resources = PropertyUtil.class.getClassLoader().getResource("prop.properties").getPath(); 
    Properties properties = loadProperties(resources); 
    return properties; 
  } 
   
  /** 
   * @descript:測試 
   * @param args 
   */ 
  public static void main(String[] args) { 
    PropertyUtil p=new PropertyUtil(); 
    Properties pro=p.readProperties(); 
    String mailSenderUsername=(String) pro.get("mail.sender.username"); 
    System.out.println("郵件發(fā)送者用戶名:"+mailSenderUsername);//neo_lifes@163.com 
    String path = PropertyUtil.class.getClassLoader().getResource("prop.properties").getPath(); 
    System.out.println(path);// /G:/Workspaces4.4/test/bin/prop.properties 
  } 
   
} 

資源文件pro.properties:

#-------------------郵件功能------------------ 
#----------------這兩個是構(gòu)建session必須的字段---------- 
#smtp服務(wù)器,構(gòu)建session回話必須的字段 
mail.smtp.host=smtp.163.com 
#身份驗(yàn)證,構(gòu)建session回話必須的字段 
mail.smtp.auth=true 
#-------------------------------------------------------------- 
#發(fā)送者的郵箱用戶名 
mail.sender.username=neo_lifes@163.com 
#發(fā)送者的郵箱密碼 
mail.sender.password=827623LIU 
#發(fā)送者的郵箱地址 
mail.sender.address=neo_lifes@163.com 

類JavaMail:發(fā)送郵箱

package mail; 
 
import java.io.PrintWriter; 
import java.io.StringWriter; 
import java.io.UnsupportedEncodingException; 
import java.net.InetAddress; 
import java.net.UnknownHostException; 
import java.util.Date; 
import java.util.Properties; 
 
import javax.activation.DataHandler; 
import javax.activation.DataSource; 
import javax.activation.FileDataSource; 
import javax.mail.Address; 
import javax.mail.BodyPart; 
import javax.mail.Message; 
import javax.mail.MessagingException; 
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; 
import javax.mail.internet.MimeUtility; 
 
/** 
 * @class:JavaMail 
 *@descript:發(fā)送信息郵箱 
 *所需jar包: 
 *fastjson-1.1.24.jar 
 *javax.mail-1.5.6.jar 
 *@date:2016年10月26日 下午8:13:05 
 *@author sanghaiqin 
 *@version:V1.0 
 */ 
public class JavaMail { 
   
  public static void sendExceptionMail(ExceptionInfo info){ 
    try { 
      //通過發(fā)送者獲得發(fā)送者郵箱 
      String mail = Developer.getMail(info.getDeveloper()); 
      if(mail!=null){ 
        MailSenderInfo mailInfo = new MailSenderInfo(); 
        //設(shè)置郵件的文本內(nèi)容 
        mailInfo.setContent("負(fù)責(zé)人 : "+info.getDeveloper()+"==>服務(wù)器 ip:"+InetAddress.getLocalHost().getHostAddress()+"==>方法名: "+info.getMethod()+"==>地址:"+info.getUrl()+"==>異常信息: "+getEmessage(info.getE())); 
        //設(shè)置郵件接收者的地址 
        mailInfo.setToAddress(mail); 
        //郵件主題 
        mailInfo.setSubject("易卡愛途異常通知"); 
        //發(fā)送郵件 
        sendTextMail(mailInfo); 
      } 
    } catch (UnknownHostException e) { 
      e.printStackTrace(); 
    } 
  } 
   
  /** 
   * @descript:以文本格式發(fā)送郵件 
   * @param: mailInfo 待發(fā)送的郵件的信息 
   * @return: 發(fā)送成功返回true;失敗返回false 
   */ 
  public static boolean sendTextMail(MailSenderInfo mailInfo) { 
    // 判斷是否需要身份認(rèn)證 
    MyAuthenticator authenticator = null; 
    Properties pro = mailInfo.getProperties(); 
    try {  
      if ("true".trim().equals(pro.getProperty("mail.smtp.auth"))) { 
        // 如果需要身份認(rèn)證,則創(chuàng)建一個密碼驗(yàn)證器 
        authenticator = new MyAuthenticator(pro.getProperty("mail.sender.username"),pro.getProperty("mail.sender.password")); 
      } 
      // 根據(jù)郵件會話屬性和密碼驗(yàn)證器構(gòu)造一個發(fā)送郵件的session 
      Session sendMailSession = Session.getDefaultInstance(pro, authenticator); 
      // 根據(jù)session創(chuàng)建一個郵件消息 
      Message mailMessage = new MimeMessage(sendMailSession); 
      // 創(chuàng)建郵件發(fā)送者地址 
      Address from = new InternetAddress(pro.getProperty("mail.sender.address")); 
      // 設(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ā)送的時間 
      mailMessage.setSentDate(new Date()); 
      // 設(shè)置郵件消息的主要內(nèi)容 
      mailMessage.setText(mailInfo.getContent()); 
      // 發(fā)送郵件 
      Transport.send(mailMessage); 
      return true; 
    } catch (MessagingException ex) { 
      ex.printStackTrace(); 
    } 
    return false; 
  } 
 
  /** 
   * @descript:以HTML格式發(fā)送郵件 
   * @param mailInfo: 待發(fā)送的郵件的信息 
   * @param attachment:附件內(nèi)容 
   * @return:發(fā)送成功返回true;失敗返回false 
   */ 
  public static boolean sendHtmlMail(MailSenderInfo mailInfo, String[] attachment) { 
    // 判斷是否需要身份認(rèn)證 
    MyAuthenticator authenticator = null; 
    Properties pro = mailInfo.getProperties(); 
    try { 
      // 如果需要身份認(rèn)證,則創(chuàng)建一個密碼驗(yàn)證器 
      if ("true".trim().equals(pro.getProperty("mail.smtp.auth"))) { 
        // 如果需要身份認(rèn)證,則創(chuàng)建一個密碼驗(yàn)證器 
        authenticator = new MyAuthenticator(pro.getProperty("mail.sender.username"),pro.getProperty("mail.sender.password")); 
      } 
      // 根據(jù)郵件會話屬性和密碼驗(yàn)證器構(gòu)造一個發(fā)送郵件的session 
      Session sendMailSession = Session.getDefaultInstance(pro, authenticator); 
      // 根據(jù)session創(chuàng)建一個郵件消息 
      Message mailMessage = new MimeMessage(sendMailSession); 
      // 創(chuàng)建郵件發(fā)送者地址 
      Address from = new InternetAddress(pro.getProperty("mail.sender.address")); 
      // 設(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ā)送的時間 
      mailMessage.setSentDate(new Date()); 
      // MiniMultipart類是一個容器類,包含MimeBodyPart類型的對象 
      Multipart mainPart = new MimeMultipart(); 
      // 創(chuàng)建一個包含HTML內(nèi)容的MimeBodyPart 
      BodyPart html = new MimeBodyPart(); 
      // 設(shè)置HTML內(nèi)容 
      html.setContent(mailInfo.getContent(), "text/html; charset=utf-8"); 
      //添加HTML內(nèi)容的MimeBodyPart 
      mainPart.addBodyPart(html); 
      // 添加附件的內(nèi)容 
      if (attachment != null) { 
        for (String filePath : attachment) { 
          MimeBodyPart filePart = new MimeBodyPart(); 
          DataSource source = new FileDataSource(filePath); 
          filePart.setDataHandler(new DataHandler(source)); 
          try { 
            // 網(wǎng)上流傳的解決文件名亂碼的方法,其實(shí)用MimeUtility.encodeWord就可以很方便的搞定 
            filePart.setFileName(MimeUtility.encodeWord(source.getName())); 
          } catch (UnsupportedEncodingException e) { 
            e.printStackTrace(); 
          } 
          mainPart.addBodyPart(filePart); 
        } 
      } 
      // 將MiniMultipart對象設(shè)置為郵件內(nèi)容 
      mailMessage.setContent(mainPart); 
      //保持內(nèi)容 
      mailMessage.saveChanges(); 
      // 發(fā)送郵件 
      Transport.send(mailMessage); 
      return true; 
    } catch (MessagingException ex) { 
      ex.printStackTrace(); 
    } 
    return false; 
  } 
   
   
  /** 
   * @descript:獲得發(fā)送者方法的異常信息 
   * 使用字符串作為物理節(jié)點(diǎn)的字符輸入輸出流的用法,即StringReader和StringWriter的用法 
   * PrintWriter(Writer out, boolean autoFlush) 創(chuàng)建帶自動行刷新的新 PrintWriter, true代表能自動刷新 
   * @param e 異常信息 
   * @return 
   */ 
  private static String getEmessage(Exception e){   
    //StringWriter輸出異常信息 
    StringWriter sw = new StringWriter();   
    PrintWriter pw = new PrintWriter(sw, true);   
    e.printStackTrace(pw);   
    pw.flush();   
    sw.flush();   
    return sw.toString();   
  }  
   
  /** 
   * @descript:測試 
   * @param args 
   */ 
  public static void main(String[] args) { 
    //測試1:發(fā)送郵件以文本格式 
    try { 
      String s=""; 
      s.substring(2); 
    } catch (Exception e) { 
      e.printStackTrace(); 
      System.out.println(getEmessage(e)); 
      sendExceptionMail(new ExceptionInfo( Developer.sanghaiqin.getName(),"get()", "123",e)); 
    } 
     
    //測試2:發(fā)送郵件以html格式 
    MailSenderInfo mailInfo = new MailSenderInfo(); 
    mailInfo.setToAddress("1522580013@qq.com");       // 設(shè)置接受者郵箱地址 
    mailInfo.setSubject("標(biāo)題");  
    mailInfo.setContent("內(nèi)容<h1>www.baidu.com</h1>"); 
    String[] files = {"G:/upload/image/2016/10/28/1477372845440.jpg","G:/upload/image/2016/10/28/1477372845660.jpg"}; 
    JavaMail.sendHtmlMail(mailInfo,files); // 發(fā)送html格式 
     
    System.out.println("發(fā)送成功"); 
     
     
  } 
   
} 

測試截圖:

測試1:發(fā)送郵件以文本格式:

測試2:發(fā)送郵件以html格式:


項(xiàng)目結(jié)構(gòu)截圖:

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

相關(guān)文章

  • Scala之文件讀取、寫入、控制臺操作的方法示例

    Scala之文件讀取、寫入、控制臺操作的方法示例

    這篇文章主要介紹了Scala之文件讀取、寫入、控制臺操作的方法示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-06-06
  • Java Socket編程(二) Java面向連接的類

    Java Socket編程(二) Java面向連接的類

    Java Socket編程(二) Java面向連接的類...
    2006-12-12
  • Java中實(shí)現(xiàn)接口限流的方案詳解

    Java中實(shí)現(xiàn)接口限流的方案詳解

    常用的接口限流方案就是計(jì)數(shù)器限流方案、時間窗口限流方案和令牌桶限流方案等,這些方案的概念大致也知道,但是實(shí)際上也沒有實(shí)現(xiàn)過,所以本文就來自動動手實(shí)踐一下吧
    2023-05-05
  • Mybatis-Plus的saveOrUpdateBatch(null)問題及解決

    Mybatis-Plus的saveOrUpdateBatch(null)問題及解決

    這篇文章主要介紹了Mybatis-Plus的saveOrUpdateBatch(null)問題及解決方案,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-07-07
  • Java線程的并發(fā)工具類實(shí)現(xiàn)原理解析

    Java線程的并發(fā)工具類實(shí)現(xiàn)原理解析

    本文給大家講解Java線程的并發(fā)工具類的一些知識,通過適用場景分析大數(shù)據(jù)量統(tǒng)計(jì)類任務(wù)的實(shí)現(xiàn)原理和封裝,多個示例代碼講解的非常詳細(xì),對java線程并發(fā)工具類相關(guān)知識感興趣的朋友一起學(xué)習(xí)下吧
    2021-06-06
  • 基于java實(shí)現(xiàn)斗地主代碼實(shí)例解析

    基于java實(shí)現(xiàn)斗地主代碼實(shí)例解析

    這篇文章主要介紹了基于java實(shí)現(xiàn)斗地主代碼實(shí)例解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-07-07
  • java?-jar啟動參數(shù)設(shè)置file.encoding編碼,解決中文亂碼的問題

    java?-jar啟動參數(shù)設(shè)置file.encoding編碼,解決中文亂碼的問題

    這篇文章主要介紹了java?-jar啟動參數(shù)設(shè)置file.encoding編碼,解決中文亂碼的問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-07-07
  • Java PreparedStatement用法詳解

    Java PreparedStatement用法詳解

    這篇文章主要介紹了Java PreparedStatement用法詳解,本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-08-08
  • Maven搭建springboot項(xiàng)目的方法步驟

    Maven搭建springboot項(xiàng)目的方法步驟

    這篇文章主要介紹了Maven搭建springboot項(xiàng)目的方法步驟,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-04-04
  • Java 阻塞隊(duì)列詳解及簡單使用

    Java 阻塞隊(duì)列詳解及簡單使用

    這篇文章主要介紹了Java 阻塞隊(duì)列詳解及簡單使用的相關(guān)資料,需要的朋友可以參考下
    2017-02-02

最新評論