javax.mail.getValidSentAddresses()

729 views Asked by At

I updated my code:

1) I added the following properties:

        Properties props=new Properties();
        props.put(smtp,host);
        props.put("mail.smtp.reportsuccess","true");
        props.put("mail.smtp.sendpartial", "true");

Then written this block as directed in answer:

        }catch (MessagingException mex){   

        Exception ex = mex;

            do {
                if(ex instanceof SendFailedException) {
                SendFailedException sfe = (SendFailedException) ex;

                Address[] vsa   = sfe.getValidSentAddresses();                                  
                Address[] vua   = sfe.getValidUnsentAddresses();
                Address[] ia    = sfe.getInvalidAddresses();

                if(vsa !=null || vsa.length>0){
                    String validSentAddresses = vsa[0].toString();              
                    printReport("GSWvalidSentAddresses.txt", validSentAddresses);
                }
                else if(vua !=null || vua.length>0){
                    String validUnsentAddresses = vua[0].toString();
                    printReport("GSWvalidUnsentAddresses.txt", validUnsentAddresses);
                }
                else if(ia !=null || ia.length>0){              
                    String invalidAddresses = ia[0].toString();
                    printReport("GSWinvalidAddresses.txt", invalidAddresses);
                }
                else{}                                      
                        if (ex instanceof MessagingException)
                        ex = ((MessagingException) ex).getNextException();
                    else
                        ex = null;
                }// 
            } while (ex != null);
    }//main catch block     
    }

when it ran throws 504 Gateway Time-out--------nginx

Please advise

Thanks in anticipation

1

There are 1 answers

13
jmehrens On BEST ANSWER

Set the mail.smtp.reportsuccess session property to true. This will cause Transport.send to always throw SendFailedException. Per the documentation:

When sending a message, detailed information on each address that fails is available in an SMTPAddressFailedException chained off the top level SendFailedException that is thrown. In addition, if the mail.smtp.reportsuccess property is set, an SMTPAddressSucceededException will be included in the list for each address that is successful. Note that this will cause a top level SendFailedException to be thrown even though the send was successful.

The use of the word 'chained' means you have to call MessagingException.getNextException().

Also of interest is to you is the 'mail.smtp.sendpartial' property which is also covered in the documentation.

You should also change the code to use Session.getInstance.