Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1322723
  • 博文数量: 334
  • 博客积分: 10302
  • 博客等级: 上将
  • 技术积分: 2986
  • 用 户 组: 普通用户
  • 注册时间: 2006-01-12 10:17
文章分类

全部博文(334)

文章存档

2013年(1)

2012年(9)

2011年(4)

2010年(10)

2009年(24)

2008年(64)

2007年(72)

2006年(150)

我的朋友

分类: Java

2008-07-19 11:12:53

mail.java 代码

  1. package mail;  
  2.   
  3. import java.util.* ;  
  4. import java.io.* ;  
  5. import javax.mail.* ;  
  6. import javax.mail.internet.* ;  
  7. import javax.activation.* ;  
  8. public class Mail {  
  9.     //定义发件人、收件人、SMTP服务器、用户名、密码、主题、内容等  
  10.     private String displayName;  
  11.     private String to;  
  12.     private String from;  
  13.     private String smtpServer;  
  14.     private String username;  
  15.     private String password;  
  16.     private String subject;  
  17.     private String content;  
  18.     private boolean ifAuth; //服务器是否要身份认证  
  19.     private String filename="";  
  20.     private Vector file = new Vector(); //用于保存发送附件的文件名的集合  
  21.      
  22.      
  23.     /** 
  24.      * 设置SMTP服务器地址 
  25.      */  
  26.     public void setSmtpServer(String smtpServer){  
  27.         this.smtpServer=smtpServer;  
  28.     }  
  29.      
  30.     /** 
  31.      * 设置发件人的地址 
  32.      */  
  33.     public void setFrom(String from){  
  34.         this.from=from;  
  35.     }  
  36.     /** 
  37.      * 设置显示的名称 
  38.      */  
  39.     public void setDisplayName(String displayName){  
  40.         this.displayName=displayName;  
  41.     }  
  42.      
  43.     /** 
  44.      * 设置服务器是否需要身份认证 
  45.      */  
  46.     public void setIfAuth(boolean ifAuth){  
  47.         this.ifAuth=ifAuth;  
  48.     }  
  49.      
  50.     /** 
  51.      * 设置E-mail用户名 
  52.      */  
  53.     public void setUserName(String username){  
  54.         this.username=username;  
  55.     }  
  56.      
  57.     /** 
  58.      * 设置E-mail密码 
  59.      */  
  60.     public void setPassword(String password){  
  61.         this.password=password;  
  62.     }  
  63.      
  64.     /** 
  65.      * 设置接收者 
  66.      */  
  67.     public void setTo(String to){  
  68.         this.to=to;  
  69.     }  
  70.      
  71.     /** 
  72.      * 设置主题 
  73.      */  
  74.     public void setSubject(String subject){  
  75.         this.subject=subject;  
  76.     }  
  77.      
  78.     /** 
  79.      * 设置主体内容 
  80.      */  
  81.     public void setContent(String content){  
  82.         this.content=content;  
  83.     }  
  84.      
  85.     /** 
  86.      * 该方法用于收集附件名 
  87.      */  
  88.     public void addAttachfile(String fname){  
  89.         file.addElement(fname);  
  90.     }  
  91.      
  92.     public Mail(){  
  93.          
  94.     }  
  95.      
  96.     /** 
  97.      * 初始化SMTP服务器地址、发送者E-mail地址、用户名、密码、接收者、主题、内容 
  98.      */  
  99.     public Mail(String smtpServer,String from,String displayName,String username,String password,String to,String subject,String content){  
  100.         this.smtpServer=smtpServer;  
  101.         this.from=from;  
  102.         this.displayName=displayName;  
  103.         this.ifAuth=true;  
  104.         this.username=username;  
  105.         this.password=password;  
  106.         this.to=to;  
  107.         this.subject=subject;  
  108.         this.content=content;  
  109.     }  
  110.      
  111.     /** 
  112.      * 初始化SMTP服务器地址、发送者E-mail地址、接收者、主题、内容 
  113.      */  
  114.     public Mail(String smtpServer,String from,String displayName,String to,String subject,String content){  
  115.         this.smtpServer=smtpServer;  
  116.         this.from=from;  
  117.         this.displayName=displayName;  
  118.         this.ifAuth=false;  
  119.         this.to=to;  
  120.         this.subject=subject;  
  121.         this.content=content;  
  122.     }  
  123.   
  124.     /** 
  125.      * 发送邮件 
  126.      */  
  127.     public HashMap send(){  
  128.         HashMap map=new HashMap();  
  129.         map.put("state""success");  
  130.         String message="邮件发送成功!";  
  131.         Session session=null;  
  132.         Properties props = System.getProperties();  
  133.         props.put("mail.smtp.host", smtpServer);  
  134.         if(ifAuth){ //服务器需要身份认证  
  135.             props.put("mail.smtp.auth","true");     
  136.             SmtpAuth smtpAuth=new SmtpAuth(username,password);  
  137.             session=Session.getDefaultInstance(props, smtpAuth);   
  138.         }else{  
  139.             props.put("mail.smtp.auth","false");  
  140.             session=Session.getDefaultInstance(props, null);  
  141.         }  
  142.         session.setDebug(true);  
  143.         Transport trans = null;    
  144.         try {  
  145.             Message msg = new MimeMessage(session);   
  146.             try{  
  147.                 Address from_address = new InternetAddress(from, displayName);  
  148.                 msg.setFrom(from_address);  
  149.             }catch(java.io.UnsupportedEncodingException e){  
  150.                 e.printStackTrace();  
  151.             }  
  152.             InternetAddress[] address={new InternetAddress(to)};  
  153.             msg.setRecipients(Message.RecipientType.TO,address);  
  154.             msg.setSubject(subject);  
  155.             Multipart mp = new MimeMultipart();  
  156.             MimeBodyPart mbp = new MimeBodyPart();  
  157.             mbp.setContent(content.toString(), "text/html;charset=gb2312");  
  158.             mp.addBodyPart(mbp);    
  159.             if(!file.isEmpty()){//有附件  
  160.                 Enumeration efile=file.elements();  
  161.                 while(efile.hasMoreElements()){   
  162.                     mbp=new MimeBodyPart();  
  163.                     filename=efile.nextElement().toString(); //选择出每一个附件名  
  164.                     FileDataSource fds=new FileDataSource(filename); //得到数据源  
  165.                     mbp.setDataHandler(new DataHandler(fds)); //得到附件本身并至入BodyPart  
  166.                     mbp.setFileName(fds.getName());  //得到文件名同样至入BodyPart  
  167.                     mp.addBodyPart(mbp);  
  168.                 }    
  169.                 file.removeAllElements();      
  170.             }   
  171.             msg.setContent(mp); //Multipart加入到信件  
  172.             msg.setSentDate(new Date());     //设置信件头的发送日期  
  173.             //发送信件  
  174.             msg.saveChanges();   
  175.             trans = session.getTransport("smtp");  
  176.             trans.connect(smtpServer, username, password);  
  177.             trans.sendMessage(msg, msg.getAllRecipients());  
  178.             trans.close();  
  179.              
  180.         }catch(AuthenticationFailedException e){     
  181.              map.put("state""failed");  
  182.              message="邮件发送失败!错误原因:\n"+"身份验证错误!";  
  183.              e.printStackTrace();   
  184.         }catch (MessagingException e) {  
  185.              message="邮件发送失败!错误原因:\n"+e.getMessage();  
  186.              map.put("state""failed");  
  187.              e.printStackTrace();  
  188.              Exception ex = null;  
  189.              if ((ex = e.getNextException()) != null) {  
  190.                  System.out.println(ex.toString());  
  191.                  ex.printStackTrace();  
  192.              }   
  193.         }  
  194.         //System.out.println("\n提示信息:"+message);  
  195.         map.put("message", message);  
  196.         return map;  
  197.     }  
  198.      
  199. }  


SmtpAuth.java 代码
 
  1. package mail;  
  2.   
  3. public class SmtpAuth extends javax.mail.Authenticator {   
  4.     private String username,password;   
  5.   
  6.     public SmtpAuth(String username,String password){   
  7.         this.username = username;    
  8.         this.password = password;    
  9.     }   
  10.     protected javax.mail.PasswordAuthentication getPasswordAuthentication() {   
  11.         return new javax.mail.PasswordAuthentication(username,password);   
  12.     }   
  13. }   
阅读(1879) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~