Java Send Email (In a Jar)

| Posted by watashii | Filed under Java, Programming

Step 1

Download the JavaMail and Activation Framework library .jar files

• If using Java SE, you’ll need to download JavaMail, eg mail-1.4.3.jar

• If using below Java SE 6, you’ll need to download and include Activation Framework, eg activation-1.1.1.jar

Step 2

In the same directory, create the following java source code and save it as Send.java, with class named Send.  You will need to customise the host/port/to/from entries to your own SMTP mail server settings.

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

public class Send { 

    public static void main(String args[]) throws Exception { 

        String host = "smtp.domain.com";
        String port = "25";
        String from = "sender@domain.com";
        String to = "recipient@domain.com";

        // Get system properties
        Properties properties = System.getProperties(); 

        // Setup mail server
        properties.setProperty("mail.smtp.host", host);
        properties.setProperty("mail.smtp.port", port); 

        // Get the default Session object.
        Session session = Session.getDefaultInstance(properties); 

        // Create a default MimeMessage object.
        MimeMessage message = new MimeMessage(session); 

        // Set the RFC 822 "From" header field using the
        // value of the InternetAddress.getLocalAddress method.
        message.setFrom(new InternetAddress(from));

        // Add the given addresses to the specified recipient type.
        message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));

        // Set the "Subject" header field.
        message.setSubject("This is the subject"); 

        // Sets the given String as this part's content,
        // with a MIME type of "text/plain".
        message.setText("This is the email body."); 

        // Send message
        Transport.send(message);
        System.out.println("Message Sent.....");
    }
}

Step 3

Compile the Send.java, which will create a Send.class file.  Note we must also specify the classpath of our downloaded jar libraries when we compile, otherwise it would not know how to find external JavaMail classes.

$ javac -classpath activation-1.1.1.jar;mail-1.4.3.jar Send.java

Step 4

Create a manifest plaintext file: MANIFEST.MF, containing the class name with the main-method, and the classpath of the jar files from step 1. Note the jars are in the same directory (relative) as our Send.class file.  Also make sure the last line of this file must be emtpy, since it is always rejected.

Manifest-Version: 1.0
Main-Class: Send
Class-Path: activation-1.1.1.jar mail-1.4.3.jar

Step 5

Combining our class and manifest file, we create a jar file named SendEmail.jar using the following command:

$ jar cmf MANIFEST.MF SendEmail.jar Send.class

Step 6

To run our send-email program, we only need the following 3 files: SendEmail.jar, activation-1.1.1.jar, and mail-1.4.3.jar.  Place them together in the same directory and run the following command:

$ java -jar SendEmail.jar

Share:

  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Print
  • email

Related Posts:

  1. Setup Hibernate Log4j on Eclipse (Basic Tutorial)

Tags: , , ,

Leave a Reply