java mail api. high level representation of the basic components of any mail system. the components...

16
JAVA MAIL API

Upload: denis-austin

Post on 23-Dec-2015

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: JAVA MAIL API. High level representation of the basic components of any mail system. The components are represented by abstract classes in the javax.mail

JAVA MAIL API

Page 2: JAVA MAIL API. High level representation of the basic components of any mail system. The components are represented by abstract classes in the javax.mail

• High level representation of the basic components of any mail system.

• The components are represented by abstract classes in the javax.mail package.

• These classes are all abstract because they don’t make many assumptions about how the email is stored or transferred between machines.

Page 3: JAVA MAIL API. High level representation of the basic components of any mail system. The components are represented by abstract classes in the javax.mail

• JavaMail API is a standard extension to Java, not part of the core JDK or JRE class library. Consequently there’ll need to download it separately from Sun.

• Download “mail.jar”. This file contains the actual .class file that implement the JavaMail API.

• Add this file to the class path OR simply place it in jre/lib/ext directory.

Page 4: JAVA MAIL API. High level representation of the basic components of any mail system. The components are represented by abstract classes in the javax.mail

• Java Mail API uses JavaBeans Activation Framework(JAF) to describe and display multilingual text and encoded multimedia data.

• The JavaBeans Activation Framework is also a standard extension to Java, not part of the core API.

• Download “activation.jar” file and place it in the class path.

Page 5: JAVA MAIL API. High level representation of the basic components of any mail system. The components are represented by abstract classes in the javax.mail

Sending Email

1. Set the “mail.host” property to point the local mail server-

Properties props = new Properties()

props.put("mail.host",“<name of mail server>");

It can be like::

alt2.gmail-smtp-in.l.google.com

Page 6: JAVA MAIL API. High level representation of the basic components of any mail system. The components are represented by abstract classes in the javax.mail

How to know about mail server

• Open a DOS Command Prompt 

• Type "nslookup". 

• Your computer's DNS Server name and IP address will be displayed. 

• Type "set type=mx"

• This will cause NSLOOKUP to only return what are known as MX (Mail eXchange) records from the DNS servers. 

Page 7: JAVA MAIL API. High level representation of the basic components of any mail system. The components are represented by abstract classes in the javax.mail
Page 8: JAVA MAIL API. High level representation of the basic components of any mail system. The components are represented by abstract classes in the javax.mail

Step 2

• These properties are used to retrieve a Session object from the Session.getInstance() method like this-

• Session mailConnection = Session.getInstance(props,null);

• Session object represents an ongoing communication between a program and one mail server.

• The second argument to the getInstance(), null here is a javax.mail.Authenticator that ask the user for a password if requested by the server.

Page 9: JAVA MAIL API. High level representation of the basic components of any mail system. The components are represented by abstract classes in the javax.mail

Step 3

• Session object is used to create a new Message object-

• Message msg=new MimeMessage(mailConnection);

• MimeMessage is used to send Internet email.

Page 10: JAVA MAIL API. High level representation of the basic components of any mail system. The components are represented by abstract classes in the javax.mail

Step 4

• Set up from address and to address-

• Address fromMy=new InternetAddress([email protected],”ajay”);

• Address toMy= new InternetAddress([email protected]);

Page 11: JAVA MAIL API. High level representation of the basic components of any mail system. The components are represented by abstract classes in the javax.mail

Step 5

• Set up from header-

• Msg.setFrom(fromMy);

Page 12: JAVA MAIL API. High level representation of the basic components of any mail system. The components are represented by abstract classes in the javax.mail

Step 6

• Set up the To header-• Not only specify the address that the message

will be sent to but how that address is used i.e. TO, Cc, or Bcc. This is done by

• Message.RecipientType.TO• Message.RecipientType.CC• Message.RecipientType.BCC• Set the to header-• Msg.setRecipient(Message.RecipientType.TO,

toMy);

Page 13: JAVA MAIL API. High level representation of the basic components of any mail system. The components are represented by abstract classes in the javax.mail

Step 7

• Set message subject-

• Msg.setSubject(“This is Subject Line”);

Page 14: JAVA MAIL API. High level representation of the basic components of any mail system. The components are represented by abstract classes in the javax.mail

Step 8

• Set up the content of message-

• msg.setContent(“TextTextText”, “text/plain”);

Page 15: JAVA MAIL API. High level representation of the basic components of any mail system. The components are represented by abstract classes in the javax.mail

Step 9

• Send the message-

• Transport.send(msg);

• Example to Send Email

Page 16: JAVA MAIL API. High level representation of the basic components of any mail system. The components are represented by abstract classes in the javax.mail

Receiving Mail