Java邮件发送解决ssl及邮件中文乱码


Java邮件发送解决ssl及邮件中文乱码

org.apache.commons.mail.EmailException: Sending the email to the following server failed : smtp.qiye.163.com:25
Caused by: javax.mail.AuthenticationFailedException: 535 5.7.0 ERR.LOGIN.NONSSL

导入jar包
compile('org.apache.commons:commons-email:1.3.3')

/**
     * 发送邮件给指定的人
     *
     * @param toEmial 邮件
     * @param subject 主题
     * @param content 内容
     * @return
     * @throws Exception
     */
    public String sendEmail(String[] toEmial, String subject, String content) {
        try {
            HtmlEmail simpleEmail = new HtmlEmail();
            simpleEmail.setHostName(getHostName());
            simpleEmail.setAuthentication(this.getFromAddr(), this.getMailPassword());
            simpleEmail.setFrom(getFromAddr(), getFromName());
            simpleEmail.addTo(toEmial);
            simpleEmail.setSubject(subject);
            simpleEmail.setMsg(content);
            //add
            simpleEmail.setSSLOnConnect(true);
            simpleEmail.setCharset(StandardCharsets.UTF_8.name());
            
            return simpleEmail.send();
        } catch (Exception e) {
            logger.error("发送邮件异常:", e);
        }
        return null;
    }

 

ssl邮箱默认端口号:465 

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM