邮件发送整合

2019-09-10 18:10:06 浏览数 (1)

1. 邮件发送整合

1.1. 工具代码

代码语言:javascript复制
package com.zhiyis.framework.util;

import com.zhiyis.framework.config.MailConfig;
import org.apache.commons.mail.DefaultAuthenticator;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 邮件发送.Util
 *
 * @author lll 
 */
public class MailUtil {
    private static Logger logger = LoggerFactory.getLogger(MailUtil.class);

    public static boolean sendDefaultMail(String mailSubject, String mailBody) {
        return sendMail(MailConfig.getAdminConfig().getMailUsername(), mailSubject, mailBody);
    }
    /**
     *
     * @param toAddress     收件人邮箱
     * @param mailSubject   邮件主题
     * @param mailBody      邮件正文
     * @return
     */
    public static boolean sendMail(String toAddress, String mailSubject, String mailBody){

        try {
            // Create the email message
            HtmlEmail email = new HtmlEmail();

            //email.setDebug(true);     // 将会打印一些log
            //email.setTLS(true);       // 是否TLS校验,,某些邮箱需要TLS安全校验,同理有SSL校验
            //email.setSSL(true);

            email.setHostName(MailConfig.getAdminConfig().getMailHost());

            if (MailConfig.getAdminConfig().isMailSSL()) {
                email.setSslSmtpPort(MailConfig.getAdminConfig().getMailPort());
                email.setSSLOnConnect(true);
            } else {
                email.setSmtpPort(Integer.valueOf(MailConfig.getAdminConfig().getMailPort()));
            }

            email.setAuthenticator(new DefaultAuthenticator(MailConfig.getAdminConfig().getMailUsername(), MailConfig.getAdminConfig().getMailPassword()));
            email.setCharset("UTF-8");

            email.setFrom(MailConfig.getAdminConfig().getMailUsername(), MailConfig.getAdminConfig().getMailSendNick());
            email.addTo(toAddress);
            email.setSubject(mailSubject);
            email.setMsg(mailBody);

            //email.attach(attachment); // add the attachment

            email.send();               // send the email
            return true;
        } catch (EmailException e) {
            logger.error(e.getMessage(), e);

        }
        return false;
    }

}

1.2. application.properties配置

代码语言:javascript复制
### email
mail.host=smtp.163.com
mail.port=25
mail.ssl=false
mail.username=15068610616@163.com
mail.password=123456
mail.sendNick=《错误信息邮件发送》

1.3. 配置类

代码语言:javascript复制
package com.zhiyis.framework.config;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

/**
 * @author laoliangliang
 * @date 2018/12/20 17:03
 */
@Configuration
public class MailConfig implements InitializingBean {
    private static MailConfig adminConfig = null;

    public static MailConfig getAdminConfig() {
        return adminConfig;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        adminConfig = this;
    }

    @Value("${mail.host}")
    private String mailHost;

    @Value("${mail.port}")
    private String mailPort;

    @Value("${mail.ssl}")
    private boolean mailSSL;

    @Value("${mail.username}")
    private String mailUsername;

    @Value("${mail.password}")
    private String mailPassword;

    @Value("${mail.sendNick}")
    private String mailSendNick;

    public String getMailHost() {
        return mailHost;
    }

    public String getMailPort() {
        return mailPort;
    }

    public boolean isMailSSL() {
        return mailSSL;
    }

    public String getMailUsername() {
        return mailUsername;
    }

    public String getMailPassword() {
        return mailPassword;
    }

    public String getMailSendNick() {
        return mailSendNick;
    }

}

0 人点赞