Jmail的简单应用

java 代码
  1. package bruce;   
  2.   
  3. import java.util.Date;   
  4. import java.util.Enumeration;   
  5. import java.util.Properties;   
  6. import java.util.Vector;   
  7.   
  8. import javax.activation.DataHandler;   
  9. import javax.activation.FileDataSource;   
  10. import javax.mail.Authenticator;   
  11. import javax.mail.Message;   
  12. import javax.mail.MessagingException;   
  13. import javax.mail.Multipart;   
  14. import javax.mail.PasswordAuthentication;   
  15. import javax.mail.Session;   
  16. import javax.mail.Transport;   
  17. import javax.mail.internet.InternetAddress;   
  18. import javax.mail.internet.MimeBodyPart;   
  19. import javax.mail.internet.MimeMessage;   
  20. import javax.mail.internet.MimeMultipart;   
  21. import javax.mail.internet.MimeUtility;   
  22.   
  23. public class SendMail {   
  24.     String to = "";//收件人   
  25.     String from = "";//发件人   
  26.     String smtpServer = "";//smtp服务器   
  27.     String username = "" ;   
  28.     String password = "" ;   
  29.     String filename = "";//附件文件名   
  30.     String subject = "";//邮件主题   
  31.     String content = "";//邮件正文   
  32.     Vector file = new Vector();//附件文件集合   
  33.   
  34.     public SendMail()   
  35.     {   
  36.     }   
  37.     public SendMail(String to,String from,String smtpServer,String username,String password,String subject,String content)   
  38.     {   
  39.         this.to = to;   
  40.         this.from = from;   
  41.         this.smtpServer = smtpServer;   
  42.         this.username = username;   
  43.         this.password = password;   
  44.         this.subject = subject;   
  45.         this.content = content;   
  46.     }   
  47.   
  48.     public void setSmtpServer(String smtpServer)   
  49.     {   
  50.         this.smtpServer = smtpServer;   
  51.     }   
  52.     public void setUserName(String usename)   
  53.     {   
  54.         this.username = usename;   
  55.     }   
  56.     public void setPassWord(String pwd)   
  57.     {   
  58.         this.password = pwd;   
  59.     }   
  60.     public void setTo(String to)   
  61.     {   
  62.         this.to = to;    
  63.     }   
  64.     public void setFrom(String from)   
  65.     {   
  66.         this.from = from;   
  67.     }   
  68.     public void setSubject(String subject)   
  69.     {   
  70.         this.subject = subject;   
  71.     }   
  72.     public void setContent(String content)   
  73.     {   
  74.         this.content = content;   
  75.     }   
  76.   
  77.     //把邮件主题转换为中文   
  78.     public String transferChinese(String strText)   
  79.     {   
  80.         try  
  81.         {   
  82.             strText = MimeUtility.encodeText(new String(strText.getBytes(), "GB2312"), "GB2312""B");   
  83.         }   
  84.         catch(Exception e)   
  85.         {   
  86.             e.printStackTrace();   
  87.         }   
  88.         return strText;   
  89.     }   
  90.     //增加附件   
  91.     public void attachfile(String fname)   
  92.     {   
  93.         file.addElement(fname);   
  94.     }   
  95.   
  96.     public boolean send()   
  97.     {   
  98.         //构造mail session   
  99.         Properties props = System.getProperties();   
  100.         props.put("mail.smtp.host",smtpServer);   
  101.         props.put("mail.smtp.auth","true");   
  102.         Session session=Session.getDefaultInstance(props, new Authenticator()   
  103.             {   
  104.                 public PasswordAuthentication getPasswordAuthentication()   
  105.                 {   
  106.                     return new PasswordAuthentication(username,password);    
  107.                 }   
  108.             });   
  109.         try  
  110.         {   
  111.             //构造MimeMessage 并设定基本的值   
  112.             MimeMessage msg = new MimeMessage(session);   
  113.             msg.setFrom(new InternetAddress(from));   
  114.             InternetAddress[] address={new InternetAddress(to)};   
  115.             msg.setRecipients(Message.RecipientType.TO,address);   
  116.             subject = transferChinese(subject);   
  117.             msg.setSubject(subject);       
  118.             //构造Multipart   
  119.             Multipart mp = new MimeMultipart();   
  120.             //向Multipart添加正文   
  121.             MimeBodyPart mbpContent = new MimeBodyPart();   
  122.             mbpContent.setText(content);   
  123.             //向MimeMessage添加(Multipart代表正文)   
  124.             mp.addBodyPart(mbpContent);   
  125.             //向Multipart添加附件   
  126.             Enumeration efile=file.elements();   
  127.             while(efile.hasMoreElements())   
  128.             {   
  129.                 MimeBodyPart mbpFile = new MimeBodyPart();   
  130.                 filename=efile.nextElement().toString();   
  131.                 FileDataSource fds = new FileDataSource(filename);   
  132.                 mbpFile.setDataHandler(new DataHandler(fds));   
  133.                 mbpFile.setFileName(fds.getName());   
  134.                 //向MimeMessage添加(Multipart代表附件)   
  135.                 mp.addBodyPart(mbpFile);   
  136.             }       
  137.             file.removeAllElements();   
  138.             //向Multipart添加MimeMessage   
  139.             msg.setContent(mp);   
  140.             msg.setSentDate(new Date());   
  141.             //发送邮件   
  142.             Transport.send(msg);     
  143.         }   
  144.         catch (MessagingException mex)   
  145.         {   
  146.             mex.printStackTrace();   
  147.             Exception ex = null;   
  148.             if ((ex=mex.getNextException())!=null)   
  149.             {   
  150.                 ex.printStackTrace();   
  151.             }   
  152.             return false;   
  153.         }   
  154.         return true;   
  155.     }   
  156.   
  157.     public static void main(String[] args)   
  158.     {   
  159.         SendMail sendmail = new SendMail();   
  160.         sendmail.setSmtpServer("smtp.163.com");   
  161.         //此处设置登陆的用户名   
  162.         sendmail.setUserName("username");   
  163.         //此处设置登陆的密码   
  164.         sendmail.setPassWord("password");   
  165.         //发送的地址   
  166.         sendmail.setTo("[email protected]");   
  167.         //发送人   
  168.         sendmail.setFrom("[email protected]");   
  169.         //设置标题   
  170.         sendmail.setSubject("你好,这是测试!");   
  171.         //设置内容   
  172.         sendmail.setContent("你好这是一个带多附件的测试!");    
  173.         //粘贴附件   
  174.         sendmail.attachfile("bruce/SendMail.java");   
  175.         //sendmail.attachfile("build.xml");   
  176.         sendmail.send();   
  177.     }   
  178. }   

你可能感兴趣的:(应用服务器,xml)