您现在的位置是:首页 >

手机邮件怎么发送邮件 在Java中发送邮件的一个相当完善的脚本

火烧 2021-09-11 11:37:16 1044
在Java中发送邮件的一个相当完善的脚本    ackage a ql a e   im ort java util *   im ort javax mail *   im ort javax ma

在Java中发送邮件的一个相当完善的脚本  

手机邮件怎么发送邮件 在Java中发送邮件的一个相当完善的脚本
  package 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()== )     {       if (sendlog!=null)    sendlog println( Mail Target Address Requried );   return false;     }     Address toaddress[] = new Address[temp size()];     for(i= ;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()> )   {   try{     java util Vector temp = WordsConvert getWords(mailcc sep);     if (temp size()> )     {      Address ccaddress[] = new Address[temp size()];      for(i= ;i size();i++)    ccaddress[i]=InternetAddress parse(temp elementAt(i) toString())[ ];      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 GB );   BodyPart bp = new MimeBodyPart();   bp setContent( +    mailmessage text/;charset=GB );   mp addBodyPart(bp);   }   catch(Exception e)    {        if (sendlog!=null)   sendlog println(e getMessage());     return false;    }      if(mailattach != null && mailattach length()> )   {   try{      java util Vector temp = WordsConvert getWords(mailattach sep);      for(i= ;i size();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 );     nnect((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;    }  }   lishixinzhi/Article/program/Java/Javascript/201311/25339 ();i++)
  
永远跟党走
  • 如果你觉得本站很棒,可以通过扫码支付打赏哦!

    • 微信收款码
    • 支付宝收款码