mail

 

public class SendMail {
 private Log log=LogFactory.getLog(this.getClass().getName());
 
    //收件人地址(如多人发送则用逗号隔开)
    private String mailTo = null;
    //发送人地址
    private String mailFrom = null;
   
    //SMTP主机地址   
    private String smtpHost = null;
    //是否采用调试方式
    private boolean debug = false;
    //登陆SMTP服务器的用户名和密码
    private String smtpUser=null;
    private String smtpPassword=null;
 
    private String messageBasePath = null;
    //Mail主题
    private String subject;
    //Mail内容
    private String msgContent;
 
    private Vector attachedFileList;
    private String messageContentMimeType ="text/html; charset=gb2312";
 
    //抄送人地址、密送人地址
    private String mailccTo = null;
    private String mailbccTo = null;
   
    public SendMail() {
    }
 
    private void fillMail(MimeMessage msg) throws Exception {
   
        String fileName = null;
        
        if (mailFrom != null) {
            msg.setFrom(new InternetAddress(mailFrom));
        } else {
            throw new Exception("Addresser was not maintained!");
        }
       
        if (mailTo != null) {
            InternetAddress[] address = InternetAddress.parse(mailTo);
            msg.setRecipients(Message.RecipientType.TO, address);
        } else {
         throw new Exception("Addresser was not maintained!");
        }
   
        if (mailccTo != null) {
            InternetAddress[] ccaddress = InternetAddress.parse(mailccTo);
            msg.setRecipients(Message.RecipientType.CC, ccaddress);
        }
        if (mailbccTo != null) {
            InternetAddress[] bccaddress = InternetAddress.parse(mailbccTo);
            msg.setRecipients(Message.RecipientType.BCC, bccaddress);
        }
       
        //加入主题
        msg.setSubject(subject);
       
        //加入内容
        Multipart mPart = new MimeMultipart();
       
        MimeBodyPart mBodyContent = new MimeBodyPart();
        if (msgContent != null)
            mBodyContent.setContent(msgContent, messageContentMimeType);
        else
            mBodyContent.setContent("", messageContentMimeType);
        mPart.addBodyPart(mBodyContent);
       
        //加入附件
        if (attachedFileList != null) {
            for (Enumeration fileList = attachedFileList.elements(); fileList.hasMoreElements();) {
                fileName = (String) fileList.nextElement();
                MimeBodyPart mBodyPart = new MimeBodyPart();    
               
                FileDataSource fds = new FileDataSource(messageBasePath + fileName);
                mBodyPart.setDataHandler(new DataHandler(fds));
                mBodyPart.setFileName(fileName);
                mPart.addBodyPart(mBodyPart);
            }
        }

        msg.setContent(mPart);
    }
   
    /**
     * 发送邮件
     * @return true表成功,false为失败
     * @throws Exception
     */
    public boolean sendMail(){
        Properties props = System.getProperties();
        props.put("mail.smtp.host", smtpHost);
        props.put("mail.smtp.auth", "true");
   
        MailAuthenticator auth = new MailAuthenticator();
        MailAuthenticator.MAIL_USER=smtpUser;
        MailAuthenticator.MAIL_PASSWORD=smtpPassword;
        Session session = Session.getInstance(props, auth);
        session.setDebug(debug);
       
        MimeMessage msg = new MimeMessage(session);
        Transport trans = null;
        try {
   
            fillMail(msg);
            trans = session.getTransport("smtp");
            try {
                trans.connect(smtpHost, smtpUser, smtpPassword);
            } catch (AuthenticationFailedException e) {
             log.error("Connection to mail server failed",e);
             throw e;
            } catch (MessagingException e) {
             log.error("Connection to mail server failed",e);
             throw e;
            }
   
            Transport.send(msg);
            return true;
   
        } catch (MessagingException mex) {
         log.error("",mex);
            Exception ex = null;
            if ((ex = mex.getNextException()) != null) {
                log.error(ex);
            }           
            return false;
        }catch(Exception ex){
         log.error("",ex);
         return false;
        }finally {
            try {
                if (trans != null && trans.isConnected())
                    trans.close();
            } catch (Exception e) {
             log.error("",e);
            }
        }
    }
   
    public void setAttachedFileList(java.util.Vector filelist)
    {
        attachedFileList = filelist;
    }
    public void setDebug(boolean debugFlag)
    {
        debug=debugFlag;
    }
    public void setMailAccount(String strAccount) {
    }
    public void setMailbccTo(String bccto) {
        mailbccTo = bccto;
    }
    public void setMailccTo(String ccto) {
        mailccTo = ccto;
    }
    public void setMailFrom(String from)
    {
        mailFrom=from;
    }
    public void setMailPass(String strMailPass) {
    }
    public void setMailTo(String to)
    {
        mailTo=to;
    }
    public void setMessageBasePath(String basePath)
    {
        messageBasePath=basePath;
    }
    public void setMessageContentMimeType(String mimeType)
    {
        messageContentMimeType = mimeType;
    }
    public void setMsgContent(String content)
    {
        msgContent=content;
    }
    public void setSMTPHost(String host)
    {
        smtpHost=host;
    }
    public void setSubject(String sub)
    {
        subject=sub;
    }
   
   
    public static void main(String[] argv) throws Exception
    {
        for(int i = 0;i<2;i++) {
        SendMail sm = new SendMail();
        sm.setSMTPHost("");
        sm.setMailFrom("");
        //sm.setMailTo("");
        sm.setMailTo("");
        sm.setMsgContent("自动邮件内容");
        sm.setSubject("标题");
        sm.sendMail();
        }
    }

 public void setSmtpPassword(String smtpPassword) {
  this.smtpPassword = smtpPassword;
 }

 public void setSmtpUser(String smtpUser) {
  this.smtpUser = smtpUser;
 }

}

class MailAuthenticator extends Authenticator
{
    //******************************
    //由于发送邮件的地方比较多,
    //下面统一定义用户名,口令.
    //******************************
    public static String MAIL_USER;
    public static String MAIL_PASSWORD;
 
 
    public MailAuthenticator()
    {
    }
 
    protected PasswordAuthentication getPasswordAuthentication()
    {
        return new PasswordAuthentication(MAIL_USER, MAIL_PASSWORD);
    }
 
}

你可能感兴趣的:(mail)