Java Mail API - PowerPoint PPT Presentation

1 / 23
About This Presentation
Title:

Java Mail API

Description:

Java Mail API is an easy and standard way of sending and receiving emails ... 4: basit_at_hell.com de subject. 5: Bill Gates bill_gates_at_microsoft.com de subject ... – PowerPoint PPT presentation

Number of Views:1466
Avg rating:3.0/5.0
Slides: 24
Provided by: bas44
Category:
Tags: api | com | gates | hell | inbox | java | mail | of

less

Transcript and Presenter's Notes

Title: Java Mail API


1
Java Mail API
  • Sending and Receiving Emails

2
Mail API
  • Java Mail API is an easy and standard way of
    sending and receiving emails
  • Java Mail API supports following Protocols
  • SMTP
  • POP
  • IMAP
  • MIME

3
SMTP
  • Simple Mail Transfer Protocol
  • Usually used for sending emails from clients
  • Also used for relaying emails from one server to
    another

4
POP
  • Post office protocol
  • Currently Version 3 in use (Pop3 RFC1939)
  • Used at client side to check the emails that are
    received in the mailbox for a user

5
IMAP
  • Stands for Internet Message Access Protocol
  • Currently IMAP4 (RFC2060) in use
  • More advanced protocol for email access that
    allows multiple folder management on server,
    periodic backups and several other advanced
    features.

6
MIME
  • Multi-purpose Internet Mail Extention
  • Defines the contents that are to be transferred
    in an email

7
What do you need to use Java Mail?
  • You should have following two APIs
  • Java Mail API
  • JavaBeans Activation Framework

8
Mail API
  • As the name says, is used for sending and
    receiving emails.
  • Mail API requires JavaBeans Activation Framework

9
JAF
  • JavaBeans Activation Framework
  • Helps programmers to Determine the type of an
    arbitrary piece of data
  • Encapsulates access to it
  • Discovers operations that can be performed on it

10
Where to get?
  • Both APIs can be downloaded from
    http//java.sun.com
  • Also placed on \\shares\teachers\basit\shared
    docs\aip\APIs

11
How to Install?
  • Un-zip the javamail.zip and jaf.zip into some
    folder
  • Put mail.jar from javamail folder to the
    classpath
  • Put activation.jar from jaf folder to classpath

12
Java Mail classes
  • You must know following classes before you start
  • Session
  • Message
  • Address
  • Transport
  • Store
  • Folder

13
javax.mail.Session
  • Defines a basic mail session
  • Everything in mail api works due to this session

14
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

15
javax.mail.Address
  • Represents an email addres
  • Address is an abstract class
  • javax.mail.Internet.InternetAddress is the
    sub-class of Address

16
Transport
  • This class speaks the protocol specific language
    to send messages. (Usually SMTP)

17
Store and Folder
  • 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

18
Sending an Email
  • //Create properties object
  • Properties p System.getProperties()
  • p.put("mail.smtp.host", "202.125.140.71")
  • // Get session
  • Session s Session.getDefaultInstance(p,null)

19
Sending an Email (cont)
  • Message m new MimeMessage(s)
  • InternetAddress to new InternetAddress("basit_at_uc
    p.edu.pk")
  • InternetAddress from new InternetAddress("bill_g
    ates_at_microsoft.com", "Bill Gates")
  • m.setContent("yeah this is the body",
    "text/plain")
  • m.setFrom(from)
  • m.setRecipient(Message.RecipientType.TO, to)
  • m.setSubject("de subject")
  • Transport.send(m)

20
Checking Mails
  • Properties props new Properties()
  • Session session Session.getDefaultInstance(props
    , null)
  • Store store session.getStore("pop3")
  • store.connect(host, username, password)
  • Folder folder store.getFolder("INBOX")
  • folder.open(Folder.READ_ONLY)

21
  • Message message folder.getMessages()
  • for (int i0, nmessage.length iltn i)
  • System.out.println(i " "
    messagei.getFrom()0 "\t\t"
    messagei.getSubject())
  • folder.close(false)
  • store.close()

22
Output
  • 0 Syed Basit ltbasit_at_ucp.edu.pkgt test 1
  • 1 basit_at_cnn.com de subject
  • 2 basit_at_microsoft.com de subject
  • 3 basit_at_dell.com de subject
  • 4 basit_at_hell.com de subject
  • 5 Bill Gates ltbill_gates_at_microsoft.comgt de
    subject

23
Lets Try it out!!!
Write a Comment
User Comments (0)
About PowerShow.com