0

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

Raakh
  • 67
  • 2
  • 3
  • 12

1 Answers1

0

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.

jmehrens
  • 10,580
  • 1
  • 38
  • 47
  • I followed your directives and did as: Properties props=new Properties(); props.put(smtp,host); props.put("mail.smtp.reportsuccess","true"); Session session1 = Session.getDefaultInstance(props,null); Message msg =new MimeMessage(session1); but nothing happened. – Raakh Jun 11 '15 at 13:25
  • its throwing this log: com.sun.mail.smtp.SMTPSendFailedException: 250 ok 1434193689 qp 3482
    ;
    nested exception is:
    com.sun.mail.smtp.SMTPAddressSucceededException: 250 ok

    at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:598)
    at javax.mail.Transport.send0(Transport.java:169)
    at javax.mail.Transport.send(Transport.java:98)
    at SendEmail.send(SendEmail.java:76)
    at promoClass.promoLoop(promoClass.java:67)
    at pNews.doGet(pNews.java:219)
    at javax.servlet.http.HttpServlet.
    – Raakh Jun 13 '15 at 11:10
  • I updated my code in my very first post in this thread. Please check and let me know where I am making mistake that files are not created and writing validSentAddress, invalidSentAddress, validNotSentAddress if my code is correct. If my code is not correct then let me know where I am making mistake – Raakh Jun 17 '15 at 13:15
  • }catch (SendFailedException e){ I am catching SFE is that right or change to message or address exception to work my code? – Raakh Jun 17 '15 at 18:44
  • Looks like your if condtions are wrong. You should check if the array is not null or array.length is not zero. Your code is checking the length of the first address which is incorrect. – jmehrens Jun 20 '15 at 02:35
  • Thanks for correcting my code. It was really a big mistake – Raakh Jun 24 '15 at 09:39
  • I am seeing its writing all emails as validSentAddresses and not working for other conditions. Please advise – Raakh Jun 25 '15 at 16:50
  • I've updated the answer. You need to look at walking through the exception cause chain which will make your code more complex. – jmehrens Jun 25 '15 at 17:16
  • Great! I am learning everyday and nobody can believe from this one post how much I learnt. Thanks from the bottom of my heart – Raakh Jun 27 '15 at 17:01
  • Now I am getting java.lang.ArrayIndexOutOfBoundsException: 0 if(ia !=null || ia.length>0){ String invalidAddresses = ia[0].toString(); printReport("GSWinvalidAddresses.txt", invalidAddresses); } – Raakh Jun 29 '15 at 13:20
  • I updated my first post to show the code that is done whatever you recommended but unfortunately still I am having problem. Please read my first updated post – Raakh Jun 29 '15 at 17:48
  • Change your if's from OR to AND. You need to use a nested for loop to log all addresses in each array. Your code is still looking at only the first address. – jmehrens Jul 01 '15 at 01:53
  • I changed OR to AND and added for loop. Thanks from the bottom of my heart – Raakh Jul 02 '15 at 04:57