3
    MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress("xyz@example.com"));
msg.addRecipient(Message.RecipientType.TO, new InternetAddress("abcd@example.com"));

Getting error in last two lines that unhandled exception type MessagingException.

Eclipse suggests me to cover them with a try catch block.But when i tried to search in internet they are using these two statement without any try catch block. so i think i am doing something wrong. how to solve this?

Dev
  • 378
  • 2
  • 16
  • [Lessons: Exceptions (The Java™ Tutorials)](https://docs.oracle.com/javase/tutorial/essential/exceptions/) – Seelenvirtuose Apr 09 '15 at 08:35
  • Could you please check in the internet that they may be throws MessagingException instead of covering try catch block. – Subbu Apr 09 '15 at 08:39
  • possible duplicate of [Differences between Runtime/Checked/Unchecked/Error/Exception](http://stackoverflow.com/questions/3162760/differences-between-runtime-checked-unchecked-error-exception) – Jaroslaw Pawlak Apr 09 '15 at 09:10

1 Answers1

1

You have to possibilities to solve it.

  1. Add a try-catch- block arround your code:

    try { MimeMessage msg = new MimeMessage(session); msg.setFrom(new InternetAddress("xyz@example.com")); msg.addRecipient(Message.RecipientType.TO, new InternetAddress("abcd@example.com"));

    } catch (MessagingException me){ // do something }

  2. Add throws MessagingException to the method signature:

    public void mymethod throws MessagingException { MimeMessage msg = new MimeMessage(session); msg.setFrom(new InternetAddress("xyz@example.com")); msg.addRecipient(Message.RecipientType.TO, new InternetAddress("abcd@example.com")); }

Jens
  • 67,715
  • 15
  • 98
  • 113