I am trying to send email to user provided email id and I am getting below :
org.springframework.mail.MailSendException:
Failed messages: javax.mail.SendFailedException: Invalid Addresses;
nested exception is:
com.sun.mail.smtp.SMTPAddressFailedException
Now the problem is I can catch MailSendException and send a Meaningful error message to Client saying, please provide valid emailId, but MailSendException is a very generic exception to catch and catching it doesn't guarantee that the exception has occurred because of invalid email id. This exception can occur for various other reasons.
My Problem is that I want to catch SMTPAddressFailedException but unfortunately in this case it is a nested exception and the actual exception thrown by Spring is a very generic MailSendException. Please suggest
I figured it out this way. The Spring's MailSendException has
getMessageExceptions()
which will contain all the nested root exception. This was my requirement as I wanted to find out whether the nested root exception is SMTPAddressFailedException or not. Turns out the nested exception I got fromgetMessageExceptions()
was javax mailSendFailedException
so I had to use thatSendFailedException
getNextException() to find out the nested root exception. A little lengthy but totally worth it to find out exact nested root exception: