JavaMail API : Java API to send and receive email.
Core protocol implementations that are bundled as part of the
JavaMail distribution:
✦ Simple Mail Transport Protocol (SMTP)
✦ Post Office Protocol version 3 (POP3)
✦ Internet Message Access Protocol (IMAP)
SMTP
The Simple Mail Transport Protocol was first proposed back in 1982 and was designed
for the delivery of mail messages to servers. Note that SMTP
is merely a delivery agent and is not used to read e-mail.
POP3
The Post Office Protocol is the mechanism by which the majority of people collect
their e-mail. It is then the responsibility of the user to
take care of the e-mail by filing
it in some logical storage. This setup can be a little
confusing to new users because modern-day e-mail clients give the illusion that
the server stores the messages. POP has been in its present state, version 3.0
IMAP
The Internet Message Access Protocol is a protocol that many enterprise e-mail
servers employ. It offers a far richer set of functions than
POP. With POP the premise
is that the user is responsible for the storage of e-mail,
whereas with IMAP the server
assumes
this responsibility.
IMAP is a communication protocol used between the user and the
server and is only responsible for the reading and retrieval of messages. It is
not used for the delivery of e-mail between servers.
MIME
The Multipurpose Internet Mail
Extension defines the translation of and all the
rules
that are associated with the transmission of binary-based
e-mail. Internet mail is
fundamentally based on pure American Standard Code for
Information Interchange
(ASCII)
text, and on the whole does not permit non-ASCII data to be used.
Note
: uses the SMTP protocol to deliver our message.
There are several classes
and interfaces in java Mail API, in which some are commonly used.
javax.mail.Session
The
Session class represents a mail session and is not sub classed. It collects
together properties and defaults used by the mail API's. A single default
session can be shared by multiple applications on the desktop. Unshared
sessions can also be created. Session
class defines
the mail session used for communicating with remote mail systems. The Session class has no public constructors to which to create
a new instance.
Commonly used method:
static Session getInstance(Properties P) :return
new (private )session instance
static Session getDefaultInstance(Properties P) :return
same instance, if one exist
static Session getDefaultInstance(Properties P,
Authenticator A): authenticate connection than return session instance.
Different session parameter is passed using
java.util.properties class.
Like below-
static Properties
properties = new Properties();
static
{
properties.put("mail.smtp.host", "smtp.gmail.com");//hostname
properties.put("mail.smtp.socketFactory.port",
"465");
properties.put("mail.smtp.socketFactory.class",
"javax.net.ssl.SSLSocketFactory"); //secure socket login
properties.put("mail.smtp.auth", "true");
//authentication true
properties.put("mail.smtp.port", "465");
}
Session
session=Session.getDefaultInstance(properties);
javax.mail.Authenticator
When a session comes to the point
where it requires authentication details, it makes a call to this class for the
required information.
Simply make a class by extending
Authenticator class and override its getPasswordAuthentication() method.
Below create a anonymous class to override
getPasswordAuthentication() method.
new
javax.mail.Authenticator() {
protected PasswordAuthentication
getPasswordAuthentication() {
return new
PasswordAuthentication(username, password);
}
}
javax.mail.Message
This abstract class provides the basic container
for the representation of a mail message.
Message.RecipientType defines the following constants:
Message.RecipientType.TO
Message.RecipientType.CC
Message.RecipientType.BCC
MimeMessage.RecipientType.NEWSGROUPS
javax.mail.internet.MimeMessage
This class extends javax.mail.Message
class. An email message that understands MIME type.
To construct email
message, constructor is used.
MimeMessage(Session s)
Commonly used method:
setText(String msgBody)
setSubject(String subject)
setRecipients(RecipientType
type, Address address)
setFrom(Address address)
javax.mail.Transport
It is responsible to delivery of
message.
Commonly used method:
Send(Message message)
javax.mail.internet.InternetAddress
This class extends Address class.
An e-mail address must contain at
least an address; optionally a name may be associated with it. For example, the
following two e-mail addresses are valid:
“navindra” <navindra01@gmail.com>
< navindra01@gmail.com >
To construct a email address
constructor is used.
InternetAddress(String address)
Commonly used method :
static InternetAddress[] parse(String
string)Now, My SendMail.class
·
Add mail.jar file in library
public class SendMail {
static Properties properties = new Properties();
static
{
properties.put("mail.smtp.host", "smtp.gmail.com");
properties.put("mail.smtp.socketFactory.port",
"465");
properties.put("mail.smtp.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.port", "465");
}
public String sendMessage(String from, String password, String to,
String subject, String body)
{
try
{
Session session =
Session.getDefaultInstance(properties,
new javax.mail.Authenticator() {
protected PasswordAuthentication
getPasswordAuthentication() {
return new
PasswordAuthentication(from,
password);
}});
Message message = new
MimeMessage(session);
message.setFrom(new
InternetAddress(from));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(to));
message.setSubject(subject);
message.setText(body);
Transport.send(message);
}
catch(Exception e)
{
e.printStackTrace();
}
}
Simply use sendMessage() method by
providing appropriate arguments.
No comments:
Post a Comment