【问题标题】:How to trap nested SMTPAddressFailedException如何捕获嵌套的 SMTPAddressFailedException
【发布时间】:2019-10-14 11:23:23
【问题描述】:

我的应用对电子邮件收件人有一个循环,其中一些人的地址可能不正确。当遇到一个坏的,我希望它报告给用户并继续循环。问题是即使我捕获了所有可能的异常,但 SMTPAddressFailedException 却被 javax 捕获:

javax.mail.SendFailedException:无效地址; 嵌套异常是: com.sun.mail.smtp.SMTPAddressFailedException: 550 5.1.1 : 收件人地址被拒绝:虚拟邮箱表中的用户未知

其余收件人将被跳过。处理这个问题的正确方法是什么?

这是代码;

            List<Entry> theEntries = theConcours.GetEntriesList();
        for(Entry entry : theEntries){
            MasterPersonExt mp = aConcours.GetMasterPersonnelObject().GetMasterPerson(entry.GetOwnerUnique()) ;
            String owneremail = mp.getEmail();
            String ownername = mp.getFirstName() + " " + mp.getLastName();
            curOwnerUniqueName = mp.getUniqueName();
            System.out.println("Processing " + mp.getUniqueName() + " email: " + owneremail);
            MimeMessage message = new MimeMessage(session);  
            try {  
                message.setFrom(new InternetAddress(mailuser));
                System.out.println("From set while processing " + curOwnerUniqueName);
            } catch (AddressException ex) {
                String msg = "AddressException for: " + mailuser + " while processing " + curOwnerUniqueName;
                //Logger.getLogger(SendMailSSL.class.getName()).log(Level.SEVERE, null, ex);
                okDialog(msg);
                aConcours.GetLogger().log(Level.INFO, msg, ex);
                continue;
            } catch (MessagingException ex) {
               // Logger.getLogger(SendMailSSL.class.getName()).log(Level.SEVERE, null, ex);
                String msg = "MessagingException for: " + mailuser + " while processing " + curOwnerUniqueName;
                //Logger.getLogger(SendMailSSL.class.getName()).log(Level.SEVERE, null, ex);
                okDialog(msg);
                aConcours.GetLogger().log(Level.INFO, msg, ex);
                continue;
            }
            try {  
                message.setSubject("Your JCNA Concours Entry");
            } catch (MessagingException ex) {
               // Logger.getLogger(SendMailSSL.class.getName()).log(Level.SEVERE, null, ex);
                String msg = "MessagingException setting Subject while processing " + curOwnerUniqueName ;
                //Logger.getLogger(SendMailSSL.class.getName()).log(Level.SEVERE, null, ex);
                okDialog(msg);
                aConcours.GetLogger().log(Level.INFO, msg, ex);
                continue;
            }
            InternetAddress ownerInternetAddr;
             try {
                ownerInternetAddr = new InternetAddress(owneremail);
            } catch (AddressException ex) {
                String msg = "Bad judge eMail address for " + curOwnerUniqueName + "  \"" + mp.getEmail() + "\"";
                okDialog(msg);
                Logger.getLogger(SendMailSSL.class.getName()).log(Level.INFO, msg, ex);
                continue;
            }
            try {
                ///
                message.addRecipient(Message.RecipientType.TO, ownerInternetAddr);
            } catch (MessagingException ex) {
               // Logger.getLogger(SendMailSSL.class.getName()).log(Level.SEVERE, null, ex);
                String msg = "MessagingException setting TO while processing " + curOwnerUniqueName + "  \"" + mp.getEmail() + "\"";
                okDialog(msg);
                Logger.getLogger(SendMailSSL.class.getName()).log(Level.INFO, msg, ex);
                continue;
            }
            String ownerFirst = entry.GetOwnerFirst();
            String  entryplacard = "http://www.concoursbuilder.us/manual-uploads/" + theConcours.GetHostClub() + "/" + concoursName + "/Placards/" + entry.GetOwnerLast() + "_" +  entry.GetUniqueDescription() + "-Placard.pdf";
            String jagModel = entry.GetModel();
            String[] theVals = {ownerFirst, jagModel, concoursName, judgingStarts, lunchTime,
                                awardsTime, concoursChairFirstLastName, concoursChairEmail, entryplacard, schedbyclass};
            messageContent  = replaceTagsWithValues(content_template, theTags, theVals);
            try {
                message.setContent(messageContent, "text/html; charset=utf-8");
            } catch (MessagingException ex) {
                //Logger.getLogger(SendMailSSL.class.getName()).log(Level.SEVERE, null, ex);
                String msg = "MessagingException setting Content while processing " + curOwnerUniqueName + "  \"" + mp.getEmail() + "\"";
                okDialog(msg);
                Logger.getLogger(SendMailSSL.class.getName()).log(Level.INFO, msg, ex);
                continue;
           }
            //send the message  
            try {
                Transport.send(message); 
                System.out.println("message sent successfully...");  
            } catch (SMTPAddressFailedException ex){
                String msg = "SMTPAddressFailedException while processing Owner " + ownername + " email address " + owneremail + " is invalid.";
                System.out.println(msg);
                okDialog(msg);
                aConcours.GetLogger().log(Level.INFO, msg, ex); 
                continue;
            } catch (MessagingException ex) {
               // Logger.getLogger(SendMailSSL.class.getName()).log(Level.SEVERE, null, ex);
                String msg = "MessagingException while processing Owner " + ownername + " email address " + owneremail;
                System.out.println(msg);
                okDialog(msg);
                aConcours.GetLogger().log(Level.INFO, msg, ex); 
                continue;
            }
        }

【问题讨论】:

  • 也许为已经扔掉的条目创建另一个列表...(完成列表)。并调用一个函数,该函数采用整个列表和完成列表(起初为空)。运行循环跳过包含在已完成列表中的任何内容...随时添加到已完成列表中。抛出异常时再次调用该函数。
  • 或者,在函数中创建条目列表的副本并在执行过程中删除值...然后在捕获异常时使用复制的列表重新调用函数。您可能还想调用一个函数,在遇到异常时从源数据中删除电子邮件。

标签: java email-validation


【解决方案1】:

我通过实现一个名为 sendMailToAnEntrant() 的辅助函数解决了这个问题。然后,for 循环在 try-catch 中对 sendMailToAnEntrant() 进行了一次调用。这导致 javax 代码抛出的异常被捕获。似乎有效。

            for(Entry entry : theEntries){
        try {
            //sendMailToAnEntrant(Concours aConcours, String aFromEmailAddress, String aSubject, String aContentTemplate, String [] aTags, Entry aEntry, Session aSession)
            sendMailToAnEntrant(aConcours, mailuser, "Your JCNA Concours Entry", content_template, theTags, entry, session);
        } catch (MessagingException ex) {
            MasterPersonExt mp = aConcours.GetMasterPersonnelObject().GetMasterPerson(entry.GetOwnerUnique()) ;
            String owneremail = mp.getEmail();
            String ownername = mp.getFirstName() + " " + mp.getLastName();

            String msg = "Messaging Exception in sendMailToAnEntrant() for: " + ownername + " Email: " + owneremail;
            System.out.println(msg);
            okDialog(msg);
            aConcours.GetLogger().log(Level.INFO, msg, ex);
            //Logger.getLogger(SendMailSSL.class.getName()).log(Level.SEVERE, null, ex);
            continue;
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-06
    • 2011-11-27
    • 2013-02-07
    • 2016-05-30
    • 2012-04-15
    • 2018-04-18
    相关资源
    最近更新 更多