我收到InvocationTargetException,但我不知道如何知道真正的错误?

时间:2018-09-24 10:10:44

标签: java javamail invocationtargetexception

我有一个类,该类具有使用javax.mail发送电子邮件的方法。我已经编译了代码,并且运行良好,然后突然我开始收到InvocationTargetException,不知道是什么原因引起的。

这是我的代码:

    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();
        }



    }






}

当我调用方法“ getStr”时,我得到了该异常,我试图通过在“ SendEmail”方法的末尾放置catch块来处理该异常,因此该方法如下所示:

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
        }

    }






}

但是我得到的是:“ InvocationTargetException的不可达捕获块。永远不会从try语句主体中抛出此异常”

我该如何处理并查找导致异常的原因? 请帮忙。

1 个答案:

答案 0 :(得分:0)

编译器告诉您,您有尝试处理InvocationTargetException的代码,该代码可以 tell 永远不会被抛出,因此它是多余的,可以在不更改程序行为的情况下安全删除。

删除此catch块,然后重试。