java mail api. high level representation of the basic components of any mail system. the components...
TRANSCRIPT
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 package.
• These classes are all abstract because they don’t make many assumptions about how the email is stored or transferred between machines.
• 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.
• 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.
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
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.
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.
Step 3
• Session object is used to create a new Message object-
• Message msg=new MimeMessage(mailConnection);
• MimeMessage is used to send Internet email.
Step 4
• Set up from address and to address-
• Address fromMy=new InternetAddress([email protected],”ajay”);
• Address toMy= new InternetAddress([email protected]);
Step 5
• Set up from header-
• Msg.setFrom(fromMy);
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);
Step 7
• Set message subject-
• Msg.setSubject(“This is Subject Line”);
Step 8
• Set up the content of message-
• msg.setContent(“TextTextText”, “text/plain”);
Step 9
• Send the message-
• Transport.send(msg);
• Example to Send Email
Receiving Mail