发送邮件功能

使用公司架构

try {
				SendMailUtil.sendEmail("******账户审核通知", "您好:
  您所申请的账户审核已经通过,如有疑问请联系工作人员,电话010-66677222。", entity.getPatientMail(), 3000); } catch (EmailException e) { // TODO Auto-generated catch block e.printStackTrace(); }

架构源码,其实就是封装了org.apache.commons.mail.HtmlEmail

package com.***.framework.util;

import com.***.framework.common.config.PropertiesBean;
import java.util.Calendar;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.mail.DefaultAuthenticator;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;

public abstract class SendMailUtil
{
  private static Log logger = LogFactory.getLog(SendMailUtil.class);

  public static void sendEmail(String subject, String msg, String toMail, int[] timeOut)
    throws EmailException
  {
    if (toMail == null) {
      return;
    }

    HtmlEmail email = new HtmlEmail();
    email.setHostName(PropertiesBean.getInstance().getProperty("mail.host"));
    email.setSslSmtpPort(PropertiesBean.getInstance().getProperty(
      "mail.port"));
    email.setAuthenticator(new DefaultAuthenticator(
      PropertiesBean.getInstance().getProperty("mail.username"), 
      PropertiesBean.getInstance().getProperty("mail.pwd")));
    email.setSSLOnConnect(Boolean.parseBoolean(PropertiesBean.getInstance()
      .getProperty("mail.ssl")));
    email.setCharset("utf-8");
    email.setFrom(PropertiesBean.getInstance().getProperty("mail.username"));
    email.setDebug(Boolean.parseBoolean(PropertiesBean.getInstance()
      .getProperty("mail.debug")));
    email.setSubject(subject);
    email.setMsg(msg);
    email.setSentDate(Calendar.getInstance().getTime());
    email.setSocketConnectionTimeout(10000);
    String[] arrayOfString;
    int j = (arrayOfString = toMail
      .split(",")).length; for (int i = 0; i < j; i++) {
      String toAddress = arrayOfString[i];
      email.addTo(toAddress.trim());
    }
    if ((timeOut != null) && (timeOut.length > 0))
      email.setSocketTimeout(timeOut[0]);
    else {
      email.setSocketTimeout(60000);
    }
    email.send();
  }

  public static void sendEmailByAsync(String subject, final String msg, final String toMail, final int[] timeOut)
    throws EmailException
  {
    if (toMail == null) {
      return;
    }
    new Thread()
    {
      public void run() {
        try {
          SendMailUtil.sendEmail(SendMailUtil.this, msg, toMail, timeOut);
        } catch (EmailException e) {
          SendMailUtil.logger.error(e.getMessage(), e);
        }
      }
    }
    .start();
  }
}

 PropertiesBean.getInstance().getProperty("mail.username"),

在application.propertities中配置

#========mail configuration =====
mail.host=smtp.163.com
mail.username=****@163.com
mail.pwd=*****
mail.port=25
mail.debug=false
mail.ssl=false

SSL协议,一种加密通信的协议

你可能感兴趣的:(邮件,java语言基础)