国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

在Java中發送郵件的一個相當完善的腳本

2019-11-18 13:13:53
字體:
來源:轉載
供稿:網友

  package com.asql.base;
  import java.util.*;
  import javax.mail.*;
  import javax.mail.internet.*;
  import javax.activation.*;
  
  
  public class MailSender {
  
   public final static boolean sendMail(LoadLog sendlog,String smtphost,String mailfrom,
   boolean mailauth,String mailuser,String mailpassWord,
   String mailto,String mailcc,String mailsubject,
   String mailmessage,String mailattach)
   {
   MimeMessage mimeMsg;
   MimeMultipart mp;    
   session session;  
   String sep[]={";"};
   PRoperties props=new java.util.Properties();  
   int i;
   props.put("mail.smtp.host",smtphost);
   try{
     session = Session.getDefaultInstance(props,null);
     mimeMsg = new MimeMessage(session);
     mp = new MimeMultipart();
     if(mailauth)
      props.put("mail.smtp.auth","true");
      else
       props.put("mail.smtp.auth","false");
     if (sendlog!=null)
   sendlog.println("Mail Host Address: "+smtphost);
   }
   catch(Exception e)
   {
     if (sendlog!=null)
      sendlog.println(e.getMessage());
     return false;
    }
   try
   {
     mimeMsg.setFrom(new InternetAddress(mailfrom));
     if (sendlog!=null)
      sendlog.println("Mail From Address: "+mailfrom);
   }
   catch(Exception e)
    {
     if (sendlog!=null)
      sendlog.println(e.getMessage());
     return false;
   }
   try{
     java.util.Vector temp = WordsConvert.getWords(mailto,sep);
     if (temp.size()==0)
     {
       if (sendlog!=null)
    sendlog.println("Mail Target Address Requried.");
   return false;
     }
     Address toaddress[] = new Address[temp.size()];
     for(i=0;i   toaddress[i]=InternetAddress.parse(temp.elementAt(i).toString())[0];
     mimeMsg.setRecipients(Message.RecipientType.TO,toaddress);
        if (sendlog!=null)
     sendlog.println("Mail To  Address: "+mailto);
   }
   catch(Exception e)
   {
        if (sendlog!=null)
     sendlog.println("Error Mail To,"+e);
     return false;
      }
      if(mailcc != null && mailcc.length()>0)
   {
   try{
     java.util.Vector temp = WordsConvert.getWords(mailcc,sep);
     if (temp.size()>0)
     {
      Address ccaddress[] = new Address[temp.size()];
      for(i=0;i    ccaddress[i]=InternetAddress.parse(temp.elementAt(i).toString())[0];
      mimeMsg.setRecipients(Message.RecipientType.CC,ccaddress);
         if (sendlog!=null)
      sendlog.println("Mail Cc  Address: "+mailcc);
     }
   }
   catch(Exception e)
   {
         if (sendlog!=null)
      sendlog.println(e.getMessage());
     return false;
       }
   }
   try
   {
    mimeMsg.setSubject(mailsubject,"GB2312");
   BodyPart bp = new MimeBodyPart();
   bp.setContent(""+
    mailmessage,"text/html;charset=GB2312");
   mp.addBodyPart(bp);
   }
   catch(Exception e)
   {
        if (sendlog!=null)
   sendlog.println(e.getMessage());
     return false;
    }
      if(mailattach != null && mailattach.length()>0)
   {
   try{
      java.util.Vector temp = WordsConvert.getWords(mailattach,sep);
      for(i=0;i      {
      MimeBodyPart bp = new MimeBodyPart();
      FileDataSource fileds = new FileDataSource(temp.elementAt(i).toString());
      DataHandler dh = new DataHandler(fileds);
      bp.setDisposition(Part.ATTACHMENT);
      bp.setFileName(fileds.getName());
      bp.setDataHandler(dh);
      mp.addBodyPart(bp);
      }
   }
   catch(Exception e)
   {
    if (sendlog!=null)
      sendlog.println(e.getMessage());
      return false;
   }
   }
   try{
     mimeMsg.setContent(mp);
     mimeMsg.saveChanges();
     Session mailSession = Session.getInstance(props,null);
     Transport transport = mailSession.getTransport("smtp");
     transport.connect((String)props.get("mail.smtp.host"),mailuser,mailpassword);
     transport.sendMessage(mimeMsg,mimeMsg.getAllRecipients());
     if (sendlog!=null)
      sendlog.println("Mail SUCcessfully Sended!");
     transport.close();
   }
   catch(Exception e)
   {
     if (sendlog!=null)
      sendlog.println(e.getMessage());;
     return false ;
   }
   return true;
    }
  }

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 临泽县| 齐齐哈尔市| 鞍山市| 自贡市| 炉霍县| 石狮市| 海原县| 云安县| 古蔺县| 南澳县| 合水县| 廊坊市| 海宁市| 濮阳县| 政和县| 额济纳旗| 浮梁县| 福海县| 淳化县| 金乡县| 资中县| 罗源县| 鞍山市| 寻乌县| 洞口县| 库车县| 民丰县| 尖扎县| 滦平县| 崇礼县| 武山县| 伊通| 九寨沟县| 静宁县| 泗水县| 革吉县| 泰和县| 鹤山市| 汽车| 静乐县| 英吉沙县|