javaMail邮件发送

来源:互联网 发布:vivo软件商店手机版 编辑:程序博客网 时间:2024/05/21 17:11
最近项目需要做了一个例子..跟大家分享~~~ 有问题我们可以一起交流.~~~

 

Java代码 
  1. package com.me.util.mail;   
  2.   
  3. import java.util.Properties;   
  4.   
  5. import javax.activation.DataHandler;   
  6. import javax.activation.FileDataSource;   
  7. import javax.mail.Address;   
  8. import javax.mail.BodyPart;   
  9. import javax.mail.Message;   
  10. import javax.mail.Multipart;   
  11. import javax.mail.Session;   
  12. import javax.mail.Transport;   
  13. import javax.mail.internet.InternetAddress;   
  14. import javax.mail.internet.MimeBodyPart;   
  15. import javax.mail.internet.MimeMessage;   
  16. import javax.mail.internet.MimeMultipart;   
  17.   
  18. /**  
  19.  * @author kangkang http://corncc.ikdiy.com 2005.4.14 
  20.  */  
  21. public class Mail {   
  22.     // 21-30行把本程序所用变量进行定义。 具体在main中对它们赋植。   
  23.     private MimeMessage mimeMsg; // MIME邮件对象  
  24.     private Session session; // 邮件会话对象  
  25.     private Properties props; // 系统属性  
  26.     private boolean needAuth = false// smtp是否需要认证  
  27.     private String username = ""// smtp认证用户名和密码  
  28.     private String password = "";   
  29.     private Multipart mp; // Multipart对象,邮件内容,标题,附件等内容均添加到其中后再生成//MimeMessage对象  
  30.   
  31.     public Mail(String smtp) {   
  32.         setSmtpHost(smtp);   
  33.         createMimeMessage();   
  34.     }   
  35.   
  36.     /**  
  37.      * @param hostName  
  38.      *            String  
  39.      */  
  40.     public void setSmtpHost(String hostName) {   
  41.         System.out.println("设置系统属性:mail.smtp.host = " + hostName);   
  42.         if (props == null)   
  43.             props = System.getProperties(); // 获得系统属性对象  
  44.         props.put("mail.smtp.host", hostName); // 设置SMTP主机  
  45.     }   
  46.   
  47.     /**  
  48.      * @return boolean  
  49.      *   
  50.      */  
  51.     public boolean createMimeMessage() {   
  52.         try {   
  53.             System.out.println("准备获取邮件会话对象!");   
  54.             session = Session.getDefaultInstance(props, null); // 获得邮件会话对象  
  55.         } catch (Exception e) {   
  56.             System.err.println("获取邮件会话对象时发生错误!" + e);   
  57.             return false;   
  58.         }   
  59.         System.out.println("准备创建MIME邮件对象!");   
  60.         try {   
  61.             mimeMsg = new MimeMessage(session); // 创建MIME邮件对象  
  62.             mp = new MimeMultipart(); // mp 一个multipart对象  
  63.             // Multipart is a container that holds multiple body parts.  
  64.             return true;   
  65.         } catch (Exception e) {   
  66.             System.err.println("创建MIME邮件对象失败!" + e);   
  67.             return false;   
  68.         }   
  69.     }   
  70.   
  71.     /**  
  72.      * @param need  
  73.      *            boolean  
  74.      *   
  75.      */  
  76.     public void setNeedAuth(boolean need) {   
  77.         System.out.println("设置smtp身份认证:mail.smtp.auth = " + need);   
  78.         if (props == null)   
  79.             props = System.getProperties();   
  80.         if (need) {   
  81.             props.put("mail.smtp.auth""true");   
  82.         } else {   
  83.             props.put("mail.smtp.auth""false");   
  84.         }   
  85.     }   
  86.   
  87.     /**  
  88.      *   
  89.      * @param name  
  90.      *            String  
  91.      *   
  92.      * @param pass  
  93.      *            String  
  94.      *   
  95.      */  
  96.     public void setNamePass(String name, String pass) {   
  97.         System.out.println("程序得到用户名与密码");   
  98.         username = name;   
  99.         password = pass;   
  100.     }   
  101.   
  102.     /**  
  103.      *   
  104.      * @param mailSubject  
  105.      *            String  
  106.      * @return boolean  
  107.      *   
  108.      */  
  109.     public boolean setSubject(String mailSubject) {   
  110.         System.out.println("设置邮件主题!");   
  111.         try {   
  112.             mimeMsg.setSubject(mailSubject);   
  113.             return true;   
  114.         } catch (Exception e) {   
  115.             System.err.println("设置邮件主题发生错误!");   
  116.             return false;   
  117.         }   
  118.     }   
  119.   
  120.     /**  
  121.      *   
  122.      * @param mailBody  
  123.      *            String  
  124.      *   
  125.      */  
  126.     public boolean setBody(String mailBody) {   
  127.         try {   
  128.             System.out.println("设置邮件体格式");   
  129.             BodyPart bp = new MimeBodyPart();   
  130.             bp.setContent(   
  131.                     "<meta http-equiv=Content-Type content=text/html; charset=gb2312>"  
  132.                             + mailBody, "text/html;charset=GB2312");   
  133.             mp.addBodyPart(bp);   
  134.             return true;   
  135.         } catch (Exception e) {   
  136.             System.err.println("设置邮件正文时发生错误!" + e);   
  137.             return false;   
  138.         }   
  139.     }   
  140.   
  141.     /**  
  142.      *   
  143.      * @param name  
  144.      *            String  
  145.      * @param pass  
  146.      *            String  
  147.      */  
  148.     public boolean addFileAffix(String filename) {   
  149.         System.out.println("增加邮件附件:" + filename);   
  150.         try {   
  151.             BodyPart bp = new MimeBodyPart();   
  152.             FileDataSource fileds = new FileDataSource(filename);   
  153.             bp.setDataHandler(new DataHandler(fileds));   
  154.             bp.setFileName(fileds.getName());   
  155.             mp.addBodyPart(bp);   
  156.             return true;   
  157.         } catch (Exception e) {   
  158.             System.err.println("增加邮件附件:" + filename + "发生错误!" + e);   
  159.             return false;   
  160.         }   
  161.     }   
  162.   
  163.     /**  
  164.      * @param name  
  165.      *            String  
  166.      *   
  167.      * @param pass  
  168.      *            String  
  169.      */  
  170.     public boolean setFrom(String from) {   
  171.         System.out.println("设置发信人!");   
  172.         try {   
  173.             mimeMsg.setFrom(new InternetAddress(from)); // 设置发信人  
  174.             return true;   
  175.         } catch (Exception e) {   
  176.             return false;   
  177.         }   
  178.     }   
  179.   
  180.     /**  
  181.      * @param name  
  182.      *            String  
  183.      *   
  184.      * @param pass  
  185.      *            String  
  186.      *   
  187.      */  
  188.     public boolean setTo(String to) {   
  189.         System.out.println("设置收信人");   
  190.         if (to == null)   
  191.             return false;   
  192.         try {   
  193.             mimeMsg.setRecipients(Message.RecipientType.TO,   
  194.                     InternetAddress.parse(to));   
  195.             return true;   
  196.         } catch (Exception e) {   
  197.             return false;   
  198.         }   
  199.     }   
  200.   
  201.     /**  
  202.      * @param name  
  203.      *            String  
  204.      *   
  205.      * @param pass  
  206.      *            String  
  207.      */  
  208.     public boolean setCopyTo(String copyto) {   
  209.         System.out.println("发送附件到");   
  210.         if (copyto == null)   
  211.             return false;   
  212.         try {   
  213.             mimeMsg.setRecipients(Message.RecipientType.CC,   
  214.                     (Address[]) InternetAddress.parse(copyto));   
  215.             return true;   
  216.         } catch (Exception e) {   
  217.             return false;   
  218.         }   
  219.     }   
  220.   
  221.     /**  
  222.      * @param name  
  223.      *            String  
  224.      *   
  225.      * @param pass  
  226.      *            String  
  227.      *   
  228.      */  
  229.     public boolean sendout() {   
  230.         try {   
  231.             mimeMsg.setContent(mp);   
  232.             mimeMsg.saveChanges();   
  233.             System.out.println("正在发送邮件....");   
  234.             Session mailSession = Session.getInstance(props, null);   
  235.             Transport transport = mailSession.getTransport("smtp"); // ???  
  236.             transport.connect((String) props.get("mail.smtp.host"), username,   
  237.                     password);   
  238.             transport.sendMessage(mimeMsg,   
  239.                     mimeMsg.getRecipients(Message.RecipientType.TO));   
  240.             // transport.send(mimeMsg);   
  241.             System.out.println("发送邮件成功!");   
  242.             transport.close();   
  243.             return true;   
  244.         } catch (Exception e) {   
  245.             System.err.println("邮件发送失败!" + e);   
  246.             return false;   
  247.         }   
  248.     }   
  249.   
  250.     /**  
  251.      * Just do it as this  
  252.      */  
  253.     public static void main(String[] args) {   
  254.         String mailbody = "http://www.laabc.com 用户邮件注册测试 <font color=red>欢迎光临</font> <a href=\"http://www.laabc.com\">啦ABC</a>";   
  255.         Mail themail = new Mail("smtp.163.com");   
  256.         themail.setNeedAuth(true);   
  257.         if (themail.setSubject("laabc.com邮件测试") == false)   
  258.             return;   
  259.         // 邮件内容 支持html 如 <font color=red>欢迎光临</font> <a  
  260.         // href=\"http://www.laabc.com\">啦ABC</a>  
  261.         if (themail.setBody(mailbody) == false)   
  262.             return;   
  263.         // 收件人邮箱   
  264.         if (themail.setTo("90902333@qq.com") == false)   
  265.             return;   
  266.         // 发件人邮箱   
  267.         if (themail.setFrom("lisi86@163.com") == false)   
  268.             return;   
  269.         // 设置附件   
  270.          if (themail.addFileAffix("C:\\TwoColorBall.java") == false)   
  271.          return// 附件在本地机子上的绝对路径  
  272.         themail.setNamePass("lisi""{password}"); // 用户名与密码 ("用户名不要后缀", "{密码}")  
  273.         if (themail.sendout() == false)   
  274.             return;   
  275.     }   
  276. }  
~
原创粉丝点击