email发送工具类

import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Address;
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 {
 
 static Map<String, String> mailProperties = new HashMap<String, String>();
 
 private String SMTPHost;
 
 private String SMTPUser;
 
 private String SMTPPassword;
 
 private String from;
 
 private String recipients;
 
 private String cc;
 
 private String bcc;
 
 private String subject;
 
 private String message;
 
 private String[] attachedFileNames;
 
 public static Map<String, String> getMailProperties() {
  return mailProperties;
 }

 public static void setMailProperties(Map<String, String> mailProperties) {
  EmailUtil.mailProperties = mailProperties;
 }

 public String getSMTPHost() {
  return SMTPHost;
 }

 public void setSMTPHost(String sMTPHost) {
  SMTPHost = sMTPHost;
 }

 public String getSMTPUser() {
  return SMTPUser;
 }

 public void setSMTPUser(String sMTPUser) {
  SMTPUser = sMTPUser;
 }

 public String getSMTPPassword() {
  return SMTPPassword;
 }

 public void setSMTPPassword(String sMTPPassword) {
  SMTPPassword = sMTPPassword;
 }

 public String getFrom() {
  return from;
 }

 public void setFrom(String from) {
  this.from = from;
 }

 public String getRecipients() {
  return recipients;
 }

 public void setRecipients(String recipients) {
  this.recipients = recipients;
 }

 public String getCc() {
  return cc;
 }

 public void setCc(String cc) {
  this.cc = cc;
 }

 public String getBcc() {
  return bcc;
 }

 public void setBcc(String bcc) {
  this.bcc = bcc;
 }

 public String getSubject() {
  return subject;
 }

 public void setSubject(String subject) {
  this.subject = subject;
 }

 public String getMessage() {
  return message;
 }

 public void setMessage(String message) {
  this.message = message;
 }

 public String[] getAttachedFileNames() {
  return attachedFileNames;
 }

 public void setAttachedFileNames(String[] attachedFileNames) {
  this.attachedFileNames = attachedFileNames;
 }

 /**
  * SMTP邮件认证
  * @param SMTPHostName 服务器名
  * @param SMTPUser     服务器用户名
  * @param SMTPPassword 服务器密码
  * @return             SMTP邮件认证
  */
 private Session getSession() {
  Properties props = new Properties();
        props.put("mail.smtp.host", SMTPHost);
        props.put("mail.smtp.auth", "false");
       
        Authenticator auth = new SMTPAuthenticator(SMTPUser,SMTPPassword);
       
        Session session = Session.getDefaultInstance(props, auth);
       
        session.setDebug(false);
       
        return session;
 }
 
 /**
  * 根据邮件内容和附件获取邮件体
  * @param message               邮件内容
  * @param attachFileNames       邮件附件
  * @return                      邮件体
  * @throws MessagingException   消息异常
  */
 private Multipart getMailContent() throws MessagingException{
  Multipart oMultipart = new MimeMultipart();
       
        MimeBodyPart oMBPText = new MimeBodyPart();
       
        oMBPText.setContent(message, "text/html");
       
        oMultipart.addBodyPart(oMBPText);
       
        if(attachedFileNames != null){
         for (int i = 0 ; i < attachedFileNames.length; i ++){
             String sFileName = attachedFileNames[i];
             File oFile = new File(sFileName);
             if (oFile.exists()){
     MimeBodyPart oMBP = new MimeBodyPart();
     FileDataSource oFileDataSource = new FileDataSource(sFileName);
     oMBP.setDataHandler(new DataHandler(oFileDataSource));
     oMBP.setFileName(oFileDataSource.getName());
     oMultipart.addBodyPart(oMBP);
     oFileDataSource = null;
     oMBP = null;
             }
             oFile = null;
             sFileName = null;
         }
        }
       
        return oMultipart;
 }
 
 private Message getMessageContent() throws MessagingException{
  
  if ((recipients == null) || (recipients.length() == 0)){
            return null;
        }
  
  Session session = getSession();
  
  Message msg = new MimeMessage(session);
  
  InternetAddress addressFrom = new InternetAddress(from);
        msg.setFrom(addressFrom);
       
        Address[] oAddressTo = InternetAddress.parse(recipients);
        msg.setRecipients(Message.RecipientType.TO, oAddressTo);
       
        if (cc != null){
           Address[] oAddressCC = InternetAddress.parse(cc);
           msg.setRecipients(Message.RecipientType.CC,oAddressCC);
       }
      
       if (bcc != null){
           Address[] oAddressBCC = InternetAddress.parse(bcc);
           msg.setRecipients(Message.RecipientType.BCC,oAddressBCC);
       }
      
       msg.setSubject(subject);
      
       msg.setContent(getMailContent());
  
  return msg;
 }
 
 /**
  * 发送邮件
  * @param message                邮件
  * @throws MessagingException    消息异常
  */
     public void postMail() throws MessagingException{  
         Transport.send(getMessageContent());    
    }
    
 /**
  * SimpleAuthenticator is used to do simple authentication
  * when the SMTP server requires it.
  */
 private class SMTPAuthenticator extends javax.mail.Authenticator
 {
          private String msUserName ;
          private String msPassword;
          public SMTPAuthenticator(String psUserName,String psPassword){
              msUserName = psUserName;
              msPassword = psPassword;
          }
         
          @Override
          public javax.mail.PasswordAuthentication getPasswordAuthentication()
          {
           return new PasswordAuthentication(msUserName, msPassword);
          }
 }
    
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值