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. }  
相关文章
|
弹性计算 负载均衡 容灾
slb配置后端服务器组
配置阿里云SLB后端服务器组涉及四个主要步骤:创建服务器组、添加ECS实例、关联监听规则和设定负载均衡策略。这使得流量根据业务需求和服务器特性进行转发,便于应用架构的灵活管理和扩展,支持蓝绿部署、灰度发布,并通过多可用区提升系统可用性和容灾能力。
335 3
|
监控 NoSQL Linux
Redis突现拒绝连接问题处理总结
Redis突现拒绝连接问题处理总结
545 0
|
10月前
|
人工智能 自然语言处理 网络性能优化
Kandinsky-3:开源的文本到图像生成框架,适应多种图像生成任务
Kandinsky-3 是一个开源的文本到图像生成框架,基于潜在扩散模型,能够适应多种图像生成任务。该框架支持高质量和逼真的图像合成,包括文本引导的修复/扩展、图像融合、文本-图像融合及视频生成等功能。Kandinsky-3 通过简化模型架构,提高了推理速度,同时保持了图像质量。
257 2
Kandinsky-3:开源的文本到图像生成框架,适应多种图像生成任务
|
存储 安全 Java
深入理解Java堆栈:机制、特性与应用
深入理解Java堆栈:机制、特性与应用
301 1
|
10月前
|
存储 easyexcel Java
SpringBoot+EasyExcel轻松实现300万数据快速导出!
本文介绍了在项目开发中使用Apache POI进行数据导入导出的常见问题及解决方案。首先比较了HSSFWorkbook、XSSFWorkbook和SXSSFWorkbook三种传统POI版本的优缺点,然后根据数据量大小推荐了合适的使用场景。接着重点介绍了如何使用EasyExcel处理超百万数据的导入导出,包括分批查询、分批写入Excel、分批插入数据库等技术细节。通过测试,300万数据的导出用时约2分15秒,导入用时约91秒,展示了高效的数据处理能力。最后总结了公司现有做法的不足,并提出了改进方向。
|
数据安全/隐私保护 iOS开发 MacOS
Mac终端出现 brew command not found 解决
Mac终端出现 brew command not found 解决
659 3
|
前端开发 API Android开发
25. 【Android教程】列表控件 ListView
25. 【Android教程】列表控件 ListView
530 2
北极星指标是什么
北极星指标是什么
796 0
|
数据处理 数据库
在Hologres的HoloWeb控制台中,您可以查看并分析历史查询语句
【2月更文挑战第20天】在Hologres的HoloWeb控制台中,您可以查看并分析历史查询语句
191 1
|
JavaScript Windows
记一下 Windows11 安装与配置 node.js 的标准步骤
这篇文章记录了在Windows 11系统上安装和配置Node.js的步骤,包括安装Node.js、验证安装、配置npm、设置npm镜像加速、全局安装cnpm并配置镜像、解决TLS连接不安全警告的详细过程。
1423 0