Mail工具包

邮件工具包

POM

 <dependency>
     <groupId>com.sun.mail</groupId>
     <artifactId>javax.mail</artifactId>
     <version>1.5.6</version>
     <scope>compile</scope>
 </dependency>

MailUtils

import com.sun.mail.util.MailSSLSocketFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;


/**
 * 邮件发送类
 * @author yangchengguang
 */
@Component
public class MailUtils {

    private static final Logger LOGGER = LoggerFactory.getLogger(MailUtils.class);

    private static String USER_NAME;
    @Value("${mail.name:}")
    public void setUserName(String userName) {
        USER_NAME = userName;
    }

    private static String PASSWORD;
    @Value("${mail.password:}")
    public void setPASSWORD(String passWord) {
        PASSWORD = passWord;
    }

    private static String AUTHORIZATION_CODE;
    @Value("${mail.code:}")
    public void setAuthorizationCode(String authorizationCode) {
        AUTHORIZATION_CODE = authorizationCode;
    }

    private static String MAIL_HOST;
    @Value("${mail.host:}")
    public void setMailHost(String mailHost) {
        MAIL_HOST = mailHost;
    }

    private static String POP_PORT;
    @Value("${mail.port:}")
    public void setPopPort(String popPort) {
        POP_PORT = popPort;
    }
//    public static void main(String[] args) {
//        sendOutLookSMTPMail("wangshuai9776@morimatsu.cn", "看到请Call我", "这是一个测试邮件");
//        sendQQMail("wangshuai9776@morimatsu.cn", "看到请Call我", "这是一个测试邮件");
//    }

    /**
     * 发送腾讯邮件
     * @param toUrl 接收邮件邮箱地址
     * @param text 邮件内容
     * @param title 邮件主题
     */
    public static void sendQQMail(String toUrl, String text, String title) {
        try {
            Properties props = new Properties();

            // 开启debug调试
            props.setProperty("mail.debug", "true");
            // 发送服务器需要身份验证
            props.setProperty("mail.smtp.auth", "true");
            // 设置邮件服务器主机名
            props.setProperty("mail.host", MAIL_HOST);
            // 发送邮件协议名称
            props.setProperty("mail.transport.protocol", "smtp");

            MailSSLSocketFactory sf = new MailSSLSocketFactory();
            sf.setTrustAllHosts(true);
            props.put("mail.smtp.ssl.enable", "true");
            props.put("mail.smtp.ssl.socketFactory", sf);

            Session session = Session.getInstance(props);

            //邮件内容部分
            Message msg = new MimeMessage(session);
            msg.setSubject(title);
            msg.setText(text);
            //邮件发送者
            msg.setFrom(new InternetAddress(USER_NAME));

            //发送邮件
            Transport transport = session.getTransport();
            transport.connect(MAIL_HOST, USER_NAME, AUTHORIZATION_CODE);

            transport.sendMessage(msg, new Address[]{new InternetAddress(toUrl)});
            transport.close();
        } catch (Exception e) {
            LOGGER.error("qq邮件发送异常:{}",e);
        }
    }

    /**
     * 发送OutLook邮件
     * @param toUrl 接收邮件邮箱地址
     * @param text 邮件内容
     * @param title 邮件主题
     */
    public static void sendOutLookSMTPMail(String toUrl, String text, String title) {
//        String MAIL_HOST = "SMC-EXSRV03.cmorimatsu.sh.cn";
//        String POP_PORT = "587";
        final Properties props = new Properties();

        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.host", MAIL_HOST);
        props.put("mail.store.protocol", "smtp");
        props.put("mail.smtp.port", POP_PORT);
        //开启SSL
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.socketFactory.port",POP_PORT);
        props.put("mail.smtp.socketFactory.fallback","false");
        try {
            Session session = Session.getDefaultInstance(props, new Authenticator() {
                @Override
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(USER_NAME, PASSWORD);//账号密码
                }
            });
            session.setDebug(true);
            // 创建邮件消息
            MimeMessage message = new MimeMessage(session);
            // 设置发件人
            InternetAddress form = new InternetAddress(USER_NAME);
            message.setFrom(form);
            // 设置收件人
            InternetAddress toAddress = new InternetAddress(toUrl);
            message.setRecipient(Message.RecipientType.TO, toAddress);
            // 设置邮件标题
            message.setSubject(title);
            // 设置邮件的内容体
            message.setContent(text, "text/html;charset=UTF-8");
            // 发送邮件
            Transport.send(message);
        } catch (Exception e) {
            LOGGER.error("OutLook邮件发送异常:{}",e);
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值