发送邮件和验证邮件是否可以发送

/**
     * @Title: sendHTMLMail   
     * @Description: TODO(发送邮件) 
     * @author: cmh
     * @date 2020年3月17日 下午4:03:12
     * @param title            发送标题
     * @param content        发送内容
     * @param recipient        接受者邮箱
     * @param from            发送人邮箱
     * @param pass            发送人密码
     * @param emailServer    邮箱服务器
     * @param port            邮箱端口
     * @param coding        编码格式
     * @param addresser        发送人名称
     * @throws
     */
    public void sendHTMLMail(String title, String content, String recipient, String from, String pass, 
            String emailServer, Integer port, String coding, String addresser) {
 
        try {
            HtmlEmail hemail = new HtmlEmail ();
            hemail.setHostName (emailServer);
            hemail.setSmtpPort (port);
            hemail.setCharset (coding);
            hemail.addTo(recipient);
            hemail.setFrom( from, addresser);
            hemail.setAuthentication( from, pass);
            hemail.setSubject(title);
            hemail.setContent(content, EmailConstants.TEXT_HTML );
            hemail.send ();
            //hemail.setHtmlMsg(connect);
            System.out.println ( "email发送成功!---------" );
        } catch (Exception e) {
            e.printStackTrace ();
            System.out.println ( "email发送失败!----------" );
        }
 
    }

 

/**
     * @Title: checkSend   
     * @Description: TODO(验证邮箱是否可以发送邮件) 
     * @author: cmh
     * @date 2020年3月17日 下午4:06:37
     * @param emailServer        邮箱服务器
     * @param port                邮箱端口
     * @param emailName            邮箱地址    
     * @param password            邮箱密码
     * @param coding            编码格式
     * @throws GeneralSecurityException
     * @throws
     */
    public boolean checkSend(String emailServer, Integer port, String emailName, String password, String coding) throws GeneralSecurityException {
        Properties prop = new Properties();
        prop.setProperty("mail.host", emailServer);
        prop.setProperty("mail.smtp.port", port.toString());
        prop.setProperty("mail.transport.protocol", "smtp");
        prop.setProperty("mail.smtp.auth", "true");
        //设置超时时间为20秒
        prop.setProperty("mail.smtp.timeout", "20000");

        boolean result = false;

        //1、创建session
        Session session = Session.getInstance(prop);
        //开启Session的debug模式,这样就可以查看到程序发送Email的运行状态
        session.setDebug(true);
        //2、通过session得到transport对象
        Transport ts;
        //3、使用邮箱的用户名和密码连上邮件服务器,发送邮件时,发件人需要提交邮箱的用户名和密码给smtp服务器,用户名和密码都通过验证之后才能够正常发送邮件给收件人。
        try {
            ts = session.getTransport();
            ts.connect(emailServer, emailName, password);
            ts.close();
            result = true;
        } catch (NoSuchProviderException e1) {
            result = false;
        } catch (MessagingException e) {
            result = false;
        }
        return result;
    }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值