1. 程式人生 > >org.springframework.mail.MailSendException: Failed messages: javax.mail.SendFailedException: Invalid Addresses

org.springframework.mail.MailSendException: Failed messages: javax.mail.SendFailedException: Invalid Addresses

toa false ssa net 代碼 ati private res erb

問題

  • org.springframework.mail.MailSendException: Failed messages: javax.mail.SendFailedException: Invalid Addresses

  • 分析:可能是收件人或抄送人列表存在無效的地址

坑:不能直接cach到SendFailedException

解決方案

遍歷異常,提起無效地址後過濾原地址列表再次發送

具體代碼如下:

1.發郵件方法代碼

    /**
     * 發送html郵件
     *
     * @param to
     * @param cc
     * @param subject
     * @param content
     */
    public void sendHtmlMail(String[] to, String[] cc, String subject, String content) {
        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper helper = null;
        try {
            //true表示需要創建一個multipart message
            helper = new MimeMessageHelper(message, true);
            helper.setFrom(from);
            helper.setTo(to);
            helper.setValidateAddresses(false);
            helper.setCc(cc);
            helper.setSubject(subject);
            helper.setText(content, true);

            mailSender.send(message);
            logger.info("sendHtmlMail success.from:" + from);
        } catch (Throwable e) {
            logger.error("sendHtmlMail fail.", e);
            String[] invalid = getInvalidAddresses(e);
            if (invalid != null) {
                sendHtmlMail(filterByArray(to, invalid), filterByArray(cc, invalid), subject, content);
            }
        }
    }

2.從異常獲取無效地址的方法代碼

    /**
     * 從異常獲取無效地址
     * @param e
     * @return
     */
    private static String[] getInvalidAddresses(Throwable e) {
        if (e == null) {
            return null;
        }
        if (e instanceof MailSendException) {
            System.out.println("e instanceof SendFailedException");
            Exception[] exceptions = ((MailSendException) e).getMessageExceptions();
            for (Exception exception : exceptions) {
                if (exception instanceof SendFailedException) {
                    return getStringAddress(((SendFailedException) exception).getInvalidAddresses());
                }
            }
        }
        if (e instanceof SendFailedException) {
            return getStringAddress(((SendFailedException) e).getInvalidAddresses());
        }
        return null;
    }

    /**
     * 將Address[]轉成String[]
     * @param address
     * @return
     */
    private static String[] getStringAddress(Address[] address) {
        List<String> invalid = new ArrayList<>();
        for (Address a : address) {
            String aa = ((InternetAddress) a).getAddress();
            if (!StringUtils.isEmpty(aa)) {
                invalid.add(aa);
            }
        }
        return invalid.stream().distinct().toArray(String[]::new);
    }

3.過濾發件人中無效地址的方法代碼

    /**
     * 過濾數組source,規則為數組元素包含了數組filter中的元素則去除
     *
     * @param source
     * @param filter
     * @return
     */
    private static String[] filterByArray(String[] source, String[] filter) {
        List<String> result = new ArrayList<>();
        for (String s : source) {
            boolean contains = false;
            for (String f : filter) {
                if (s.contains(f)) {
                    contains = true;
                    break;
                }
            }
            if (!contains) {
                result.add(s);
            }
        }
        return result.stream().toArray(String[]::new);
    }

org.springframework.mail.MailSendException: Failed messages: javax.mail.SendFailedException: Invalid Addresses