1.邮件发送类:
package
com.taihuatalk.taihua.common.common;
/**
*Author:
*CreateDate:2006-06-2522:47:00
*Modifier:
*ModifyDate:2006-06-2522:47:00
*Version:1.0
*Copyright(c)2006泰华网络沪ICP备06××××××号
*Allrightreserved.
*/
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import java.util.Date;
import javax.mail.Message.RecipientType;
import javax.mail.internet.MimeMessage;
import javax.mail.Message;
import javax.mail.Session;
import java.util.Properties;
import javax.mail.MessagingException;
import javax.mail.internet.MimeBodyPart;
import javax.mail.Multipart;
import javax.mail.internet.MimeMultipart;
import org.apache.log4j.Logger;
/**
*发送Email类
*/
public class SendEmail {
Loggerlogger = Logger.getLogger( this .getClass());
/**
*Email发送方法
* @param toemails需要一个字符串参数,用来设置收件人地址,如果收件人为多个,则用","隔开
* @param content邮件内容
* @param subject邮件主题
* @return 邮件成功发送则返回true,否则返回false
*/
public boolean sendSimpleEmail(Stringtoemails,Stringcontent,Stringsubject) {
boolean result = false ;
logger.info( " 进入发送Email类 " );
try {
// 创建属性对象
Propertiesprops = new Properties();
// 设置邮件传输协议为:smtp
props.put( " mail.transpost.protocol " , " smtp " );
// 设置邮件服务器地址
props.put( " mail.smtp.host " , " smtp.163.com " );
// 设置邮件验证为真
props.put( " mail.smtp.auth " , " true " );
// 设置邮件服务器端口
props.put( " mail.smtp.port " , " 25 " );
// 调用验证类进行验证,需要参数发件人用户名和密码
logger.info( " 验证类实例化============== " );
CheckSendEmailauth = new CheckSendEmail( "lip009 " , " lip009 " );
logger.info( " 验证通过 " );
// 创建session对象
SessionsendMailSession;
sendMailSession = Session.getInstance(props,auth);
// 设置输出调试信息
sendMailSession.setDebug( true );
logger.info( " 开始创建消息对象 " );
// 创建信息对象
MessagenewMessage = new MimeMessage(sendMailSession);
// 输入发送信息
// 设置发信人地址
logger.info( " 设置发信人地址 " );
newMessage.setFrom( new InternetAddress( " link2friend@163.com " ));
// 设置收信人地址,只支持单用户发送
// newMessage.setRecipient(Message.RecipientType.TO,newInternetAddress("lip0091981@163.com"));
// 设置收信人地址,可以支持多用户发送
logger.info( " 设置收信人地址 " );
newMessage.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(toemails));
// 附件
// ==============================================================================
// msgText是信件的正文,共有两行
// StringmsgText=content;
// msgAttachment是一段字符串作为附件内容
// StringmsgAttachment="Thisisanattachmentstring!";
// MimeBodyPartmbp1=newMimeBodyPart();
// mbp1.setText(msgText); // 把前面定义的msgText中的文字设定为邮件正文的内容
// 创建附件部分
// MimeBodyPartmbp2=newMimeBodyPart();
// 使用setText(text,charset)来加入附件
// mbp2.setText(msgAttachment,"gb2312");
// 创建Multipart
// Multipartmp=newMimeMultipart();
// mp.addBodyPart(mbp1);
// mp.addBodyPart(mbp2);
// 添加Multipart到Message中
// newMessage.setContent(mp);
// ==============================================================================
// 设置信件文本格式(当设置了附件,这里就不能有)
logger.info( " 设置格式 " );
newMessage.setContent( " SendMail " , " text/html " );
// 设置信件主题
logger.info( " 设置主题 " );
newMessage.setSubject(subject);
// 设置信件发送日期
logger.info( " 设置发送日期 " );
newMessage.setSentDate( new Date());
// 设置信件正文(当设置了附件,这里就不能有)
newMessage.setText(content);
logger.info( " 设置完消息 " );
// 创建对象
Transporttransport;
transport = sendMailSession.getTransport( " smtp " );
logger.info( " 将要发送 " );
// 发送
result = true ;
transport.send(newMessage); // 此处总是抛出异常,让人很是郁闷,但是邮件却发送成功!
logger.info( " 恭喜你!您的邮件已经成功发送! " );
}
catch (MessagingExceptionex) {
// ex.printStackTrace();
}
return result;
}
}
/**
*Author:
*CreateDate:2006-06-2522:47:00
*Modifier:
*ModifyDate:2006-06-2522:47:00
*Version:1.0
*Copyright(c)2006泰华网络沪ICP备06××××××号
*Allrightreserved.
*/
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import java.util.Date;
import javax.mail.Message.RecipientType;
import javax.mail.internet.MimeMessage;
import javax.mail.Message;
import javax.mail.Session;
import java.util.Properties;
import javax.mail.MessagingException;
import javax.mail.internet.MimeBodyPart;
import javax.mail.Multipart;
import javax.mail.internet.MimeMultipart;
import org.apache.log4j.Logger;
/**
*发送Email类
*/
public class SendEmail {
Loggerlogger = Logger.getLogger( this .getClass());
/**
*Email发送方法
* @param toemails需要一个字符串参数,用来设置收件人地址,如果收件人为多个,则用","隔开
* @param content邮件内容
* @param subject邮件主题
* @return 邮件成功发送则返回true,否则返回false
*/
public boolean sendSimpleEmail(Stringtoemails,Stringcontent,Stringsubject) {
boolean result = false ;
logger.info( " 进入发送Email类 " );
try {
// 创建属性对象
Propertiesprops = new Properties();
// 设置邮件传输协议为:smtp
props.put( " mail.transpost.protocol " , " smtp " );
// 设置邮件服务器地址
props.put( " mail.smtp.host " , " smtp.163.com " );
// 设置邮件验证为真
props.put( " mail.smtp.auth " , " true " );
// 设置邮件服务器端口
props.put( " mail.smtp.port " , " 25 " );
// 调用验证类进行验证,需要参数发件人用户名和密码
logger.info( " 验证类实例化============== " );
CheckSendEmailauth = new CheckSendEmail( "lip009 " , " lip009 " );
logger.info( " 验证通过 " );
// 创建session对象
SessionsendMailSession;
sendMailSession = Session.getInstance(props,auth);
// 设置输出调试信息
sendMailSession.setDebug( true );
logger.info( " 开始创建消息对象 " );
// 创建信息对象
MessagenewMessage = new MimeMessage(sendMailSession);
// 输入发送信息
// 设置发信人地址
logger.info( " 设置发信人地址 " );
newMessage.setFrom( new InternetAddress( " link2friend@163.com " ));
// 设置收信人地址,只支持单用户发送
// newMessage.setRecipient(Message.RecipientType.TO,newInternetAddress("lip0091981@163.com"));
// 设置收信人地址,可以支持多用户发送
logger.info( " 设置收信人地址 " );
newMessage.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(toemails));
// 附件
// ==============================================================================
// msgText是信件的正文,共有两行
// StringmsgText=content;
// msgAttachment是一段字符串作为附件内容
// StringmsgAttachment="Thisisanattachmentstring!";
// MimeBodyPartmbp1=newMimeBodyPart();
// mbp1.setText(msgText); // 把前面定义的msgText中的文字设定为邮件正文的内容
// 创建附件部分
// MimeBodyPartmbp2=newMimeBodyPart();
// 使用setText(text,charset)来加入附件
// mbp2.setText(msgAttachment,"gb2312");
// 创建Multipart
// Multipartmp=newMimeMultipart();
// mp.addBodyPart(mbp1);
// mp.addBodyPart(mbp2);
// 添加Multipart到Message中
// newMessage.setContent(mp);
// ==============================================================================
// 设置信件文本格式(当设置了附件,这里就不能有)
logger.info( " 设置格式 " );
newMessage.setContent( " SendMail " , " text/html " );
// 设置信件主题
logger.info( " 设置主题 " );
newMessage.setSubject(subject);
// 设置信件发送日期
logger.info( " 设置发送日期 " );
newMessage.setSentDate( new Date());
// 设置信件正文(当设置了附件,这里就不能有)
newMessage.setText(content);
logger.info( " 设置完消息 " );
// 创建对象
Transporttransport;
transport = sendMailSession.getTransport( " smtp " );
logger.info( " 将要发送 " );
// 发送
result = true ;
transport.send(newMessage); // 此处总是抛出异常,让人很是郁闷,但是邮件却发送成功!
logger.info( " 恭喜你!您的邮件已经成功发送! " );
}
catch (MessagingExceptionex) {
// ex.printStackTrace();
}
return result;
}
}
2.邮件帐号密码验证类:
package
com.taihuatalk.taihua.common.common;
/**
*Author:
*CreateDate:2006-06-2522:47:00
*Modifier:
*ModifyDate:2006-06-2522:47:00
*Version:1.0
*Copyright(c)2006泰华网络沪ICP备06××××××号
*Allrightreserved.
*/
import javax.mail. * ;
/**
*Email发送帐号密码验证类
*/
public class CheckSendEmail extends Authenticator {
private Stringm_username = null ;
private Stringm_userpass = null ;
public void setUsername(Stringusername)
{
m_username = username;
}
public void setUserpass(Stringuserpass)
{
m_userpass = userpass;
}
public CheckSendEmail(Stringusername,Stringuserpass)
{
super ();
setUsername(username);
setUserpass(userpass);
}
public PasswordAuthenticationgetPasswordAuthentication()
{
return new PasswordAuthentication(m_username,m_userpass);
}
}
/**
*Author:
*CreateDate:2006-06-2522:47:00
*Modifier:
*ModifyDate:2006-06-2522:47:00
*Version:1.0
*Copyright(c)2006泰华网络沪ICP备06××××××号
*Allrightreserved.
*/
import javax.mail. * ;
/**
*Email发送帐号密码验证类
*/
public class CheckSendEmail extends Authenticator {
private Stringm_username = null ;
private Stringm_userpass = null ;
public void setUsername(Stringusername)
{
m_username = username;
}
public void setUserpass(Stringuserpass)
{
m_userpass = userpass;
}
public CheckSendEmail(Stringusername,Stringuserpass)
{
super ();
setUsername(username);
setUserpass(userpass);
}
public PasswordAuthenticationgetPasswordAuthentication()
{
return new PasswordAuthentication(m_username,m_userpass);
}
}