As you might already know, we can use various APIs (generally Java Beans Components) provided by various vendors to carryout the tasks like printing, dealing with fax messages, scanning the images, accepting the credit card payment.

We can develop an application for sending and receiving the mails using Java mail API (J2EE related API) or we can use Java Beans Components provided by various companies like JScape etc.

In this post, I'm going to explain Java mail API i.e.; sending and receiving emails etc.

In order to exchange the mails, we must use the email server. Typically, there will be one machine in a company on which the administrators install a mail server and a mail client (Microsoft outlook) will be setup on all the other machines.

Please download James mail server @http://james.apache.org/download.cgi in order to implement below program.

Procedures for setting up JAMES mail server

  1. Extract the contents of the file james-3.0.1.zip (the file that you've downloaded from the above URL) using the following command

    jar xf james-3.0.1.zip
  2. To start james server, follow below steps
    • Use the following command to set Java home. (Replace C:\Java1.5 with the Java folder where you've installed Java.)

      SET JAVA_HOME=C:\Java1.5\
    • Using CD command, move to bin directory of james folder extracted in the first step.
    • Run startup.bat to start james mail serv er
  3. An account should be created in order to proceed with sending email. Here the steps to create user accounts
    • Start telnet client
    • Use the following command with telnet client to establish connection with the james server
      open localhost 4555
    • Provide "root" as user name, "root" as password.
    • To add a user use the command shown below
      adduser santhosh santhosh
SendMail.java

/**
* A demo class to send email
* @author Santhosh Reddy Mandadi
* @since 28-Jun-2012
* @version 1.0
*/

import com.jscape.inet.smtp.Smtp;
import com.jscape.inet.email.EmailMessage;

public class SendMail
{
public static void main(String args[])
{
try
{
Smtp smtp = new Smtp("localhost");
EmailMessage message = new EmailMessage();
message.setTo("msreddy61184@gmail.com");
message.setFrom("santhosh@javaservletsjspweb.in");
message.setSubject("First email...");
message.setBody("This is a sample mail\r\n Thanks");
smtp.connect();
smtp.send(msg);
smtp.disconnect();
}
catch(Exception e)
{
System.out.println(e);
}
}
}
CheckMail.java

/**
* A demo class to check emails in your inbox
* @author Santhosh Reddy Mandadi
* @since 28-Jun-2012
* @version 1.0
*/

import com.jscape.inet.pop.Pop;
import com.jscape.inet.email.EmailMessage;
import java.util.*;

public class CheckMail
{
public static void main(String args[])
{
try
{
Pop pop = new Pop("localhost", "santhosh", "santhosh");
pop.connect();
Enumeration e = pop.getMessages();
while(e.hasMoreElements())
{
EmailMessage message = (EmailMessage)e.nextElement();
System.out.println(message.getSubject()+" from "+message.getFrom());
System.out.println(message.getBody());
System.out.println("**************************");
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}
Please note that the above program will be compiled only when you set the classpath to refer to the james-mail API (jar which is part of the above downloaded file).