javamail发送带附件并且加密的邮件

来源:互联网 发布:淘宝达人推荐 编辑:程序博客网 时间:2024/06/17 22:25

给客户用javamail的方式实现了邮件的发送,但是为了保证数据的安全,客户提出发送邮件时应该简单加密一下(有个固定设置的6位的密码)用附件形式。
其中邮件发送我用到了activation.jar、commons-email-1.0.jar、mail.jar这三个jar包。

实现方法:先获取日志数据,生成txt文件,然后再调用rar命令生成加密的rar文件,之后再将rar文件作为邮件附件进行发送。

(1)、生成txt文件。

/**
 * path===文件夹路径
 * filePathAndName===文件路径
 * content===文件内容
 * 功能://写入文本文件
 */
 public void write(String path,  String filePathAndName, String content) {
   // String basePath = request.getRealPath("/");
    //String path = basePath + "upload/"+newFileName.substring(4,12);
   // String filePath = path+"/"+newFileName;
       String s = new String();
       String s1 = new String();
       try {
      File upDir=new File(path); //得到上传文件夹
        if(!upDir.exists())
         upDir.mkdirs();//目录不存在就创建
      File f = new File(filePathAndName);
        if (f.exists()) {
         System.out.println("文件存在");
        } else {
         System.out.println("文件不存在,正在创建...");
         if (f.createNewFile()) {
          System.out.println("文件创建成功!");
         } else {
          System.out.println("文件创建失败!");
         }

        }
        BufferedReader input = new BufferedReader(new FileReader(f));

        while ((s = input.readLine()) != null) {
         s1 += s + "/r/n";
        }
        input.close();
        s1 += content;

        BufferedWriter output = new BufferedWriter(new FileWriter(f));
        output.write(s1);
        output.close();
      
       } catch (Exception e) {
        e.printStackTrace();
       }  
 }

(2)java调用rar命令生成加密的rar文件

 /**
  * 压缩命令具体执行
  * @param info
  * @return
  */
 private static boolean execute(String rarPath,String path,String title){
  try {   
   //C:/Program Files/WinRAR>rar a F://upload/1.rar F://upload//20100806.txt -p123 

  //rarPath=C:/Program Files/WinRAR/Rar.exe
   String strCmd = rarPath+" a "+path+title+".rar "+ path+title+".txt"+" -p123 ";
   Runtime run = Runtime.getRuntime();
         Process process = run.exec(strCmd);          
         int iRtn = process.waitFor(); //30秒         
         if(iRtn==0){
          process.destroy();            
          return true;      
         }
  }
      catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace(); 
   return false;
  }
   return true;
 }

(3)、用附件形式发送

MultiPartEmail email = new MultiPartEmail();//建立邮件发送类

//邮件发送开始====
   email.setHostName(mail_host);//设置发送方的主机
   email.setAuthentication(mail_username, mail_password);//设置发送方的用户名和密码
   
   //email.setCharset("UTF-8");
   email.setCharset("GBK");
   
   email.addTo(toMail, toMail_jx); // .addTo(parameters.get("to"));     
   email.setFrom(mail_username, mail_username_jx); //.setFrom(parameters.get("from"));
   
   email.setSubject(title);
   email.setMsg(title);
   
   if (file!=null) {
    EmailAttachment eAttachment = new EmailAttachment();
    eAttachment.setPath(file.getPath());//设置附件的路径
    eAttachment.setDescription(EmailAttachment.ATTACHMENT);//    
    String filename = new String(file.getName().getBytes("GBK"),"iso-8859-1");//解决附件乱码
    eAttachment.setName(MimeUtility.encodeWord(file.getName())); //解决附件乱码、设置附件发送的名字
    //eAttachment.setName(filename);//设置附件发送的名字
    email.attach(eAttachment);
   }
   email.send(); //执行发送操作
   // 邮件发送结束====

(4)删除掉生成的临时文件:

   //删除掉生成的文件。
   delFile(filePathAndName);
   delFile(rarPathAndName);

    /**
     * 删除文件
     * @param filePathAndName String 文件路径及名称 如c:/fqf.txt
     * @param fileContent String
     * @return boolean
     */
    public void delFile(String filePathAndName) {
      try {
        String filePath = filePathAndName;
        filePath = filePath.toString();
        java.io.File myDelFile = new java.io.File(filePath);
        if(myDelFile.exists())
        myDelFile.delete();

      }
      catch (Exception e) {
        System.out.println("删除文件操作出错");
        e.printStackTrace();
      }
    }

原创粉丝点击