www.pudn.com > Javamail.rar > SendMail.java


package com.digipower.automail.mailsender;

import java.util.Properties;
import java.util.Date;
import java.util.ArrayList;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
import com.digipower.automail.AutoMailConstants;


public abstract class SendMail{

    protected BodyPart messageBodyPart = null;
    protected Multipart multipart = null;
    protected MimeMessage mailMessage = null;
    protected Session mailSession = null;
    protected Properties mailProperties = System.getProperties();

    protected InternetAddress mailFromAddress = null;
    protected InternetAddress mailToAddress = null;
    protected MailAuthenticator authenticator = null;
    protected String mailSubject ="";
    protected Date mailSendDate = null;

    protected String unSendMailPath="";//待发邮件目录
    public String getHtmlFromFile(String uspath){return null;}//从文件里得到HTML信息

    public SendMail(String smtpHost,String username,String password){

        mailProperties.put("mail.smtp.host",smtpHost);
        mailProperties.put("mail.smtp.auth","true"); //设置smtp认证,很关键的一句
        authenticator = new MailAuthenticator(username,password);
        mailSession = Session.getInstance(mailProperties,authenticator);
        mailMessage = new MimeMessage(mailSession);
        messageBodyPart = new MimeBodyPart();
    }

    //设置邮件主题
    public void setSubject(String mailSubject)throws MessagingException{
        this.mailSubject = mailSubject;
        mailMessage.setSubject(mailSubject);
    }
    //所有子类都需要实现的抽象方法,为了支持不同的邮件类型
    protected abstract void setMailContent(String mailContent)throws MessagingException;

    //设置邮件发送日期
    public void setSendDate(Date sendDate)throws MessagingException{
        this.mailSendDate = sendDate;
        mailMessage.setSentDate(sendDate);
    }

    //设置邮件发送附件
    public void setAttachments(String attachmentName)throws MessagingException,Exception{
System.out.println("attachmentNamestart:" + attachmentName);
        attachmentName = new String(attachmentName.getBytes(),"GBK");
System.out.println("attachmentNameend:" + attachmentName);
      messageBodyPart = new MimeBodyPart();
        DataSource source = new FileDataSource(attachmentName);
        messageBodyPart.setDataHandler(new DataHandler(source));
        int index = attachmentName.lastIndexOf('\\');

        //防止在传送中文名副档的时候档名出现乱码
        String attachmentRealName = MimeUtility.encodeText(attachmentName.substring(index+1));

System.out.println("attachmentRealName:" + attachmentRealName);
        messageBodyPart.setFileName(attachmentRealName);
        multipart.addBodyPart(messageBodyPart);
    }

    //设置发件人地址
    public void setMailFrom(String mailFrom)throws MessagingException{

      try
      {
        mailFromAddress = new InternetAddress(mailFrom, AutoMailConstants.COMPANY);
        mailMessage.setFrom(mailFromAddress);
      }
      catch(Exception e)
      {
        e.printStackTrace();
      }
    }

    //设置收件人地址,收件人类型为to,cc,bcc(大小写不限)
    public void setMailTo(String mailTo,String mailType)throws Exception{
        //for(int i=0;i");
      //mailMessage.setHeader("Disposition-Notification-To","\"" + replyName + "\"<" + replyAddress + ">");
      mailMessage.setHeader("Disposition-Notification-To", AutoMailConstants.REPLY_SEPARATOR + folderName + "<" + replyAddress + ">");
    }

    /**
     *
     * @param replyTo String
     * @throws MessagingException
     */
    public void setReplyTo(String replyTo) throws MessagingException{

          try
          {
            InternetAddress temp = new InternetAddress(replyTo);
           Address [] addressArray = new InternetAddress [] {temp};
            mailMessage.setReplyTo(addressArray);
          }
          catch(Exception e)
          {
            e.printStackTrace();
          }
        }

    public void setDescription( String FolderName, String strMail) throws Exception{
      mailMessage.setDescription(FolderName + strMail + AutoMailConstants.POSTMASTER_SEPARATOR);
    }

}