邮件服务器配置文件:
##协议
mail.transport.protocol=smtp
##邮件服务器地址
mail.smtp.host=mail.126.com
##邮件服务器端口
mail.smtp.port=25
##SMTP服务用户认证
mail.smtp.auth=true
 
第一个类:
 
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import javax.mail.Session;
public class SessionFactory {
 private  SmtpAuth sa = new SmtpAuth("service", "ilove17k");
 private  Session session = null;
 private  Properties props = new Properties();
 public Session getSession(String propertyFilePath) {
  try {
   props.load(new FileInputStream(new File(propertyFilePath)));
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }
  session = Session.getInstance(props,sa);
  return session;
 }
}
 
 
第二个类:
 
import java.io.UnsupportedEncodingException;
import java.util.Date;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;
public class MailUtil {
 /**
  * 是否有附件
  */
 private boolean haveAttachment = false;
 /**
  * 邮件权限验证
  */
 private SmtpAuth auth = null;
 /**
  * 从哪里发送
  */
 private InternetAddress from = null;
 /**
  * 发给谁
  */
 private String mailTo = null;
 /**
  * 邮件标题
  */
 private String title = null;
 /**
  * 邮件内容
  */
 private String body = null;
 /**
  * 邮件类型
  */
 private String content_type = null;
 /**
  * 会话对象
  */
 private Session session = null;
 /**
  * 邮件对象
  */
 private MimeMessage msg = null;
 /**
  * 正文部分
  */
 private MimeMultipart content = null;
 /**
  * 正文部分
  */
 private MimeBodyPart content_part = null;
 /**
  * 邮件发送对象
  */
 private Transport transport = null;
 /**
  * 属性文件的路径
  *
  * @param propertyFilePath
  */
 public MailUtil(String propertyFilePath) {
  SessionFactory sf=new SessionFactory();
  session = sf.getSession(propertyFilePath);
  msg = new MimeMessage(session);
  try {
   from = new InternetAddress("[email]service@17k.com[/email]");
  } catch (AddressException e) {
   e.printStackTrace();
  }
  content = new MimeMultipart();
  content_part = new MimeBodyPart();
  content_type = "text/html;charset=gb2312";
 }
 public boolean send() {
  // 设置邮件主题
  try {
   msg.setSubject(title);
   // 设置发信日期
   msg.setSentDate(new Date());
   // 设置发信人
   msg.setFrom(from);
   // 设置主收件人
   msg.setRecipient(Message.RecipientType.TO, new InternetAddress(
     mailTo));
   // 设置抄送收件人
   // msg.addRecipient(Message.RecipientType.CC, new InternetAddress(
   // ""));
   // 设置暗抄送人
   // msg.addRecipient(Message.RecipientType.BCC, new InternetAddress(
   // "[email]huql@17k.com[/email]"));
   content_part.setText(body);
   content_part.setHeader("Content-Type", content_type);
   content.addBodyPart(content_part);
   msg.setContent(content);
   // 保存修改
   msg.saveChanges();
   // 创建发送对象
   Transport trans = session.getTransport();
   // 发送
   trans.send(msg);
   return true;
  } catch (MessagingException e) {
   e.printStackTrace();
   return false;
  }
 }
 /**
  * 添加附件的方法
  *
  * @throws MessagingException
  */
 public void addAttachMent() throws MessagingException {
  // 添加附件
  String filename = "D:\\Downloads\\super_star.swf"; // 用于存储文件名称与存放路径
  if (filename != null || filename.length() > 0) {
   DataSource fds = new FileDataSource(filename); // 创建文件流对象
   MimeBodyPart attachment_part = new MimeBodyPart();
   attachment_part.setDataHandler(new DataHandler(fds));
   try {
    attachment_part.setFileName(MimeUtility.encodeText(fds
      .getName()));
   } catch (UnsupportedEncodingException e) {
    e.printStackTrace();
   }
   // 添加二进制编码至邮件体中
   content.addBodyPart(attachment_part);
  }
 }
 public InternetAddress getFrom() {
  return from;
 }
 public void setFrom(InternetAddress from) {
  this.from = from;
 }
 public String getMailTo() {
  return mailTo;
 }
 public void setMailTo(String mailTo) {
  this.mailTo = mailTo;
 }
 public String getTitle() {
  return title;
 }
 public void setTitle(String title) {
  this.title = title;
 }
 public String getBody() {
  return body;
 }
 public void setBody(String body) {
  this.body = body;
 }
}
 
 
测试:
public static void main(String args){
MailUtil mail = new MailUtil(propertyFile);
mail.setTitle(Utils.toGB2312(title));
mail.setBody(headContent.toString()+bookContent.toString());
mail.setMailTo(email);
}