Apache common-email 包发送带附件邮件

 public void sendMail(HttpServletRequest request, HttpServletResponse response)  
         throws  IOException {  
     System.out.println("doPost");  
     处理请求页面中文字符集  
     request.setCharacterEncoding("gbk");  
     response.setContentType("text/html; charset=gbk");  
     //得到表单中附件  
     File file = this.doAttachment(request);  
     //包含附件功能的邮件定义  
     MultiPartEmail email = new MultiPartEmail();  
     //设置邮件发送的服务器  
     email.setHostName("smtp.163.com");  
     //邮件发送验证用户名,密码  
     email.setAuthentication("用户名", "密码");  
     //设置邮件接收字符集  
     email.setCharset("gbk");  

     try {  
         //设置邮件信息,从表单中取得邮箱发送人、接收人、主题、内容  
         email.addTo(parameters.get("to"));  
         email.setFrom(parameters.get("from"));  
         email.setSubject(parameters.get("subject"));  
         email.setMsg(parameters.get("content"));  
         if (file != null) {  
             //邮件添加附件,设置邮件附件的路径、类型、名称  
             EmailAttachment attachment = new EmailAttachment();  
             attachment.setPath(file.getPath());  
             attachment.setDisposition(EmailAttachment.ATTACHMENT);  
             attachment.setName(file.getName());  
             email.attach(attachment);  
         }  
         //发送邮件  
         email.send();  
         request.setAttribute("sendmail.message", "success");  
     } catch (EmailException e) {  
         e.printStackTrace();  
         request.setAttribute("sendmail.message", "error");  
     }  
     //转发到结果页面  
     request.getRequestDispatcher("/sendResult.jsp").forward(request,  
             response);  

 }  


 

所使用的JAr包已经当做附件上传。需要mail.jar 与Activation.jar 

你可能感兴趣的:(apache)