如何使用JavaMail发送邮件?
如何使用JavaMail发送邮件?
1.邮件发送类:
packagecom.taihuatalk.taihua.common.common;
/***//**
*Author:李鹏
*CreateDate:2006-06-2522:47:00
*Modifier:李鹏
*ModifyDate:2006-06-2522:47:00
*Version:1.0
*Copyright(c)2006 深蓝工作室
*Allrightreserved.
*/
importjavax.mail.Transport;
importjavax.mail.internet.InternetAddress;
importjava.util.Date;
importjavax.mail.Message.RecipientType;
importjavax.mail.internet.MimeMessage;
importjavax.mail.Message;
importjavax.mail.Session;
importjava.util.Properties;
importjavax.mail.MessagingException;
importjavax.mail.internet.MimeBodyPart;
importjavax.mail.Multipart;
importjavax.mail.internet.MimeMultipart;
importorg.apache.log4j.Logger;
/***//**
*发送Email类
*/
publicclassSendEmail...{
Loggerlogger=Logger.getLogger(this.getClass());
/***//**
*Email发送方法
*@paramtoemails需要一个字符串参数,用来设置收件人地址,如果收件人为多个,则用","隔开
*@paramcontent邮件内容
*@paramsubject邮件主题
*@return邮件成功发送则返回true,否则返回false
*/
publicbooleansendSimpleEmail(Stringtoemails,Stringcontent,Stringsubject)...{
booleanresult=false;
logger.info("进入发送Email类");
try...{
//创建属性对象
Propertiesprops=newProperties();
//设置邮件传输协议为: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=newCheckSendEmail("lip009","lip009");
logger.info("验证通过");
//创建session对象
SessionsendMailSession;
sendMailSession=Session.getInstance(props,auth);
//设置输出调试信息
sendMailSession.setDebug(true);
logger.info("开始创建消息对象");
//创建信息对象
MessagenewMessage=newMimeMessage(sendMailSession);
//输入发送信息
//设置发信人地址
logger.info("设置发信人地址");
newMessage.setFrom(newInternetAddress("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(newDate());
//设置信件正文(当设置了附件,这里就不能有)
newMessage.setText(content);
logger.info("设置完消息");
//创建对象
Transporttransport;
transport=sendMailSession.getTransport("smtp");
logger.info("将要发送");
//发送
result=true;
transport.send(newMessage);//此处总是抛出异常,让人很是郁闷,但是邮件却发送成功!
logger.info("恭喜你!您的邮件已经成功发送!");
}
catch(MessagingExceptionex)...{
//ex.printStackTrace();
}
returnresult;
}
}
/***//**
*Author:李鹏
*CreateDate:2006-06-2522:47:00
*Modifier:李鹏
*ModifyDate:2006-06-2522:47:00
*Version:1.0
*Copyright(c)2006 深蓝工作室
*Allrightreserved.
*/
importjavax.mail.Transport;
importjavax.mail.internet.InternetAddress;
importjava.util.Date;
importjavax.mail.Message.RecipientType;
importjavax.mail.internet.MimeMessage;
importjavax.mail.Message;
importjavax.mail.Session;
importjava.util.Properties;
importjavax.mail.MessagingException;
importjavax.mail.internet.MimeBodyPart;
importjavax.mail.Multipart;
importjavax.mail.internet.MimeMultipart;
importorg.apache.log4j.Logger;
/***//**
*发送Email类
*/
publicclassSendEmail...{
Loggerlogger=Logger.getLogger(this.getClass());
/***//**
*Email发送方法
*@paramtoemails需要一个字符串参数,用来设置收件人地址,如果收件人为多个,则用","隔开
*@paramcontent邮件内容
*@paramsubject邮件主题
*@return邮件成功发送则返回true,否则返回false
*/
publicbooleansendSimpleEmail(Stringtoemails,Stringcontent,Stringsubject)...{
booleanresult=false;
logger.info("进入发送Email类");
try...{
//创建属性对象
Propertiesprops=newProperties();
//设置邮件传输协议为: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=newCheckSendEmail("lip009","lip009");
logger.info("验证通过");
//创建session对象
SessionsendMailSession;
sendMailSession=Session.getInstance(props,auth);
//设置输出调试信息
sendMailSession.setDebug(true);
logger.info("开始创建消息对象");
//创建信息对象
MessagenewMessage=newMimeMessage(sendMailSession);
//输入发送信息
//设置发信人地址
logger.info("设置发信人地址");
newMessage.setFrom(newInternetAddress("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(newDate());
//设置信件正文(当设置了附件,这里就不能有)
newMessage.setText(content);
logger.info("设置完消息");
//创建对象
Transporttransport;
transport=sendMailSession.getTransport("smtp");
logger.info("将要发送");
//发送
result=true;
transport.send(newMessage);//此处总是抛出异常,让人很是郁闷,但是邮件却发送成功!
logger.info("恭喜你!您的邮件已经成功发送!");
}
catch(MessagingExceptionex)...{
//ex.printStackTrace();
}
returnresult;
}
}
2.邮件帐号密码验证类:
packagecom.taihuatalk.taihua.common.common;
/***//**
*Author:李鹏
*CreateDate:2006-06-2522:47:00
*Modifier:李鹏
*ModifyDate:2006-06-2522:47:00
*Version:1.0
*Copyright(c)2006 深蓝工作室
*Allrightreserved.
*/
importjavax.mail.*;
/***//**
*Email发送帐号密码验证类
*/
publicclassCheckSendEmailextendsAuthenticator...{
privateStringm_username=null;
privateStringm_userpass=null;
publicvoidsetUsername(Stringusername)
...{
m_username=username;
}
publicvoidsetUserpass(Stringuserpass)
...{
m_userpass=userpass;
}
publicCheckSendEmail(Stringusername,Stringuserpass)
...{
super();
setUsername(username);
setUserpass(userpass);
}
publicPasswordAuthenticationgetPasswordAuthentication()
...{
returnnewPasswordAuthentication(m_username,m_userpass);
}
}
/***//**
*Author:李鹏
*CreateDate:2006-06-2522:47:00
*Modifier:李鹏
*ModifyDate:2006-06-2522:47:00
*Version:1.0
*Copyright(c)2006 深蓝工作室
*Allrightreserved.
*/
importjavax.mail.*;
/***//**
*Email发送帐号密码验证类
*/
publicclassCheckSendEmailextendsAuthenticator...{
privateStringm_username=null;
privateStringm_userpass=null;
publicvoidsetUsername(Stringusername)
...{
m_username=username;
}
publicvoidsetUserpass(Stringuserpass)
...{
m_userpass=userpass;
}
publicCheckSendEmail(Stringusername,Stringuserpass)
...{
super();
setUsername(username);
setUserpass(userpass);
}
publicPasswordAuthenticationgetPasswordAuthentication()
...{
returnnewPasswordAuthentication(m_username,m_userpass);
}
}