服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

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


  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;
    }
  }

扫描关注微信公众号