總結一下java email發送郵件

一、首先在lib導入 commons-email-1.2.jar

發送郵件:

1、一個附件 發送給一個人

   HtmlEmail email = new HtmlEmail();

   email.setAuthenticator(new DefaultAuthenticator("自己的郵箱", "密碼"));
   email.setHostName(”smtp.qq.com“);

   //設置收件人
   email.addTo(”mailto(收件人的郵箱)", "收件人暱稱(可以爲空)");

   //設置發件人
   email.setFrom("自己的郵箱", "發件人暱稱(可以爲空)");

   //主題
   email.setSubject(subject);

   // 設置郵件正文和字符編碼
   StringBuffer bodyBf = new StringBuffer();
   bodyBf.append(content);
   email.addPart(bodyBf.toString(), "text/html;charset=utf-8");

   //附件
   if (!attachment.equals("")) {
    EmailAttachment attach = new EmailAttachment();
    attach.setPath(attachment);
    attach.setDisposition(EmailAttachment.ATTACHMENT);

    email.attach(attach);
   }

   email.send();

2、多個附件 多個收件人

HtmlEmail email = new HtmlEmail();
   email.setAuthenticator(new DefaultAuthenticator(“自己的郵箱","密碼"));
   email.setHostName("smtp.qq.com");
//   email.addTo(mailto);
   //設置發件人
   email.setFrom("自己的郵箱", "發件人暱稱(可以爲空)");

   //主題

   email.setSubject(subject);

   //設置多個收件人

   List<InternetAddress> inter=new ArrayList<InternetAddress>();
    inter.add(new InternetAddress(“****@qq.com”);

    inter.add(new InternetAddress(“******@qq.com”);

    email.setTo(inter);


   // 設置郵件正文和字符編碼
   StringBuffer bodyBf = new StringBuffer();
   bodyBf.append(content);
   email.addPart(bodyBf.toString(), "text/html;charset=utf-8");


   //添加多個附件
   List<String> sumpath=null;
   if (attachment!=null) {

   //通過附件上傳方法upFile() 得到附件的上傳後的路經(類型List<String>)
     sumpath=upFile();

    List<EmailAttachment> attachlist= new ArrayList<EmailAttachment>();
    for(int i=0;i<attachment.size();i++){
     EmailAttachment attach  = new EmailAttachment();
     attach.setPath(sumpath.get(i));

    //中文附近名 可以顯示
     attach.setName(MimeUtility.encodeText(attachmentFileName.get(i)));
     attach.setDisposition(EmailAttachment.ATTACHMENT);
     attachlist.add(attach);
    }
    for(int j=0;j<sumpath.size();j++){
     email.attach(attachlist.get(j));
    }
   }
   email.send();

補充上傳方法upFile()

 private List<File>  attachment;
 private InputStream fileStream;
 private List<String> attachmentFileName;  //上傳的文件名 (1.系統自動注入  2.變量命名有規則: 前臺對象名+"FileName")
 private List<String> attachmentContentType; //文件類型           (1.系統自動注入  2.變量命名有規則: 前臺對象名+"ContentType")

 String savepath="\\Email";

/**
  * 上傳多個文件
  * @return
  */
 private List<String> upFile(){
  
  String path = ServletActionContext.getServletContext().getRealPath(savepath) + "/" ;
  List<String> sumpath=new ArrayList<String>();
  File file = new File(path);
  if(!file.exists())
   {
     file.mkdirs();;
        System.out.println("目錄需要創建!!");
  }
   BufferedInputStream bis = null;
   BufferedOutputStream bos = null;
  if (attachment != null)
       {
           for (int i = 0; i < attachment.size(); i++)
           {
               try{
             bis = new BufferedInputStream(new FileInputStream(attachment.get(i)));
             bos = new BufferedOutputStream(new FileOutputStream(file.getPath()+"/"+ attachmentFileName.get(i)));
             byte[] buf = new byte[(int)attachment.get(i).length()];
             int len = 0;
             while(((len=bis.read(buf))!=-1)){
              bos.write(buf, 0, len);
             }
            }catch(Exception e){
             e.printStackTrace();
            }finally{
              try{
                if(bos!=null){
                 bos.close();
                }
                if(bis!=null){
                 bis.close();
                }
              }catch(Exception e){
             bos = null;
             bis = null;
              }
            }
           sumpath.add(file.getPath()+"\\"+ attachmentFileName.get(i));
           }
       }
  
  return sumpath;
  
 }

/**
  * 上傳一個文件
  * @return
  */
 private String upFile(){
  
  String path = ServletActionContext.getServletContext().getRealPath(savepath) + "/" + attachmentFileName;
  File file = new File(path);
  if(!file.exists())
   {
     file.mkdirs();;
        System.out.println("目錄需要創建!!");
  }
    BufferedInputStream bis = null;
    BufferedOutputStream bos = null;
   
    try{
     bis = new BufferedInputStream(new FileInputStream(attachment));
     bos = new BufferedOutputStream(new FileOutputStream(file.getPath()));
     byte[] buf = new byte[(int)attachment.length()];
     int len = 0;
     while(((len=bis.read(buf))!=-1)){
      bos.write(buf, 0, len);
     }
    }catch(Exception e){
     e.printStackTrace();
    }finally{
      try{
        if(bos!=null){
         bos.close();
        }
        if(bis!=null){
         bis.close();
        }
      }catch(Exception e){
     bos = null;
     bis = null;
      }
    }
  return path;
  
 }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章