Reputation: 298
I have created a java program for sending mail and I import some package that is related to the program. I have downloded package javax.mail and javax.activation and put it in to the C:\Program Files\Java\jdk1.7.0\jre\lib\ext
I compile my program then it compiles but when I run it throws Exception.
I am not able to understand why it is throwing exception.
my code is here.
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
import java.net.*;
public class SendEmail
{
public static void main(String [] args)
{
// Recipient's email ID needs to be mentioned.
String to = "[email protected]";
// Sender's email ID needs to be mentioned
String from = "[email protected]";
try{
// Assuming you are sending email from localhost
String host = InetAddress.getLocalHost().toString();
// Get system properties
Properties properties = System.getProperties();
// Setup mail server
properties.setProperty("mail.smtp.host", host);
// Get the default Session object.
Session session = Session.getDefaultInstance(properties);
try{
// Create a default MimeMessage object.
MimeMessage message = new MimeMessage(session);
// Set From: header field of the header.
message.setFrom(new InternetAddress(from));
// Set To: header field of the header.
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(to));
// Set Subject: header field
message.setSubject("This is the Subject Line!");
// Now set the actual message
message.setText("This is actual message");
// Send message
Transport.send(message);
System.out.println("Sent message successfully....");
}catch (MessagingException mex) {
mex.printStackTrace();
}}catch(Exception e){}
}
}
Upvotes: 1
Views: 2008
Reputation: 72626
You need to download JavaMail API package and put it into the classpath, but make sure to include mail.jar and all dependant libraries that you can find into the lib folder.
To set the classpath from the command line :
java -cp "mail.jar;lib/*" SendEMail
Upvotes: 1