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

2024-10-09 08:35:01 浏览数 (1)

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')

代码语言:javascript复制
/**
     * 发送邮件给指定的人
     *
     * @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 

0 人点赞