javaMail实现邮件发送,群发功能

简介:  邮件系统很简单哦,只有下面两个类:  MailBean 和 SendMailOnTime  需要jar组件:  activation.

 邮件系统很简单哦,只有下面两个类: 
MailBean 和 SendMailOnTime 
需要jar组件: 
activation.jar 
mail.jar 
log4j.jar 

代码如下:
[java]  view plain copy
  1. /* 
  2.   * Created on 2005-9-7 
  3.   * 
  4.   * TODO To change the template for this generated file go to 
  5.   * Window - Preferences - Java - Code Style - Code Templates 
  6.   * 
  7.   * Update on 2008-8-14 
  8.   */  
  9. package javaMail;  
  10.    
  11. import javax.mail.MessagingException;  
  12. import javax.mail.Session;  
  13. import javax.mail.Transport;  
  14. import javax.mail.internet.InternetAddress;  
  15. import javax.mail.internet.MimeMessage;  
  16.    
  17. import org.apache.log4j.Logger;  
  18.    
  19.    
  20.    
  21. /** 
  22.   * @author panshengti 类功能 : 处理意见反馈邮件发送 调用 jar:activation.jar 、 mail.jar 
  23.   */  
  24.    
  25. public class MailBean {  
  26.      
  27.     public static Logger log = null ;  
  28.     static {  
  29.        log = Logger.getLogger (MailBean. class );  
  30.     }  
  31.      
  32.     // smtpHost 发件人所用到的 smtp 服务器  
  33.     String smtpHost = "smtp.163.com" ;  
  34.     // from 发件人邮箱  
  35.     String from = "d-ear@163.com" ;  
  36.     // to 收件人邮箱  
  37.     String to = "mbrasser@d-ear.com" ;  
  38.     // subject 邮件标题  
  39.     String subject = "receive a mail from d-ear@163.com" ;  
  40.     // theMessage 邮件内容  
  41.     StringBuffer theMessage = new StringBuffer();  
  42.    
  43.     /** 
  44.       * 固定的给 ffshi@d-ear.com ,stpan@d-ear.com 发送邮件 
  45.       * create date:2008- 8- 15 
  46.       * author:Administrator 
  47.       * 
  48.       * @param smtpHost 
  49.       * @param from 
  50.       * @param subject 
  51.       * @param messageText 
  52.       * @throws MessagingException 
  53.       */  
  54.     public void sendMessage(String smtpHost, String from,  
  55.            String subject, String messageText) throws MessagingException {  
  56.        SmtpAuth sa = new SmtpAuth();  
  57.        sa.getuserinfo( "d-ear" , "123abc" );  
  58.        java.util.Properties props = new java.util.Properties();  
  59.        props.put( "mail.smtp.auth" , "true" );  
  60.        props.put( "mail.smtp.host" , smtpHost);  
  61.        System. out .println( "Constructing message- from=" + from + " to=" + to );  
  62.        InternetAddress fromAddress = new InternetAddress(from);  
  63.        InternetAddress[] toAddresss = new InternetAddress[2];  
  64.        toAddresss[0] = new InternetAddress( "ffshi@d-ear.com" );  
  65.        toAddresss[1] = new InternetAddress( "stpan@d-ear.com" );  
  66.        int i = 0;  
  67.        while (i < toAddresss. length ) {  
  68.            Session mailSession = Session.getDefaultInstance (props, sa);  
  69.            MimeMessage testMessage = new MimeMessage(mailSession);  
  70.            testMessage.setFrom(fromAddress);  
  71.            testMessage.addRecipient(javax.mail.Message.RecipientType. TO ,  
  72.                   toAddresss[i]);  
  73.            testMessage.setSentDate( new java.util.Date());  
  74.            testMessage.setSubject(subject);  
  75.            testMessage.setText(messageText);  
  76.    
  77.            Transport.send (testMessage);  
  78.            System. out .println( "A mail have been sent!" );  
  79.            i++;  
  80.        }  
  81.     }  
  82.    
  83.     /* 
  84.       * 由 163 服务器向目的邮箱发送邮件 
  85.       * 邮件发送处理 @param stmHost,from,to,subject,messageText 
  86.       */  
  87.    
  88.     public void sendMessage(String smtpHost, String from, String to,  
  89.            String subject, String messageText) throws MessagingException {  
  90.        SmtpAuth sa = new SmtpAuth();  
  91.        sa.getuserinfo( "d-ear" , "123abc" );  
  92.        java.util.Properties props = new java.util.Properties();  
  93.        props.put( "mail.smtp.auth" , "true" );  
  94.        props.put( "mail.smtp.host" , smtpHost);  
  95.        System. out .println( "Constructing message- from=" + from + " to=" + to);  
  96.        InternetAddress fromAddress = new InternetAddress(from);  
  97.        InternetAddress toAddresss = new InternetAddress(to);  
  98.         
  99.         
  100.            Session mailSession = Session.getDefaultInstance (props, sa);  
  101.            MimeMessage testMessage = new MimeMessage(mailSession);  
  102.            testMessage.setFrom(fromAddress);  
  103.            testMessage.addRecipient(javax.mail.Message.RecipientType. TO ,  
  104.                   toAddresss);  
  105.            testMessage.setSentDate( new java.util.Date());  
  106.            testMessage.setSubject(subject);  
  107.            testMessage.setText(messageText);  
  108.    
  109.            Transport.send (testMessage);  
  110.            System. out .println( "A mail have been sent to " + to);  
  111.             
  112.     }  
  113.    
  114.     /** 
  115.       * 功能:群发功能 , 把所有的目的邮箱作为一个数组参数传入 
  116.       * create date:2008- 8- 15 
  117.       * author:Administrator 
  118.       * 
  119.       * @param smtpHost 
  120.       * @param from 
  121.       * @param to 目的邮箱数组 
  122.       * @param subject 
  123.       * @param messageText 
  124.       * @throws MessagingException 
  125.       */  
  126.     public void sendMessage(String smtpHost, String from, String[] to,  
  127.            String subject, String messageText) throws MessagingException {  
  128.        SmtpAuth sa = new SmtpAuth();  
  129.        sa.getuserinfo( "d-ear" , "123abc" );  
  130.        java.util.Properties props = new java.util.Properties();  
  131.        props.put( "mail.smtp.auth" , "true" );  
  132.        props.put( "mail.smtp.host" , smtpHost);  
  133.        System. out .println( "Constructing message- from=" + from + " to=" + to);  
  134.        InternetAddress fromAddress = new InternetAddress(from);  
  135.         
  136.        InternetAddress[] toAddresss = new InternetAddress[to. length ];  
  137.        for ( int len=0;len<to. length ;len++){  
  138.            toAddresss[0] = new InternetAddress(to[len]);  
  139.        }  
  140.         
  141.        int i = 0;  
  142.        while (i < toAddresss. length ) {  
  143.            Session mailSession = Session.getDefaultInstance (props, sa);  
  144.            MimeMessage testMessage = new MimeMessage(mailSession);  
  145.            testMessage.setFrom(fromAddress);  
  146.            testMessage.addRecipient(javax.mail.Message.RecipientType. TO ,  
  147.                   toAddresss[i]);  
  148.            testMessage.setSentDate( new java.util.Date());  
  149.            testMessage.setSubject(subject);  
  150.            testMessage.setText(messageText);  
  151.    
  152.            Transport.send (testMessage);  
  153.            System. out .println( "A mail have been sent to " +to[i]);  
  154.            i++;  
  155.        }  
  156.     }  
  157.     /* 
  158.       * 邮件用户名和密码认证 
  159.       */  
  160.     static class SmtpAuth extends javax.mail.Authenticator {  
  161.        private String user , password ;  
  162.    
  163.        public void getuserinfo(String getuser, String getpassword) {  
  164.            user = getuser;  
  165.            password = getpassword;  
  166.        }  
  167.    
  168.        protected javax.mail.PasswordAuthentication getPasswordAuthentication() {  
  169.            return new javax.mail.PasswordAuthentication( user , password );  
  170.        }  
  171.     }  
  172.    
  173. }  
  174.    
  175.    
  176. package javaMail;  
  177.    
  178. import javax.mail.MessagingException;  
  179.    
  180. import org.apache.log4j.Logger;  
  181.    
  182. public class SendMailOnTime {  
  183.    
  184.     public static Logger log = null ;  
  185.     static {  
  186.        log = Logger.getLogger (SendMailOnTime. class );  
  187.     }  
  188.    
  189.     /** 
  190.       * @param args 
  191.       */  
  192.     public static void sendMail(String str) {  
  193.    
  194.        MailBean mail = new MailBean();  
  195.        try {  
  196.            mail.sendMessage( "smtp.163.com" , "d-ear@163.com" ,  
  197.                   "rent information" , str);  
  198.        } catch (MessagingException e) {  
  199.            System. out .println( "mail send error !" );  
  200.            log .debug( "mail send error !" + e.getMessage());  
  201.            e.printStackTrace();  
  202.        }  
  203.        System. out .println( "Mail have been sended ." );  
  204.     }  
  205.    
  206.     /** 
  207.       * 给一个指定邮箱发送指定的邮件内容 create date:2008- 8- 15 author:Administrator 
  208.       * 
  209.       * @param str 
  210.       */  
  211.     public static void sendMail(String toMail, String content) {  
  212.    
  213.        MailBean mail = new MailBean();  
  214.        try {  
  215.            mail.sendMessage( "smtp.163.com" , "d-ear@163.com" , toMail,  
  216.                   "spider information" , content);  
  217.        } catch (MessagingException e) {  
  218.            System. out .println( "mail send error !" );  
  219.            log .debug( "mail send error !" + e.getMessage());  
  220.            e.printStackTrace();  
  221.        }  
  222.        System. out .println( "Mail have been sended ." );  
  223.     }  
  224.    
  225.     /** 
  226.       * 指定目的邮箱数组进行群发 create date:2008- 8- 15 author:Administrator 
  227.       * 
  228.       * @param toMail 
  229.       * @param content 
  230.       */  
  231.     public static void sendMail(String[] toMail, String content) {  
  232.    
  233.        MailBean mail = new MailBean();  
  234.        try {  
  235.            mail.sendMessage( "smtp.163.com" , "d-ear@163.com" , toMail,  
  236.                   "spider information" , content);  
  237.        } catch (MessagingException e) {  
  238.            System. out .println( "mail send error !" );  
  239.            log .debug( "mail send error !" + e.getMessage());  
  240.            e.printStackTrace();  
  241.        }  
  242.        System. out .println( "Mail have been sended ." );  
  243.     }  
  244.    
  245. }  
相关文章
|
2月前
|
数据安全/隐私保护
邮件发送的原理,如何进行邮件发送
邮件发送的原理,如何进行邮件发送
|
2月前
|
数据安全/隐私保护
JavaMail给QQ邮箱发邮件报错
JavaMail给QQ邮箱发邮件报错
16 0
|
4月前
|
搜索推荐 Go 网络安全
超详细的邮件发送,值得收藏分享
超详细的邮件发送,值得收藏分享
34 0
|
10月前
Javamail发送新浪邮件后保存邮件到已发送
Javamail发送新浪邮件后保存邮件到已发送
68 0
|
10月前
|
NoSQL API Redis
|
Java 数据安全/隐私保护
springboot发邮件,javaxmail收邮件功能
springboot发邮件,javaxmail收邮件功能
219 0
|
存储 网络协议 数据安全/隐私保护
基于163邮箱实现的邮件发送功能
基于163邮箱实现的邮件发送功能
412 0
基于163邮箱实现的邮件发送功能