首页 > 解决方案 > 我得到 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 的无法到达的 catch 块。这个异常永远不会从 try 语句体中抛出”

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

标签: javajakarta-mailinvocationtargetexception

解决方案


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

删除此 catch 块并重试。


推荐阅读