Java 邮箱推送工具类

邮箱推送工具类

引入jar包

<!--   邮箱推送     -->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-email</artifactId>
            <version>1.4</version>
        </dependency>

工具类实列

EmailUtil:

/**
       下面列出网易的SMTP服务器名和端口号:
        网易邮箱          SMTP服务器     SMTP端口     POP3服务器       POP3端口
        @126.com        smtp.126.com      25          pop3.126.com      110
        @163.com        smtp.163.com      25          pop3.163.com      110
        @yeah.net       smtp.yeah.net      25          pop3.yeah.net      110
        QQ邮箱
       @qq.com         smtp.qq.com     465或587    pop.qq.com         995
    */
public class EmailUtil {

    // TODO 静态注入方式
    private static final String mail_properties = "mail.properties";
    private static final String host_name = "smtp.qq.com";//SMTP服务器
    private static final String authen_name = "自己邮箱 ";//测试账号
    private static final String authen_pwd = "登录邮箱生成的授权码"; //测试授权码
    private static final String charset = "gb2312";//设置编码

    // TODO Spring Boot 注入方式
//    static {
//        Properties p = new Properties();
//        InputStream inputStream = EmailUtil.class.getClassLoader().getResourceAsStream(mail_properties);
//        try {
//            p.load(inputStream);
//            host_name = p.getProperty(hostNames);
//            authen_name = p.getProperty(authName);
//            authen_pwd = p.getProperty(authenPwd);
//            charset = p.getProperty(charSet);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//    }

    /**
     * 发送邮件
     * @param emailTo  收件人
     * @param emailMsg 邮件内容
     * @param title 邮件标题
     */
    public static void sendEmail(String emailTo,String title, String emailMsg) throws EmailException {

        //创建HtmlEmail对象
        HtmlEmail htmlEmail = new HtmlEmail();
        //设置邮件服务器地址
        htmlEmail.setHostName(host_name);

        //设置账号和密码
        htmlEmail.setAuthentication(authen_name, authen_pwd);
        //设置发件人
        htmlEmail.setFrom(authen_name);
        //设置编码
        htmlEmail.setCharset(charset);

        //设置收件人
        htmlEmail.addTo(emailTo);
        //设置邮件主题
        htmlEmail.setSubject(title);
        //设置邮件内容
        htmlEmail.setMsg(emailMsg);

        //发送邮件
        htmlEmail.send();
    }

}

在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值