今天研究了一下JavaMail,总算对它有了点新的认识.
需要两个包,mail.jar,activation.jar.
以下是实现代码:
package javamaildemo;
import java.io.IOException;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.AuthenticationFailedException;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
public class EmailUtil {
//要发送Email地址
private String mailTo;
//Mail发送的起始地址
private String mailFrom;
//smtp的地址
private String smtpHost;
//是否采用debug模式
private boolean debug = false;
//邮件主题
private String subject;
//邮件内容
private String msgContent;
//邮件类型
private String msgContentMimeType="text/html;charset=UTF-8";
//邮件附件列表
private List attachedFileList;
//附件的位置
private String messageBasePath;
public EmailUtil()
{
super();
}
/*
* 填充Email内容,包括发件人,收件人,主题,主体,附件,发信日期
* @param Session,MimeMessage
*/
public void fillMail(Session session,MimeMessage msg)throws IOException, MessagingException
{
String fileName;
Multipart mPart=new MimeMultipart();
if(mailFrom != null)
{
msg.setFrom(new InternetAddress(mailFrom));
System.out.println("发送人Email地址: " + mailFrom);
}
else
{
System.out.println("没有指定发送人邮件地址!");
return;
}
if(mailTo !=null)
{
InternetAddress[] address = InternetAddress.parse(mailTo);
msg.setRecipients(Message.RecipientType.TO, address);
System.out.println("收件人Email地址: "+mailTo);
}
else
{
System.out.println("没有指定收件人邮件地址!");
return;
}
msg.setSubject(subject);
InternetAddress[] replyAddress = {new InternetAddress(mailFrom)};
msg.setReplyTo(replyAddress);
MimeBodyPart mBodyContent = new MimeBodyPart();
if(msgContent != null)
{
mBodyContent.setContent(msgContent, msgContentMimeType);
}
else
{
mBodyContent.setContent("",msgContentMimeType);
}
mPart.addBodyPart(mBodyContent);
if(attachedFileList != null)
{
for(Iterator i=attachedFileList.iterator();i.hasNext();)
{
fileName = (String)i.next();
MimeBodyPart mBodyPart = new MimeBodyPart();
FileDataSource fds = new FileDataSource(messageBasePath + fileName);
System.out.println("Mail发送的附件为: "+messageBasePath + fileName);
mBodyPart.setDataHandler(new DataHandler(fds));
mBodyPart.setFileName(fileName);
mPart.addBodyPart(mBodyPart);
}
}
msg.setContent(mPart);
msg.setSentDate(new Date());
}
/*
* 发送Email,返回0表示邮件发送成功,返回3表示邮件发送失败
* @return int
*/
public int sendEmail() throws IOException,MessagingException
{
Properties props = System.getProperties();
props.put("mail.smtp.host", smtpHost);
props.put("mail.smtp.auth", "true");
MailAuthenticator auth = new MailAuthenticator();
Session session = Session.getInstance(props,auth);
session.setDebug(debug);
MimeMessage msg = new MimeMessage(session);
Transport trans = null;
try
{
fillMail(session,msg);
trans = session.getTransport("smtp");
try
{
trans.connect(smtpHost,auth.USER,auth.PASSWORD);
}
catch(AuthenticationFailedException e)
{
e.printStackTrace();
System.out.println("连接邮件服务器错误!");
return 3;
}
catch(MessagingException e)
{
System.out.println("连接邮件服务器错误!");
return 3;
}
trans.send(msg);
trans.close();
}
catch(MessagingException mex)
{
System.out.println("发送邮件失败!");
mex.printStackTrace();
Exception ex = null;
if((ex=mex.getNextException())!=null)
{
System.out.println(ex.toString());
ex.printStackTrace();
}
return 3;
}
finally
{
try{
if(trans != null && trans.isConnected())
trans.close();
}catch(Exception e){
System.out.println(e.toString());
}
}
System.out.println("发送邮件成功!");
return 0;
}
class MailAuthenticator extends Authenticator
{
private String USER="帐号";
private String PASSWORD="密码";
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(USER,PASSWORD);
}
}
public boolean isDebug() {
return debug;
}
public void setDebug(boolean debug) {
this.debug = debug;
}
public String getMailFrom() {
return mailFrom;
}
public void setMailFrom(String mailFrom) {
this.mailFrom = mailFrom;
}
public String getMailTo() {
return mailTo;
}
public void setMailTo(String mailTo) {
this.mailTo = mailTo;
}
public String getMsgContent() {
return msgContent;
}
public void setMsgContent(String msgContent) {
this.msgContent = msgContent;
}
public String getSmtpHost() {
return smtpHost;
}
public void setSmtpHost(String smtpHost) {
this.smtpHost = smtpHost;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public String getMsgContentMimeType() {
return msgContentMimeType;
}
public void setMsgContentMimeType(String msgContentMimeType) {
this.msgContentMimeType = msgContentMimeType;
}
public List getAttachedFileList() {
return attachedFileList;
}
public void setAttachedFileList(List attachedFileList) {
this.attachedFileList = attachedFileList;
}
public String getMessageBasePath() {
return messageBasePath;
}
public void setMessageBasePath(String messageBasePath) {
this.messageBasePath = messageBasePath;
}
}