Share on Google+Share on Google+

Java Mail Assimilator Example

This Example shows you how to send a message using javamail api. A client creates new message by using Message subclass.

Java Mail Assimilator Example


This Example shows you how to send a message using javamail api. A client creates new message by using Message subclass. It sets attributes like  recipient address and the subject, and inserts the contentinto the Message object, and inserts the content into  the Message object. Finally, it sends the Message by invoking the Transport.send()  method. The Transport class models the transport agent that routes a message to its  destination addresses. This class provides methods that send a message to a list of  recipients. Invoking the Transport.send() method with a Message object identifies the appropriate transport based on its destination addresses.

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

public class Assimilator { 

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

  String host = "";
Address from = new InternetAddress("test@localhost", "test");
Address to = new InternetAddress("test@localhost"); 

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

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

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

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

  // Set the RFC 822 "From" header field using the
// value of the InternetAddress.getLocalAddress method.

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

  // Set the "Subject" header field.
  msg.setSubject("Resistance is futile.");

  // Sets the given String as this part's content,
// with a MIME type of "text/plain".
msg.setContent("Resistance is futile. You will be assimilated!" , "text/plain");

   //Send mail
System.out.println("msg send...");


Output :

msg send...

Download code



Posted on: June 15, 2009 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Discuss: Java Mail Assimilator Example  

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image