YouTube Icon

Interview Questions.

Top 100+ Javamail Api Interview Questions And Answers - May 31, 2020

fluid

Top 100+ Javamail Api Interview Questions And Answers

Question 1. What Is Javamail?

Answer :

Java Mail is an API that is used to acquire and send emails between applications. To ship the emails, the protocols, SMPT, POP AND IMAP are used. The messages sending and receiving is completed through developing a framework the usage of set of abstract instructions inside the API. The framework permits the software to create custom designed go-platform mail software via having fundamental knowledge of e-mail. There are methods and classes which are used to get entry to mail folders, message downloading and sending messages at the side of attachments function.

Potential advantages consist of - Java mail is used to create personal mail filter out, simple mailing lists and private mail packages. Java mail additionally includes the talents to feature the emailing technique to an organisation utility or even to create a full-fledged e-mail purchaser. Many corporations in the enterprise have written new email customers the usage of Java Mail.

The JavaMail is an API this is used to compose, write and examine digital messages (emails).

The JavaMail API offers protocol-independent and plateform-unbiased framework for sending and receiving mails.

The javax.Mail and javax.Mail.Activation applications contains the core instructions of JavaMail API.

The JavaMail facility may be applied to many activities. It can be used at the time of registering the person (sending notification consisting of thank you to your hobby to my website online), forgot password (sending password to the users e-mail id), sending notifications for critical updates and so on. So there may be various usage of java mail api.

Question 2. Which Protocols Are Used In Javamail Api?

Answer :

There are a few protocols which might be utilized in JavaMail API.

SMTP
POP
IMAP
MIME
NNTP and others
SMTP : SMTP is an acronym for Simple Mail Transfer Protocol. It provides a mechanism to supply the e-mail. We can use Apache James server, Postcast server, cmail server and many others. As an SMTP server. But if we purchase the host area, an SMTP server is bydefault provided by means of the host company. For instance, my smtp server is mail.Javatpoint.Com. If we use the SMTP server supplied through the host company, authentication is required for sending and receiving emails.

POP : POP is an acronym for Post Office Protocol, additionally called POP3. It gives a mechanism to receive the email. It affords help for unmarried mail container for each consumer. We can use Apache James server, cmail server and so on. As an POP server. But if we purchase the host space, an POP server is bydefault furnished by means of the host provider. For example, the pop server supplied through the host issuer for my web site is mail.Javatpoint.Com. This protocol is defined in RFC 1939.

IMAP : IMAP is an acronym for Internet Message Access Protocol. IMAP is a complicated protocol for receiving messages. It provides assist for a couple of mail box for each person ,in addition to, mailbox may be shared with the aid of a couple of users. It is defined in RFC 2060.

MIME : Multiple Internet Mail Extension (MIME) tells the browser what's being sent e.G. Attachment, layout of the messages etc. It isn't always known as mail switch protocol but it's far utilized by your mail software.

NNTP and Others : There are many protocols which might be furnished by way of 1/3-celebration vendors. Some of them are Network News Transfer Protocol (NNTP), Secure Multipurpose Internet Mail Extensions (S/MIME) and so forth.

Adv Java Interview Questions
Question three. Explain Javamail Architecture?

Answer :

The java utility makes use of JavaMail API to compose, ship and acquire emails. The JavaMail API makes use of SPI (Service Provider Interfaces) that provides the intermediatory services to the java software to deal with the special protocols. 

Question four. What Are The Javamail Api Core Classes?

Answer :

There are  packages that are used in Java Mail API: javax.Mail and javax.Mail.Net package deal. These programs contains many training for Java Mail API. They are:

javax.Mail.Session magnificence
javax.Mail.Message magnificence
javax.Mail.Net.MimeMessage elegance
javax.Mail.Address elegance
javax.Mail.Internet.InternetAddress elegance
javax.Mail.Authenticator class
javax.Mail.PasswordAuthentication elegance
javax.Mail.Transport magnificence
javax.Mail.Store elegance
javax.Mail.Folder class etc.
 

Adv Java Tutorial
Question 5. Explain Pop?

Answer :

POP: The Post Office Protocol is an application-level protocol within an intranet that are utilized by the local email clients to ship and retrieve e-mails from a far flung server the ones are linked using TCP/IP. POP is one of the maximum widespread protocol fro the use of e-mail. The POP and its techniques aid the end-customers with dial-up network connections.

POP allows the users to retrieve e mail while related and later lets in viewing and changing the retrieved messages. This is finished with a promising feature – with out staying connected. The process of the usage of emails over POP is to attach, retrieve the messages, and save them on the user’s PC as a new message. Later those messages may be ‘deleted from the server’ and disconnecting the server – makes POP a outstanding protocol.

 

J2EE Interview Questions
Question 6. Explain The Use Of Mime Within Message Makeup?

Answer :

MIME message includes the photo saved as record in GIF format and the GIF layout makes use of eight-bit layout. The RFC 822 uses ASCII text format. The messages within the form of ASCII textual content format is to be encoded. To show the photograph in the recipient device, the records abut the encoding mechanism is used. The message is to be made up inside the recipient’s application. The following snippet is used to identify the content material is a GIF report that's to be encoded using the usual “base64Algorithm. This is to be dealt with as an attachment through the purchaser who makes use of the email.

Content-Type: picture/gif;
call="waterfall.Gif"
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
filename="waterfall.Gif"
[Author the encoded content here]
..

The accomplishment of this is performed by way of simplifying and rebuilding of complex files. These documents are encoded and transported as a body of the message or a sequence of messages which might be the components of the record.A message layout is described by way of the MIME that allows the following:

Non ASCII person textual message our bodies.
Non textual message our bodies 
Message our bodies which can be multipart 
Non ASCII person textual header data
Code Example:
Message msg = new MimeMessage(session);
msg.SetFrom(new InternetAddress(from));
InternetAddress[] deal with = new InternetAddress(args[0]);
msg.SetRecipients(Message.RecipientType.TO, address);
msg.SetSubject("Hello");
msg.SetSentDate(new Date());
msg.SetText("Mail Message");

Question 7. Discuss About Javamail?

Answer :

JavaMail is a hard and fast of summary training that create a framework for sending, receiving and dealing with email.
The bundle that Sun presents contains implementations of IMAP and SMTP, which allow sending and receiving mail.
The framework eases the advent of pass-platform mail application with out an in-depth knowledge of electronic mail.
There are techniques and instructions that allow access to mail folders, download messages, ship messages with attachments and filter out mail.
J2EE Tutorial Core Java Interview Questions
Question eight. Explain The Structure Of Javamail Api?

Answer :

The JavaMail API has training such as Message, Store and Transport. The API can be used to subclass for imparting new protocols and some additional functionality when needed. The concrete subclasses of this API are MimeMessage and MimeBodyPart that are implemented extensively through the internet mail protocols. The assisting protocols for Javamail API are IMAP4, POP3 and SMTP.

The Java mail architectural components include the subsequent: 

Abstract Layer: This layer declares the lessons, interfaces and abstract strategies which might be intended for helping the mail features which all mailing structures supports. Intranet Implementation Layer: The implementation of MIME internet requirements and part of the abstract layer accommodates this residue. Java Bean Activation Framework: The encapsulation of message information and coping with the statistics interacting commands is used by the Javabean

Question nine. What Is Session In Javamail Api?

Answer :

Session is the pinnacle-degree access magnificence representing mail consultation. It uses java.Util.Properties object to get facts about mail server, username, password and so on. This elegance has a non-public constructor and you may get consultation items via getInstance() and getDefaultInstance() techniques. GetDefaultInstance() approach affords default Session item which takes Properties and Authenticator items as arguments.

Properties props = new Properties();
Session consultation = Session.GetDefaultInstance(props, null);
Or create a completely unique session with the aid of getInstance() method…

Properties props = new Properties();
Session session = Session.GetInstance(props, null);

JSP Interview Questions
Question 10. Sample Code To Send Html Mail With Images Using Javamail?

Answer :

A patron create new message by means of the usage of Message subclass. It units attributes like recipient address and the concern, and inserts the content into the Message item, and inserts the content into the Message item. Finally, it sends the Message with the aid of invoking the Transport.Send() approach.

The Transport class fashions the transport agent that routes a message to its vacation spot addresses. This elegance gives methods that send a message to a listing of recipients. Invoking the Transport.Ship() approach with a Message item identifies the ideal shipping based totally on its vacation spot addresses.

Package com.Withoutbook.Commonplace;

import java.Util.*;
import javax.Mail.*;
import javax.Mail.Internet.*;
public magnificence HTMLMail 
public static void major(String args[]) throws Exception 
Strig host = "192.168.10.A hundred and ten";
String from = "arindam@localhost";
String to = "arindam@localhost";
// Get system properties
Properties residences = System.GetProperties();
// Setup mail server
residences.SetProperty("mail.Smtp.Host", host);
// Get the default Session item.
Session consultation = Session.GetDefaultInstance(properties);
// Create a default MimeMessage object.
MimeMessage message = new MimeMessage(consultation);
// Set the RFC 822 "From" header discipline the usage of the 
// price of the InternetAddress.GetLocalAddress method.
Message.SetFrom(new InternetAddress(from));
// Add the given addresses to the desired recipient type.
Message.AddRecipient(Message.RecipientType.TO, 
new InternetAddress(to));
// Set the "Subject" header field.
Message.SetSubject("hi..!");
String htmlText = "
Hello
// Sets the given String as this part's content material, // with a MIME kind of "textual content/simple".
Message.SetContent(htmlText, "textual content/html");
// Send message
Transport.Send(message);
System.Out.Println("Message Send.....");

Core Java Tutorial
Question eleven. What Is Authenticator In Javamail Api?

Answer :

The JavaMail Authenticator is discovered within the javax.Mail bundle, and used as an authenticator to get entry to included resources by way of prompting the user for username and password.

Properties props = new Properties();
Authenticator auth = new MyAuthenticator();
Session session = Session.GetDefaultInstance(props, auth);
Java-Springs Interview Questions
Question 12. What Is Transport In Javamail Api?

Answer :

This is very last part of sending Email, it's miles an abstract magnificence. Default version of this elegance can be utilized by calling static ship() technique.

Transport.Send(message); 

Or can create a particular example from the session for described protocol.

Message.SaveChanges();

Transport delivery = consultation.GetTransport("smtp");

shipping.Connect(host, username, password);

shipping.SendMessage(message, message.GetAllRecipients());

delivery.Close();

Adv Java Interview Questions
Question thirteen. What Is Store And Folder In Javamail Api?

Answer :

After getting the session you connect with javax.Mail.Store magnificence with Authenticator or host, port, user and password facts. Store object that implements the required protocol can be created via by way of passing the protocol facts to the getStore() approach of the consultation item.

Store shop = consultation.GetStore("pop3");

save.Join(host, username, password);

After connecting to keep you need to get a folder that holds messages. For POP3, the best folder to be had is the INBOX however with IMAP, you can have other folders available. For this you could use javax.Mail.Folder class.

Folder folder = store.GetFolder("INBOX");

folder.Open(Folder.READ_ONLY);

Message message[] = folder.GetMessages();

Once you've got read messages, you need to close Store and Folder.

Folder.Near(booleanValue);

keep.Close();

JSP Tutorial
Question 14. Sample Code For Reading Multipart Mail Using Javamail?

Answer :

These days Multipart mail is used to compose emails with attachment. In the email you may connect images, zip files, xls, document and so forth.. It permits you to create nicely design emails.

Java Mail API also gives instructions to create, send and examine the Multipart message. If you've got given a task to create emails with attachment in Java technology, then you can use the Java Mail api to accomplish your project.

Using the Multipart Class

Following code snippet shows how you may use the Multipart magnificence. You should create the item of Multipart elegance and then you may get the frame component and compose or examine your e-mail.

Multipart multipart = (Multipart) msg[i].GetContent();After that create BodyPart object BodyPart bodyPart = multipart.GetBodyPart(x);

And then study bodyPart content the usage of getContent() approach.

 

Package com.Withoutbook.Commonplace;
import java.Util.*;
import javax.Activation.DataHandler;
import javax.Mail.*;
import javax.Mail.Internet.*;
public class ReadMultipartMail 
public static void principal(String args[]) throws Exception 
String host = "192.168.10.110";
String username = "arindam";
String passwoed = "arindam";
Properties houses = System.GetProperties();
Session consultation = Session.GetDefaultInstance(residences);
Store keep = consultation.GetStore("pop3");
store.Connect(host, username, passwoed);
Folder folder = save.GetFolder("inbox");
if (!Folder.Exists()) 
System.Out.Println("No INBOX...");
System.Go out(zero);

folder.Open(Folder.READ_WRITE);
Message[] msg = folder.GetMessages();
for (int i = zero; i < msg.Period; i++) 
System.Out.Println("------------ Message " + (i + 1) + " ------------");
String from = InternetAddress.ToString(msg[i].GetFrom());
if (from != null) 
System.Out.Println("From: " + from);

String replyTo = InternetAddress.ToString(
msg[i].GetReplyTo());
if (replyTo != null) 
System.Out.Println("Reply-to: " + replyTo);

String to = InternetAddress.ToString(
msg[i].GetRecipients(Message.RecipientType.TO));
if (to != null) 
System.Out.Println("To: " + to);

String situation = msg[i].GetSubject();
if (concern != null) 

System.Out.Println("Subject: " + challenge);

Date sent = msg[i].GetSentDate();
if (sent != null) 
System.Out.Println("Sent: " + sent);

System.Out.Println();
System.Out.Println("Message : ");
Multipart multipart = (Multipart) msg[i].GetContent();
for (int x = 0; x < multipart.GetCount(); x++) 
BodyPart bodyPart = multipart.GetBodyPart(x);
String disposition = bodyPart.GetDisposition();
if (disposition != null && (disposition.Equals(BodyPart.ATTACHMENT))) 
System.Out.Println("Mail have some attachment : ");
DataHandler handler = bodyPart.GetDataHandler();
System.Out.Println("report call : " + handler.GetName());
 else 
System.Out.Println(bodyPart.GetContent());


System.Out.Println();

folder.Close(true);
store.Near();

Question 15. Sample Code For Reading Attachment Message Using Javamail?

Answer :

Java Mail API provides training to ship multiple Mime frame part with one Mime Message. MulipltMimeBody elements may be text, document or image. All message are saved in Folder gadgets. Folders can comprise folders or messages. The Folder elegance publicizes strategies that fetch, append, replica and delete messages, and message incorporate Attachment.

Package deal com.Withoutbook.Not unusual;

import java.Io.*;
import java.Util.*;
import javax.Mail.*;
public magnificence ReadAttachment 
public static void major(String args[]) throws Exception 
String host = "192.168.10.110";
String consumer = "arindam";
String password = "arindam";
// Get system houses
Properties houses = System.GetProperties();
// Get the default Session item.
Session consultation = Session.GetDefaultInstance(residences);
// Get a Store item that implements the specified protocol.
Store shop = consultation.GetStore("pop3");
//Connect to the modern host the usage of the desired username and password.
Save.Join(host, user, password);
//Create a Folder object similar to the given call.
Folder folder = shop.GetFolder("inbox");
// Open the Folder.
Folder.Open(Folder.READ_WRITE);
Message[] message = folder.GetMessages();
for (int a = zero; a < message.Length; a++) 
System.Out.Println("-------------" + (a + 1) + "-----------");
System.Out.Println(message[a].GetSentDate());
Multipart multipart = (Multipart) message[a].GetContent();
//System.Out.Println(multipart.GetCount());
for (int i = 0; i < multipart.GetCount(); i++) 
//System.Out.Println(i);
//System.Out.Println(multipart.GetContentType());
BodyPart bodyPart = multipart.GetBodyPart(i);
InputStream stream = bodyPart.GetInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(stream));
while (br.Prepared()) 
System.Out.Println(br.ReadLine());

System.Out.Println();

System.Out.Println();

folder.Close(actual);
store.Near();

JMS(Java Message Service) Interview Questions
Question sixteen. Sample Code For Replying To Messages Using Javamail?

Answer :

The Message magnificence have a respond() technique to configure a brand new Message with the proper recipient and problem, including "Re: " if no longer already there. This does not upload any content material to the message, reply() technique have a boolean parameter indicating whether or not to answer to best the sender (fake) or respond to all (actual).

MimeMessage respond = (MimeMessage)message.Respond(fake);reply.SetFrom(new InternetAddress("president@whitehouse.Gov"));respond.SetText("Thanks");Transport.Send(reply);

To configure the respond-to deal with while sending a message, use the setReplyTo() technique.

Package com.Withoutbook.Commonplace;

import java.Io.*;
import java.Util.*;
import javax.Mail.*;
import javax.Mail.Net.*;
public class ReplyMail 
public static void fundamental(String args[]) throws Exception 
Date date = null;
Properties properties = System.GetProperties();
residences.SetProperty("mail.Smtp.Host", "192.168.10.A hundred and ten");
Session consultation = Session.GetDefaultInstance(homes);
Store keep = consultation.GetStore("pop3");
shop.Connect("192.168.10.A hundred and ten", "arindam", "arindam");
Folder folder = save.GetFolder("inbox");
if (!Folder.Exists()) 
System.Out.Println("inbox not located");
System.Go out(0);

folder.Open(Folder.READ_ONLY);
BufferedReader reader = new BufferedReader(new InputStreamReader(System.In));
Message[] message = folder.GetMessages();
if (message.Duration != zero) 
System.Out.Println("no. From ttSubject ttDate");
for (int i = 0, n = message.Duration; i < n; i++) 
// Create a reply message
MimeMessage reply = (MimeMessage) message[i].Reply(false);
// Set the from field
reply.SetFrom(message[i].GetFrom()[0]);
// Create the reply content
// Create the reply content, copying over the original if text
MimeMessage orig = (MimeMessage) message[i];
StringBuffer buffer = new StringBuffer("Thanksnn");
if (orig.IsMimeType("text/plain")) 
String content = (String) orig.GetContent();
StringReader contentReader = new StringReader(content);
BufferedReader br = new BufferedReader(contentReader);
String contentLine;
while ((contentLine = br.ReadLine()) != null) 
buffer.Append("> ");
buffer.Append(contentLine);
buffer.Append("rn");


// Set the content
respond.SetText(buffer.ToString());
// Send the message
Transport.Send(respond);
 else if ("n".Equals(ans)) 
ruin;


 else 
System.Out.Println("There is no msg....");


Java-Springs Tutorial
Question 17. What Is Address In Javamail Api?

Answer :

Address class is likewise an abstract elegance so we can use right here magnificence javax.Mail.Internet.InternetAddress.
Address address = new InternetAddress("arindam@withoutbook.Com");
To display a call next to the Email Address use one extra argument for name.
Address deal with = new InternetAddress("arindam@withoutbook.Com", "Arindam");

After creating deal with connect with message by  approaches: 

1: By setFrom()approach: message.SetFrom(deal with);
1: By setReplyTo()approach: When want to ship respond to greater addresses.
Address address[] = ...;
Message.SetReplyTo(cope with[]);
To pick out the recipient, add the recipients via the usage of addRecipient() approach….
Message.AddRecipient(kind, cope with);

Address types are of three kinds:

Message.RecipientType.TO: number one recipient
Message.RecipientType.CC: carbon copy
Message.RecipientType.BCC: blind carbon reproduction
Java applet Interview Questions
Question 18. What Is Message In Javamail Api?

Answer :

After developing session item, create message to ship via the usage of Message magnificence. Because of message elegance is abstract class so we can use subclass javax.Mail.Internet.MimeMessage.

MimeMessage message = new MimeMessage(consultation) 
// set the content of message via setContent() method
message.SetContent(“www.Roseindia.Net”, "text/simple");
We also can use setText() approach to set the content material.
Message.SetText("www.Roseindia.Net");
To create subject line of sending message use setSubject() method.

J2EE Interview Questions
Question 19. What Are The Advantages Of Javamail?

Answer :

Potential benefits consist of - Java mail is used to create personal mail clear out, simple mailing lists and personal mail programs. Java mail also includes the abilties to add the emailing system to an organization application or even to create a complete-fledged electronic mail patron. Many agencies inside the industry have written new email clients the use of Java Mail.

Java Tutorial
Question 20. Explain Smtp?

Answer :

SMTP: Simple Mail Transfer Protocol, for sending e mail among ‘servers’. Most of the emailing systems enforce the messages over internet use SMTP. The message despatched from one server to every other server, after which the message can be retrieved by way of an e mail client. The client uses both POP or IMAP. In addition to this method, SMTP is likewise generally used for message sending and retrieval from a mail customer to a mail server. This is the motive why the need of POP or IMAP server and the SMTP servers at the time of configuring the e-mail application.

Java Interview Questions
Question 21. Explain Imap?

Answer :

IMAP: Short for Internet Message Access Protocol. This is some other maximum commonplace protocol of net fashionable for e-mail utilization aside from POP. Usually all the contemporary e-mail server and purchaser supports these two protocols for transmitting the e-mail messages. For Example Gmail server makes use of to transmit the message to a customer along with Mozilla Thunderbird and Microsoft Outlook.

IMAP is an application layer protocol over internet that is working from port no. 143 that permits the accessibility of e-mail on a faraway server by using a consumer. IMAP helps the net and offline (disconnected) modes of operations. Usually the e-mail customers using IMAP makes use of the facility of leaving the message at the server. The message lasts till the person explicitly deletes them. IMAP also allows a couple of clients to have the accessibility of the same mailbox.

Question 22. What Is Mime?

Answer :

MIME and RFC822 are the requirements for describing e mail messages which can be sent across the Internet. The javax.Mail.Internet subpackage (that's part of the JavaMail APIs) gives a whole implementation of these  applications. MIME is exact with the aid of the subsequent RFCs: RFC2045, RFC2046, RFC2047.

JavaMail API Tutorial
Question 23. What Jdk Does The Javamail Api Need?

Answer :

The JavaMail API requires JDK/JRE 1.4 or better. The JavaMail API is a Java optional package, it isn't always part of the core Java SE but is protected in Java EE.

Java 8 Interview Questions
Question 24. How Do We Store Mail Messages On My Local Disk?

Answer :

 A "local shop issuer" can be used to save mail messages on a neighborhood disk. The JavaMail API down load does not consist of this type of provider but a issuer that helps the Unix mbox format is available inside the JavaMail source repository that you may build your self. See this page for info. In addition, several neighborhood save companies are available from third events for unique local shop formats including MH and Mbox. See our Third Party Products web page for the modern listing of such vendors.

Core Java Interview Questions
Question 25. Do The Javamail Apis Work In Web Browsers?

Answer :

JavaMail will paintings in any browser that helps the required JDK version. The Java Plug-in can be required to provide such aid.

Java eight Tutorial




CFG