0% found this document useful (0 votes)
19 views32 pages

1 Javamail Final

The document provides information about the Java Mail API: - Java Mail API is a standard way to send and receive emails and supports protocols like SMTP, POP, IMAP, and MIME. - Key classes include Session, Message, Address, Transport, Store, and Folder. The Session class manages configuration and authentication. The Message class represents an email. The Address class represents an email address. The Transport class sends messages using SMTP. The Store class accesses message stores and the Folder class represents mail folders. - Examples show how to use these classes to compose, send, and receive emails by creating Sessions, Messages, Addresses, connecting to Stores and Folders, and using the Transport class to send messages
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views32 pages

1 Javamail Final

The document provides information about the Java Mail API: - Java Mail API is a standard way to send and receive emails and supports protocols like SMTP, POP, IMAP, and MIME. - Key classes include Session, Message, Address, Transport, Store, and Folder. The Session class manages configuration and authentication. The Message class represents an email. The Address class represents an email address. The Transport class sends messages using SMTP. The Store class accesses message stores and the Folder class represents mail folders. - Examples show how to use these classes to compose, send, and receive emails by creating Sessions, Messages, Addresses, connecting to Stores and Folders, and using the Transport class to send messages
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 32

Java Mail API

Java Mail API


• Java Mail API is an easy and standard way of
sending and receiving emails
• Java Mail API supports following Protocols:
– SMTP(Simple Mail Transfer Protocol)
– POP(Post office protocol)
– IMAP(Internet Message Access Protocol)
– MIME(Multi-purpose Internet Mail Extention)
Message Representation
MimeMessage
Session
Properties

Headers
Address[] To Address[] From
String subject Date sentDate

Message Content: String text


Java Mail classes
• You must know following classes before
you start
– Session
– Message
– Address
– Transport
– Store
– Folder
Session Class
Javax.mail.Session
• Defines(create) a basic mail session
• Everything in mail APIworks due to this
session
• Class representing an individual mail session
• Manages configuration of e-mail system
• Handles authentication (usually needed for receiving
rather than sending mail)
• Acts as a factory for Transport and Store objects
• Session has no public constructor
• Create a session with:
public Session getDefaultInstance(Properties prop)
Session Properties Used to Send
Mail
 In Book See page no.-642
Property Purpose Likely Value
mail.transport.protocol Default Transport protocol smtp
mail.smtp.host Host of smtp protocol andrew.cmu.edu
mail.store.protocol Default Store protocol imap
mail.imap.host Host of imap protocol cyrus.andrew.cmu.edu
mail.user Default user name for both mm6
protocols
mail.from User's return address [email protected]
Message Class
Javax.mail.Message
• Represents an email message that is either created to
be sent to the recipient or is received from someone.
• Message is an Abstract class
• MimeMessage is the sub-class of message that
understands most MIME types and is most commonly
used for email handling
• With Session object created we now move on to
creating a message that will be sent.
• The message type will be javax.mail.Message.
• Message is an abstract class. Hence its subclass
javax.mail.internet.MimeMessage class is mostly
used.
• To create the message, you need to pass session object
in MimeMessage class constructor.

• For example:
• MimeMessage message=new MimeMessage(session);

• Once the message object is created we need to store


information in it. Message class implements the
javax.mail.Part interface while javax.mail.internet.
• MimeMessage implements
javax.mail.internet.MimeMessage

• You can either use message.setContent()

• or mimeMessage.setText() to store the


content.
• Example to compose the message:
• MimeMessage message=new MimeMessage(session);  
• message.setFrom(new InternetAddress(“[email protected]
m"));  
• message.addRecipient(Message.RecipientType.To,   
new InternetAddress(“[email protected]"));  
• message.setHeader("Hi, everyone");  
• message.setText("Hi, This mail is to inform you...");  
• Commonly used methods of MimeMessage class are
• Method
public void setFrom(Address address) :-used to set the from
header field.
• public void addRecipients(Message.RecipientType type, String
addresses) :-used to add the given address to the recipient
type.
• public void setSubject(String subject) :-used to set the subject
header field.
• public void setText(String textmessage) :-used to set the text
as the message content using text/plain MIME type.
Address Class
Javax.mail.Address
• Represents an email address
• Address is an abstract class
• javax.mail.Internet.InternetAddress is the sub-
class of Address
• Now that we have a Session and Message (with
content stored in it) objects, we need to address
the letter by using Address object.
• Address is an abstract class. Hence its subclass
javax.mail.internet.InternetAddress class is
mostly used.
• Address can be created by just passing email
address:
• For example:
InternetAddress address = new
InternetAddress("[email protected]");
• Another way of creating Address is by passing
name alog with the email address:
InternetAddress address = new
InternetAddress("[email protected]", Manisha);
• You can also set the To, From, CC, BCC fields as
below
– message.setFrom(address)
– message.addRecipient(type, address)
– Three predefined address types are objects with
one of these values:
• Message.RecipientType.TO
• Message.RecipientType.CC
• Message.RecipientType.BCC
Authenticator Class
Javax.mail.Authentication
• The class Authenticator represents an object
that knows how to obtain authentication for a
network connection. Usually, it will do this by
prompting the user for information.
• Authenticator is an abstract class. You create a
subclass PasswordAuthentication, passing a
username and password to its constructor.
• You must register the Authenticator with the
Session when you create session object.
• Following is an example of Authenticator use:
• Properties props = new Properties();
• //Override props with any customized data
PasswordAuthentication auth = new
PasswordAuthentication("manisha", "pswrd")

Session session = Session.getDefaultInstance(props,


auth);
Transport Class
Javax.mail.Transport
• Transport class is used as a message transport
mechanism. This class normally uses the SMTP
protocol to send a message.
• It is an abstract class.
• You can use the default version of the class by
just calling the static send() method:
• Transport.send(message);
• The javax.mail.Transport class provides method to send the message.
• Commonly used methods of Transport class

1. public static void send(Message message)


• This method is used send the message.

2. public static void send(Message message, Address[] address)


• This method is used send the message to the given addresses.

• Example to send the message:


Transport.send(message);  
Store Class
Javax.mail.Store
• An abstract class that models a message store and its
access protocol, for storing and retrieving messages.
• Subclasses provide actual implementations. Store extends
the Service class, which provides many common methods
for naming stores, connecting to stores, and listening to
connection events.
• Clients gain access to a Message Store by obtaining a Store
object that implements the database access protocol. Most
message stores require the user to be authenticated before
they allow access. The connect method performs that
authentication.
• Store store = session.getStore("pop3");
• store.connect(host, username, password);
Folder Class

• Folder is an abstract class that represents a


folder for mail messages.
• Subclasses implement protocol specific Folders.
Folders can contain subfolders as well as
messages, thus providing a hierarchical
structure.
• After connecting to the Store, you can then get
a Folder, which must be opened before you can
read messages from it.
• Folder folder = store.getFolder("INBOX");
• folder.open(Folder.READ_ONLY);
• Message message[] = folder.getMessages();
• The getFolder(String name) method for a
Folder object returns the named subfolder.
Close the both the Store and Folder
connection once reading mail is done.
• We can see the Store and Folder relation the
image below:
When receiving email
 We create a session
 Connect to a store with our username and
password
 Get specific folders (usually inbox)
 And start receiving Message objects
• The store is divided into folders, and the
“inbox” folder is the primarily folder which
contains e-mail messages. A folder can contain
both messages and sub-folders.
• import java.util.*;  
• import javax.mail.*;  
• import javax.mail.internet.*;  
• import javax.activation.*;  
•   
• public class SendEmail  
• {  
•  public static void main(String [] args)
• {  
•       String to = "[email protected]";//change accordingly  
•       String from = "[email protected]";//change accordingly  
•       String host = "localhost";//or IP address  
•   
•      //Get the session object  
•       Properties properties = System.getProperties();  
•       properties.setProperty("mail.smtp.host", host);  
•       Session session = Session.getDefaultInstance(properties);  
•   
•      //compose the message  
•       
• MimeMessage message = new MimeMessage(session);  
• message.setFrom(new InternetAddress(from));  
• message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));  
• message.setSubject("Ping");  
• message.setText("Hello, this is example of sending email  ");  
•   
• // Send message  
• Transport.send(message);  
• System.out.println("message sent successfully....");  
•   
•         }  
• }  
Example of sending Mail(Sender side,send
message)
• //Create Properties
• Properties p=System.getProperties();
• p.put(“mail.smtp.host”,”202.125.140.171”);
• //Get Session
• Session s = Session.getDefaultInstance(p,null);

• MimeMessage m=new MimeMessage(s);  

• InternetAddress to = new InternetAddress("[email protected]",


Manisha);
• InternetAddress from = new InternetAddress(“[email protected]",
“Drashti”);
• m.setContent(”This is the Body”,”text/pain”)
• m.setFrom(from);  
• m.setRecipient(Message.RecipientType.To,to); 
•  m.setSubject(“send mail");
• Transport.send(m);
Example of Receiving Mail(Receive mail)
• //Create Properties
• Properties props= new Properties();
• p.put(“mail.smtp.host”,”202.125.140.171”);
• //Get Session
• Session sess= Session.getDefaultInstance(props,null);
• Store str= sess.getStore(“pop3”);
• str.connect(host,username,password);
• Folder fldr=store.getFolder(“INBOX”);
• Fldr.open(Folder.READ_ONLY);
• MimeMessage mess[]=fldr.getMessages();
•  for(int i=0,n=mess.length;i<n;i++)
{
System.Out.Println(i+”:”+mess[i].getFrom()
+”\t\t+mess[i].getSubject());
}
•fldr.colse(false);
•Str.close();

You might also like