熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Javascript >> 正文

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

2022-06-13   來源: Javascript 

  package comasqlbase;
  import javautil*;
  import javaxmail*;
  import javaxmailinternet*;
  import javaxactivation*;
  
  
  public class MailSender {
  
   public final static boolean sendMail(LoadLog sendlogString smtphostString mailfrom
   boolean mailauthString mailuserString mailpassword
   String mailtoString mailccString mailsubject
   String mailmessageString mailattach)
   {
   MimeMessage mimeMsg;
   MimeMultipart mp;    
   Session session;  
   String sep[]={;};
   Properties props=new javautilProperties();  
   int i;
   propsput(mailsmtphostsmtphost);
   try{
     session = SessiongetDefaultInstance(propsnull);
     mimeMsg = new MimeMessage(session);
     mp = new MimeMultipart();
     if(mailauth)
      propsput(mailsmtpauthtrue);
      else
       propsput(mailsmtpauthfalse);
     if (sendlog!=null)
   sendlogprintln(Mail Host Address: +smtphost);
   }
   catch(Exception e)
   {
     if (sendlog!=null)
      sendlogprintln(egetMessage());
     return false;
    }
   try
   {
     mimeMsgsetFrom(new InternetAddress(mailfrom));
     if (sendlog!=null)
      sendlogprintln(Mail From Address: +mailfrom);
   }
   catch(Exception e)
    {
     if (sendlog!=null)
      sendlogprintln(egetMessage());
     return false;
   }
   try{
     javautilVector temp = WordsConvertgetWords(mailtosep);
     if (tempsize()==)
     {
       if (sendlog!=null)
    sendlogprintln(Mail Target Address Requried);
   return false;
     }
     Address toaddress[] = new Address[tempsize()];
     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{
     javautilVector temp = WordsConvertgetWords(mailccsep);
     if (tempsize()>)
     {
      Address ccaddress[] = new Address[tempsize()];
      for(i=;isize();i++)
    ccaddress[i]=InternetAddressparse(tempelementAt(i)toString())[];
      mimeMsgsetRecipients(MessageRecipientTypeCCccaddress);
         if (sendlog!=null)
      sendlogprintln(Mail Cc  Address: +mailcc);
     }
   }
   catch(Exception e)
   {
         if (sendlog!=null)
      sendlogprintln(egetMessage());
     return false;
       }
   }
   try
   {
    mimeMsgsetSubject(mailsubjectGB);
   BodyPart bp = new MimeBodyPart();
   bpsetContent(+
    mailmessagetext/html;charset=GB);
   mpaddBodyPart(bp);
   }
   catch(Exception e)
   {
        if (sendlog!=null)
   sendlogprintln(egetMessage());
     return false;
    }
      if(mailattach != null && mailattachlength()>)
   {
   try{
      javautilVector temp = WordsConvertgetWords(mailattachsep);
      for(i=;isize();i++)
      {
      MimeBodyPart bp = new MimeBodyPart();
      FileDataSource fileds = new FileDataSource(tempelementAt(i)toString());
      DataHandler dh = new DataHandler(fileds);
      bpsetDisposition(PartATTACHMENT);
      bpsetFileName(filedsgetName());
      bpsetDataHandler(dh);
      mpaddBodyPart(bp);
      }
   }
   catch(Exception e)
   {
    if (sendlog!=null)
      sendlogprintln(egetMessage());
      return false;
   }
   }
   try{
     mimeMsgsetContent(mp);
     mimeMsgsaveChanges();
     Session mailSession = SessiongetInstance(propsnull);
     Transport transport = mailSessiongetTransport(smtp);
     nnect((String)propsget(mailsmtphost)mailusermailpassword);
     transportsendMessage(mimeMsgmimeMsggetAllRecipients());
     if (sendlog!=null)
      sendlogprintln(Mail Successfully Sended!);
     transportclose();
   }
   catch(Exception e)
   {
     if (sendlog!=null)
      sendlogprintln(egetMessage());;
     return false ;
   }
   return true;
    }
  }
  

From:http://tw.wingwit.com/Article/program/Java/Javascript/201311/25339.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.