0

I have a class that have a method that sends an email using javax.mail .I've compiled the code and it was working fine,then suddenly I started to get InvocationTargetException and I don't know what is causing it.

Here is my code :

    import java.lang.reflect.InvocationTargetException;
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;

public class SendEmailClass {

    private static String USER_NAME = "email@domain.com"  ; 
    private static String PASSWORD = "Password";  
    private static String RECIPIENT = "email@domain.com";

 private SendEmailClass () {

    }




    public static String getStr (String str)   {


        String from = USER_NAME;
        String pass = PASSWORD;
        String[] to = { RECIPIENT }; // list of recipient email addresses
        String subject = "Test Email";
        String body = "mail notification  " + str ;

        SendEmail(from, pass, to, subject, body);



return "test" + str ;

    }



 public static void SendEmail(String from, String pass, String[] to, String subject, String body)  {


        Properties props = System.getProperties();
        String host = "Some IP Address";
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.host", host);
        props.put("mail.smtp.user", from);
        props.put("mail.smtp.password", pass);
        props.put("mail.smtp.port", "587");
        props.put("mail.smtp.auth", "true");

        Session session = Session.getDefaultInstance(props);
        MimeMessage message = new MimeMessage(session);




        try {
            message.setFrom(new InternetAddress(from));
            InternetAddress[] toAddress = new InternetAddress[to.length];

            // To get the array of addresses
            for( int i = 0; i < to.length; i++ ) {
                toAddress[i] = new InternetAddress(to[i]);
            }

            for( int i = 0; i < toAddress.length; i++) {
                message.addRecipient(Message.RecipientType.TO, toAddress[i]);
            }

            message.setSubject(subject);
            message.setText(body);
            Transport transport = session.getTransport("smtp");
            transport.connect(host, from, pass);
            transport.sendMessage(message, message.getAllRecipients());
            transport.close();


        }

        catch (AddressException ae) {
            ae.printStackTrace();
        }
        catch (MessagingException me) {
            me.printStackTrace();
        }



    }






}

When I invoke method "getStr" I get that exception ,I tried to handle it by putting catch block after at the end of "SendEmail" method , so the method becomes like this :

public static void SendEmail(String from, String pass, String[] to, String subject, String body)  {


        Properties props = System.getProperties();
        String host = "40.101.62.34";
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.host", host);
        props.put("mail.smtp.user", from);
        props.put("mail.smtp.password", pass);
        props.put("mail.smtp.port", "587");
        props.put("mail.smtp.auth", "true");

        Session session = Session.getDefaultInstance(props);
        MimeMessage message = new MimeMessage(session);




        try {
            message.setFrom(new InternetAddress(from));
            InternetAddress[] toAddress = new InternetAddress[to.length];

            // To get the array of addresses
            for( int i = 0; i < to.length; i++ ) {
                toAddress[i] = new InternetAddress(to[i]);
            }

            for( int i = 0; i < toAddress.length; i++) {
                message.addRecipient(Message.RecipientType.TO, toAddress[i]);
            }

            message.setSubject(subject);
            message.setText(body);
            Transport transport = session.getTransport("smtp");
            transport.connect(host, from, pass);
            transport.sendMessage(message, message.getAllRecipients());
            transport.close();


        }

        catch (AddressException ae) {
            ae.printStackTrace();
        }
        catch (MessagingException me) {
            me.printStackTrace();
        }

        catch (InvocationTargetException ex){
            // handle exception
        }

    }






}

But then I get that : "Unreachable catch block for InvocationTargetException. This exception is never thrown from the try statement body"

How do I handles this and find what causing the exception? Please help .

MoeCaruso
  • 35
  • 5
  • it means you should remove this: catch (InvocationTargetException ex){ // handle exception } because this can never be the situation – Stultuske Sep 24 '18 at 10:14
  • Yes,but when I remove it,then I get InvocationTargetException. I want to know what is the real error is.How it says it cant be thrown?! I am confused. – MoeCaruso Sep 24 '18 at 10:16
  • could you show more of your stacktrace? also, it is possible that AddressException and/or MessageException are childclasses of InvocationTargetException, which may lead to this. – Stultuske Sep 24 '18 at 10:18
  • It could also be that you are using two different type of InvocationTargetException (different packages) – Stultuske Sep 24 '18 at 10:21
  • Show stacktrace, including pointers to the lines in your code. – Thorbjørn Ravn Andersen Sep 24 '18 at 10:22
  • I only get the words "InvocationTargetException" – MoeCaruso Sep 24 '18 at 10:23
  • ... you should get more. at least, if that Exception is thrown. – Stultuske Sep 24 '18 at 10:30
  • Well I don't know what happened,but I just moved the code to another test environment and in different server and and compiled it and it works ! Maybe its something related to Java version. – MoeCaruso Sep 24 '18 at 10:42

1 Answers1

0

The compiler is telling you that you have code that tries to handle an InvocationTargetException which it can tell will never be thrown so it is superfluous, and can be safely deleted without changing the behavior of your program.

Delete this catch block and try again.

Thorbjørn Ravn Andersen
  • 73,784
  • 33
  • 194
  • 347
  • you may want to read the comments. That was my first guess as well, but he replies to it, that when he deletes it, an InvocationTargetException is thrown. – Stultuske Sep 24 '18 at 10:20