package javax.mail and javax.mail.internet do not exist

saplingPro picture saplingPro · Jul 7, 2011 · Viewed 182.4k times · Source

When I compile a simple code that has the following 2 import statements:

import javax.mail.*

import javax.mail.internet.*

I get the following message:

package javax.mail does not exist

package javax.mail.internet does not exist

Why do I get this error?

Here is the code I have:

import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;

class tester {
 public static void main(String args[]) {
   Properties props = new Properties();
   props.put("mail.smtp.com" , "smtp.gmail.com");
   Session session  = Session.getDefaultInstance( props , null);
   String to = "[email protected]";
   String from = "[email protected]";
   String subject = "Testing...";
   Message msg = new MimeMessage(session);
    try {
      msg.setFrom(new InternetAddress(from));
      msg.setRecipient(Message.RecipientType.TO , new InternetAddress(to));
      msg.setSubject(subject);
      msg.setText("Working fine..!");
    }  catch(Exception exc) {
       }
 }
}

Answer

Jon Skeet picture Jon Skeet · Jul 7, 2011

You need to download the JavaMail API, and put the relevant jar files in your classpath.