heting

  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  40 随笔 :: 9 文章 :: 45 评论 :: 0 Trackbacks

import org.apache.commons.mail.*;  
 
public class test {  
     
//程序主方法  
public static void main(String[] args)throws Exception {  
   test as
=new test();  
     String host 
= "smtp.sina.com";  
         String from 
= "javalihan@sina.com";  
         String username 
= "javalihan";  
         String password 
= "javame";  
         
//接收者邮箱  
         String to = "lbl2006@qq.com";  
         String subject
="这是测试主题!";  
         String mailConent
="这是测试邮件";  
          
//调用发送附件邮件方法  
   as.sendAttachmentMail(host, from, username, password, to, subject, mailConent);  
}
  

   
public boolean sendAttachmentMail(String host,String from,String username,  
    String password,String to,String subject,String mailConent)
throws Exception{  
       
//创建附件对象  
    EmailAttachment attachment = new EmailAttachment();  
                
/*附件的地址*/       
    attachment.setPath(
"E:\\commons-email-1.0.rar");    
                      
//设定为附件  
          attachment.setDisposition(EmailAttachment.ATTACHMENT);  
          
/*附件的描述*/  
          attachment.setDescription(
"jPortMap项目设计附件文档");  
          
/*附件的名称,必须和文件名一致*/  
          attachment.setName(
"Eclipse中文教程.pdf");  
          
/*new一个HtmlEmail发送对象*/  
    HtmlEmail email 
= new HtmlEmail();    
       email.setAuthentication(username, password);  
    email.setHostName(host);  
    email.addTo(to, from);  
    email.setFrom(from);  
    email.setSubject(subject);  
          
//注意,发送内容时,后面这段会让中文正常显示,否则乱码  
    email.setCharset("GB2312");  
    email.setHtmlMsg(
"<html>这是封测试附件邮件</html>"); /*邮件内容*/  
          
//添加附件对象  
    email.attach(attachment);  
    
//发送  
    email.send();  
    System.out.println(
"带符件的邮件发送成功!");  
    
return true;  

}
  
}
 
posted on 2008-12-20 08:48 贺挺 阅读(126) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航: