Related differences

Ques 16. What is Authenticator in JavaMail api?

The JavaMail Authenticator is found in the javax.mail package, and used as an authenticator to access protected resources by prompting the user for username and password.

Properties props = new Properties();
Authenticator auth = new MyAuthenticator();
Session session = Session.getDefaultInstance(props, auth);

Is it helpful? Add Comment View Comments
 

Ques 17. What is Transport in JavaMail api?

This is final part of sending Email, it is an abstract class. Default version of this class can be used by calling static send() method.

Transport.send(message);

Or can create a specific instance from the session for defined protocol.

message.saveChanges();
Transport transport = session.getTransport("smtp");
transport.connect(host, username, password);
transport.sendMessage(message, message.getAllRecipients());
transport.close();

Is it helpful? Add Comment View Comments
 

Ques 18. What is Store and Folder in JavaMail api?

After getting the session you connect to javax.mail.Store class with Authenticator or host, port, user and password information. Store object that implements the specified protocol can be created by by passing the protocol information to the getStore() method of the session object.

Store store = session.getStore("pop3");
store.connect(host, username, password);

After connecting to store you need to get a folder that holds messages. For POP3, the only folder available is the INBOX but with IMAP, you can have other folders available. For this you can use javax.mail.Folder class.

Folder folder = store.getFolder("INBOX");
folder.open(Folder.READ_ONLY);
Message message[] = folder.getMessages();

Once you have read messages, you need to close Store and Folder.

folder.close(booleanValue);
store.close();

Is it helpful? Add Comment View Comments
 

Ques 19. Sample code for Reading message using Java Mail.

The Folder class declares methods that fetch, append, copy and delete messages. These are some of the methods used in the program.

System.getProperties() this method get the system properties.
Session.getDefaultInstance(properties) this method get the default Session object.
session.getStore("pop3") this method get a Store object that implements the pop3 protocol.
store.connect(host, user, password) Connect to the current host using the specified username and password.
store.getFolder("inbox") create a Folder object of the inbox.
folder.open(Folder.READ_ONLY) open the Folder.
folder.getMessages() get all messages for the folder.


import java.io.*;
import java.util.*;
import javax.mail.*;

public class ReadMail {

public static void main(String args[]) throws Exception {

String host = "192.168.10.110";
String user = "test";
String password = "test";

// Get system properties
Properties properties = System.getProperties();

// Get the default Session object.
Session session = Session.getDefaultInstance(properties);

// Get a Store object that implements the specified protocol.
Store store = session.getStore("pop3");

//Connect to the current host using the specified username and password.
store.connect(host, user, password);

//Create a Folder object corresponding to the given name.
Folder folder = store.getFolder("inbox");

// Open the Folder.
folder.open(Folder.READ_ONLY);

Message[] message = folder.getMessages();

// Display message.
for (int i = 0; i < message.length; i++) {

System.out.println("------------ Message " + (i + 1) + " ------------");

System.out.println("SentDate : " + message[i].getSentDate());
System.out.println("From : " + message[i].getFrom()[0]);
System.out.println("Subject : " + message[i].getSubject());
System.out.print("Message : ");

InputStream stream = message[i].getInputStream();
while (stream.available() != 0) {
System.out.print((char) stream.read());
}
System.out.println();
}

folder.close(true);
store.close();
}
}

Is it helpful? Add Comment View Comments
 

Ques 20. Sample code for sending multipart mail using JavaMail.

Multipart is like a container that holds one or more body parts. When u send a multipart mail firstly create a Multipart class object and then create BodyPart class object, set text in BodyPart class object and add all BodyPart class object in Multipart class object and send the message. Class Multipart that we will use in this code is an abstract class. Method used in this program of Multipart class is:

void addBodyPart(BodyPart part);

This method is used to add parts in multipart.


package com.withoutbook.common;

import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;

public class SendMultipartMail {

public static void main(String args[]) throws Exception {

String host = "192.168.10.110";
String from = "test@localhost";
String to = "arindam@localhost";

Properties properties = System.getProperties();
properties.setProperty("mail.smtp.host", host);

Session session = Session.getDefaultInstance(properties);

Message msg = new MimeMessage(session);

msg.setFrom(new InternetAddress(from));
msg.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
msg.setSubject("MultiPart Mail");

Multipart multipart = new MimeMultipart();

BodyPart part1 = new MimeBodyPart();
part1.setText("This is multipart mail and you read part1......");

BodyPart part2 = new MimeBodyPart();
part2.setText("This is multipart mail and you read part2......");

multipart.addBodyPart(part1);
multipart.addBodyPart(part2);

msg.setContent(multipart);

Transport.send(msg);
System.out.println("Message send....");
}
}

Is it helpful? Add Comment View Comments
 

Most helpful rated by users: