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

做爬虫开发时候为了时刻得到爬虫的抓取状况,所以用到一个定时发送邮件系统,以便随时把爬虫抓取情况发送到我的邮箱,从而更好地了解爬虫运行情况,有用到的朋友尽管拷贝用了,直接能用的,记者把你的邮箱发送服务器给该一下就行了。

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

代码如下:

/*

  * Created on 2005-9-7

  *

  * TODO To change the template for this generated file go to

  * Window - Preferences - Java - Code Style - Code Templates

  *

  * Update on 2008-8-14

  */

package javaMail;

 

import javax.mail.MessagingException;

import javax.mail.Session;

import javax.mail.Transport;

import javax.mail.internet.InternetAddress;

import javax.mail.internet.MimeMessage;

 

import org.apache.log4j.Logger;

 

 

 

/**

  * @author panshengti 类功能 : 处理意见反馈邮件发送 调用 jar:activation.jar mail.jar

  */

 

public class MailBean {

   

    public static Logger log = null ;

    static {

       log = Logger.getLogger (MailBean. class );

    }

   

    // smtpHost 发件人所用到的 smtp 服务器

    String smtpHost = "smtp.163.com" ;

    // from 发件人邮箱

    String from = "d-ear@163.com" ;

    // to 收件人邮箱

    String to = "mbrasser@d-ear.com" ;

    // subject 邮件标题

    String subject = "receive a mail from d-ear@163.com" ;

    // theMessage 邮件内容

    StringBuffer theMessage = new StringBuffer();

 

    /**

      * 固定的给 ffshi@d-ear.com ,stpan@d-ear.com 发送邮件

      * create date:2008- 8- 15

      * author:Administrator

      *

      * @param smtpHost

      * @param from

      * @param subject

      * @param messageText

      * @throws MessagingException

      */

    public void sendMessage(String smtpHost, String from,

           String subject, String messageText) throws MessagingException {

       SmtpAuth sa = new SmtpAuth();

       sa.getuserinfo( "d-ear" , "123abc" );

       java.util.Properties props = new java.util.Properties();

       props.put( "mail.smtp.auth" , "true" );

       props.put( "mail.smtp.host" , smtpHost);

       System. out .println( "Constructing message- from=" + from + " to=" + to );

       InternetAddress fromAddress = new InternetAddress(from);

       InternetAddress[] toAddresss = new InternetAddress[2];

       toAddresss[0] = new InternetAddress( "ffshi@d-ear.com" );

       toAddresss[1] = new InternetAddress( "stpan@d-ear.com" );

       int i = 0;

       while (i < toAddresss. length ) {

           Session mailSession = Session.getDefaultInstance (props, sa);

           MimeMessage testMessage = new MimeMessage(mailSession);

           testMessage.setFrom(fromAddress);

           testMessage.addRecipient(javax.mail.Message.RecipientType. TO ,

                  toAddresss[i]);

           testMessage.setSentDate( new java.util.Date());

           testMessage.setSubject(subject);

           testMessage.setText(messageText);

 

           Transport.send (testMessage);

           System. out .println( "A mail have been sent!" );

           i++;

       }

    }

 

    /*

      * 163 服务器向目的邮箱发送邮件

      * 邮件发送处理 @param stmHost,from,to,subject,messageText

      */

 

    public void sendMessage(String smtpHost, String from, String to,

           String subject, String messageText) throws MessagingException {

       SmtpAuth sa = new SmtpAuth();

       sa.getuserinfo( "d-ear" , "123abc" );

       java.util.Properties props = new java.util.Properties();

       props.put( "mail.smtp.auth" , "true" );

       props.put( "mail.smtp.host" , smtpHost);

       System. out .println( "Constructing message- from=" + from + " to=" + to);

       InternetAddress fromAddress = new InternetAddress(from);

       InternetAddress toAddresss = new InternetAddress(to);

      

      

           Session mailSession = Session.getDefaultInstance (props, sa);

           MimeMessage testMessage = new MimeMessage(mailSession);

           testMessage.setFrom(fromAddress);

           testMessage.addRecipient(javax.mail.Message.RecipientType. TO ,

                  toAddresss);

           testMessage.setSentDate( new java.util.Date());

           testMessage.setSubject(subject);

           testMessage.setText(messageText);

 

           Transport.send (testMessage);

           System. out .println( "A mail have been sent to " + to);

          

    }

 

    /**

      * 功能:群发功能 , 把所有的目的邮箱作为一个数组参数传入

      * create date:2008- 8- 15

      * author:Administrator

      *

      * @param smtpHost

      * @param from

      * @param to 目的邮箱数组

      * @param subject

      * @param messageText

      * @throws MessagingException

      */

    public void sendMessage(String smtpHost, String from, String[] to,

           String subject, String messageText) throws MessagingException {

       SmtpAuth sa = new SmtpAuth();

       sa.getuserinfo( "d-ear" , "123abc" );

       java.util.Properties props = new java.util.Properties();

       props.put( "mail.smtp.auth" , "true" );

       props.put( "mail.smtp.host" , smtpHost);

       System. out .println( "Constructing message- from=" + from + " to=" + to);

       InternetAddress fromAddress = new InternetAddress(from);

      

       InternetAddress[] toAddresss = new InternetAddress[to. length ];

       for ( int len=0;len<to. length ;len++){

           toAddresss[0] = new InternetAddress(to[len]);

       }

      

       int i = 0;

       while (i < toAddresss. length ) {

           Session mailSession = Session.getDefaultInstance (props, sa);

           MimeMessage testMessage = new MimeMessage(mailSession);

           testMessage.setFrom(fromAddress);

           testMessage.addRecipient(javax.mail.Message.RecipientType. TO ,

                  toAddresss[i]);

           testMessage.setSentDate( new java.util.Date());

           testMessage.setSubject(subject);

           testMessage.setText(messageText);

 

           Transport.send (testMessage);

           System. out .println( "A mail have been sent to " +to[i]);

           i++;

       }

    }

    /*

      * 邮件用户名和密码认证

      */

    static class SmtpAuth extends javax.mail.Authenticator {

       private String user , password ;

 

       public void getuserinfo(String getuser, String getpassword) {

           user = getuser;

           password = getpassword;

       }

 

       protected javax.mail.PasswordAuthentication getPasswordAuthentication() {

           return new javax.mail.PasswordAuthentication( user , password );

       }

    }

 

}

 

 

package javaMail;

 

import javax.mail.MessagingException;

 

import org.apache.log4j.Logger;

 

public class SendMailOnTime {

 

    public static Logger log = null ;

    static {

       log = Logger.getLogger (SendMailOnTime. class );

    }

 

    /**

      * @param args

      */

    public static void sendMail(String str) {

 

       MailBean mail = new MailBean();

       try {

           mail.sendMessage( "smtp.163.com" , "d-ear@163.com" ,

                  "rent information" , str);

       } catch (MessagingException e) {

           System. out .println( "mail send error !" );

           log .debug( "mail send error !" + e.getMessage());

           e.printStackTrace();

       }

       System. out .println( "Mail have been sended ." );

    }

 

    /**

      * 给一个指定邮箱发送指定的邮件内容 create date:2008- 8- 15 author:Administrator

      *

      * @param str

      */

    public static void sendMail(String toMail, String content) {

 

       MailBean mail = new MailBean();

       try {

           mail.sendMessage( "smtp.163.com" , "d-ear@163.com" , toMail,

                  "spider information" , content);

       } catch (MessagingException e) {

           System. out .println( "mail send error !" );

           log .debug( "mail send error !" + e.getMessage());

           e.printStackTrace();

       }

       System. out .println( "Mail have been sended ." );

    }

 

    /**

      * 指定目的邮箱数组进行群发 create date:2008- 8- 15 author:Administrator

      *

      * @param toMail

      * @param content

      */

    public static void sendMail(String[] toMail, String content) {

 

       MailBean mail = new MailBean();

       try {

           mail.sendMessage( "smtp.163.com" , "d-ear@163.com" , toMail,

                  "spider information" , content);

       } catch (MessagingException e) {

           System. out .println( "mail send error !" );

           log .debug( "mail send error !" + e.getMessage());

           e.printStackTrace();

       }

       System. out .println( "Mail have been sended ." );

    }

 

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值