Javamail

JavaMail Projects

JavaMail Project 1

adplus-dvertising
Javamail

Javamail is a Java API which is used to send and receive email through SMTP,.POP3 and IMAP . JavaMail is built into the Java EE platform, but also provides an optional package for use in Java SE. The JavaMail API is a platform-independent and protocol-independent framework which is used to build mail and appliction messaging.

JavaMail is a set of abstract classes that create a framework for sending, receiving and handling e-mail, along with implementations of those classes. IMAP and SMTP allowing you to get started immediately on sending and receiving mail. The javax.mail package definesclasses that are common to all mail systems.

Javamail can be used to create personal mail filters, simple mailing lists and customized personal mail applications, as well as to add full e-mail capabilities to an Enterprise application or create a full-fledged e-mail client.

JavaMail Tutorials and Interview Question
Role of SMTP, IMAP and POP3

SMTP Simple Mail Transfer Protocol

allows two mail servers to communicate using a simple language, and provides a step-by-step protocol for exchanging information.

IMAP Internet Mail Access Protocol

It is client/server mail protocol. SMTP delivers mail to a central location, where the user can either log in and read it directly or use a client/server mail protocol to read it remotely. IMAP is keep mail on a remote server and let the user interact with it there.

POP3 Post Office Protocol

POP3 is forward a user's mail to a single machine, where the user can go offline and read it, if necessary. IPOP3 is mainly used when connection is slow.

CLASSES OF JAVAMAIL API

JavaMail API contains number of classes few important of them are listed below

javax.mail.Session

It is the top-level class of javax.mail API its most commonly used methods provide the ability to control and load the classes that represent the service provider implementations (SPI) for various mail protocols

javax.mail.Store

it is implemented by a service provider, such as a POP Mail implementation developer. and allows for read, write, monitor, and search access for a particular mail protocol

javax.mail.Transport

This class send a message over a specific protocol.

javax.mail.Folder

It gives hierarchical organization to mail messages and provides access to e-mail messages in the form of javax.mail.Message class objects.

javax.mail.Message

It models all the details of an actual e-mail message, such as the subject line, sender/recipient e-mail address, sent date, and so on.

Uses of Javamail API

Possible uses of Javamail API are following

  • Javamail API is used to send email from any type of java application.
  • It is used to read ,compose, and sending email.
  • The one of the important use of Javamail API is to create GUI mail client.
  • It also used to send Email from java stored procedures and one of the important use of it ,is it help to search messages.
  • It is important to install javamail API to work with it.
Examples

This example is create to send message

//import the package javax.mail  and then sub package javax.mail.internet.
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
//create a class and declare the host address ,source address and destination address.as
public class SendMail {
    public static void main(String args[]) throws Exception {
        String host = "192.168.10.205";
        String from = "r4r@localhost";
        String to = "r4r@localhost"; 
//Get system properties as:
 Properties properties = System.getProperties();
//Setup mail server as:
       properties.setProperty("mail.smtp.host", host);
// Get the default Session object. as:
 Session session = Session.getDefaultInstance(properties);
// Create a default MimeMessage object. as:
  MimeMessage message = new MimeMessage(session);
//Set the RFC 822 "From" header field using the value of the InternetAddress.getLocalAddress method.  as: 
message.setFrom(new InternetAddress(from));
//Add the given addresses to the specified recipient type as:
 message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
//Set the "Subject" header field as:
 message.setSubject("hello..!");
// Sets the given String as this part's content,with a MIME type of "text/plain" as:
message.setText("Hello ......");
//Send message
 Transport.send(message);
System.out.println("Message is Send.....");