邮件系统很简单哦,只有下面两个类:
MailBean 和 SendMailOnTime
需要jar组件:
activation.jar
mail.jar
log4j.jar
代码如下:
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 ." );
- }
- }