Sending Email Attachments With Java Mail API - PowerPoint PPT Presentation

1 / 12
About This Presentation
Title:

Sending Email Attachments With Java Mail API

Description:

An email message is composed of multiple parts, usually called Message Body ... close the folder, passing in a true value to expunge the deleted messages. ... – PowerPoint PPT presentation

Number of Views:637
Avg rating:3.0/5.0
Slides: 13
Provided by: bas44
Category:

less

Transcript and Presenter's Notes

Title: Sending Email Attachments With Java Mail API


1
Sending Email AttachmentsWith Java Mail API
  • Lecture 10

2
Emails
  • An email message is composed of multiple parts,
    usually called Message Body Parts or MIME body
    parts.
  • Simple text mails consist of only one part
  • We can create and add new parts to one email
    message before sending it

3
Attaching Documents
  • Following steps are required to attach a FILE to
    an email
  • Create an email message and set its parameters
    for delivery
  • Create a MIME body part containing message text
  • Create a MIME body part containing attachment
  • Associate these parts with the email message
  • Send the message

4
Create an email message and set its parameters
for delivery
  • // Get system properties
  • Properties props System.getProperties()
  • // Setup mail server
  • props.put("mail.smtp.host", "202.125.140.71")
  • // Get session
  • Session session Session.getDefaultInstance(prop
    s, null)
  • Message message new MimeMessage(session)
  • message.setFrom(new InternetAddress("testing_at_ba
    sitali.com"))
  • message.addRecipient(Message.RecipientType.TO,
  • new InternetAddress("basit_at_ucp.edu.pk"))
  • message.setSubject("Hello JavaMail Attachment")

5
Create a MIME body part containing message text
  • // Create the message part
  • BodyPart messageBodyPart new MimeBodyPart()
  • // Fill the message
  • messageBodyPart.setText("this is the first text
    part")

6
Create a MIME body part containing attachment
  • BodyPart attachmentBodyPart new MimeBodyPart()
  • // Part two is attachment
  • DataSource source new FileDataSource("testfile.t
    xt")
  • attachmentBodyPart.setDataHandler(new
    DataHandler(source))
  • attachmentBodyPart.setFileName("testfile.txt")

7
Associate these parts with the email message
  • Multipart multipart new MimeMultipart()
  • multipart.addBodyPart(messageBodyPart)
  • multipart.addBodyPart(attachmentBodyPart)
  • // Put parts in message
  • message.setContent(multipart)

8
Finally Sending the message
  • // Send the message
  • Transport.send(message)

9
Deleting Messages
  • Deleting Messages and Flags
  • Deleting messages involves working with the Flags
    associated with the messages.
  • There are different flags for different states,
    some system-defined and some user-defined.

10
Deleting Messages
  • The predefined flags are defined in the inner
    class Flags.Flag and are listed below
  • Flags.Flag.ANSWERED
  • Flags.Flag.DELETED
  • Flags.Flag.DRAFT
  • Flags.Flag.FLAGGED
  • Flags.Flag.RECENT
  • Flags.Flag.SEEN
  • Flags.Flag.USER

11
Deleting Emails
  • To delete messages, you set the message's DELETED
    flag
  • message.setFlag(Flags.Flag.DELETED, true)
  • Open up the folder in READ_WRITE mode first
    though
  • folder.open(Folder.READ_WRITE)
  • Then, when you are done processing all messages,
    close the folder, passing in a true value to
    expunge the deleted messages.
  • folder.close(true)

12
A good tutorial
  • http//java.sun.com/developer/onlineTraining/JavaM
    ail/contents.htmlJavaMailIntro
Write a Comment
User Comments (0)
About PowerShow.com