SMTP 给多个发送人发送邮件,有的邮件无法发送,导致出异常,无法正常发送邮件

我用了之前的发送邮件方法 ,有人反映好几天没有发送 发送失败了,排查原因 发现是有人离职后账号注销了

导致 发送失败  查到一个解决方案 如下

        String serverIP = "";
        try {
            InetAddress address = InetAddress.getLocalHost();
            serverIP = address.getHostAddress();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        if (serverIP.equals("")) { // 判断服务器IP

            Map<String, String> map = new HashMap<String, String>();

            List<String> address = new ArrayList<>();
            try {

                String mailAddress2 = null;
                List<FmsAttendanceExceptionModel> query = fmsAttendanceExceptionService.queryList(new FmsAttendanceExceptionModel());
                for (FmsAttendanceExceptionModel fae : query) {
                    mailAddress2 = mstOrganizationMailMapper.getMailAddress(fae.getDepartments());
                    map.put(mailAddress2, mailAddress2);
                }
                for (String key : map.keySet()) {
                    address.add(key);
                }
                String users = String.join(",", address);
                ExcelGenerate excelGenerate = null;
                // 写log记录
               try {
                    List<Object> export2 = fmsAttendanceExceptionService.getExport(new FmsAttendanceExceptionModel(), "通知", excelGenerate, query);
                    ExcelGenerate export = (ExcelGenerate) export2.get(0);
                    //List<ScheduleEvaluationModel> list2 = (List<ScheduleEvaluationModel>) export2.get(1);
                    // 获取输出流
                    InputStream input = null;
                    // 获取输出流
                    ByteArrayOutputStream bos = new ByteArrayOutputStream();
                    export.write(bos);
                    input = new ByteArrayInputStream(bos.toByteArray());
                    //
                   try {
                        MailService.sendMail(users, "****.com", "信息", input, sysParameterMapper.queryvalue(ConstantsParameter.EXMAIL_PASSWORD));
                    } catch (SendFailedException e) { 
                        //进入这个异常就代表 出现了 某些 邮箱地址 是错误的情况  可能有人离职 邮箱 注销了
                        //log.info("存在邮箱无法发送,过滤异常邮箱再次发送
");
                        //获取 正确的地址
                        Address[] validAddresses = e.getValidUnsentAddresses();
                        if(validAddresses!=null){
                            String[] successEmail = new String[validAddresses.length];
                            for(int k=0;k<validAddresses.length;k++){
                                successEmail[k] = validAddresses[k].toString();
                            }
                             users = String.join(",", successEmail);
                            MailService.sendMail(users, "", "信息", input, sysParameterMapper.queryvalue(ConstantsParameter.EXMAIL_PASSWORD));
                        }
                    }
                    log.info("成功
");
                } catch (Exception e) {
                    log.info("失败
");
                    System.out.println(e);
                }

            } catch (Exception e) {
                log.info("失败
");
                System.out.println(e);

            }

        }

    
原文地址:https://www.cnblogs.com/Mr-Y1907/p/14601954.html