Download Android App in your Android Device from Google Play Store
- Search for "Withoutbook Practice Exam Test" in Mobile/Tablet Play Store
Practice InterviewNew Search by Name or Email

Exams Attended

Make Homepage

Bookmark this page

Subscribe Email Address

Java Mail Interview Questions and Answers

Ques. Sample code for replying to messages using JavaMail.
Ans. The Message class have a reply() method to configure a new Message with the proper recipient and subject, adding "Re: " if not already there. This does not add any content to the message, reply() method have a boolean parameter indicating whether to reply to only the sender (false) or reply to all (true).

MimeMessage reply = (MimeMessage)message.reply(false);reply.setFrom(new InternetAddress("president@whitehouse.gov"));reply.setText("Thanks");Transport.send(reply);

To configure the reply-to address when sending a message, use the setReplyTo() method.


package com.withoutbook.common;

import java.io.*;
import java.util.*;

import javax.mail.*;
import javax.mail.internet.*;

public class ReplyMail {

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

Date date = null;
Properties properties = System.getProperties();
properties.setProperty("mail.smtp.host", "192.168.10.110");

Session session = Session.getDefaultInstance(properties);

Store store = session.getStore("pop3");
store.connect("192.168.10.110", "arindam", "arindam");

Folder folder = store.getFolder("inbox");
if (!folder.exists()) {
System.out.println("inbox not found");
System.exit(0);
}
folder.open(Folder.READ_ONLY);

BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

Message[] message = folder.getMessages();
if (message.length != 0) {
System.out.println("no. From \t\tSubject \t\tDate");
for (int i = 0, n = message.length; i < n; i++) {
date = message[i].getSentDate();

System.out.println(" " + (i + 1) + ": " + message[i].getFrom()[0] + "\t" +
message[i].getSubject() + " \t" + date.getDate() + "/" +
date.getMonth() + "/" + (date.getYear() + 1900));
System.out.print("Do you want to reply [y/n] : ");
String ans = reader.readLine();
if ("Y".equals(ans) || "y".equals(ans)) {

// 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("Thanks\n\n");
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("\r\n");
}
}
// Set the content
reply.setText(buffer.toString());

// Send the message
Transport.send(reply);

} else if ("n".equals(ans)) {
break;
}
}

} else {
System.out.println("There is no msg....");
}

}
}
Is it helpful? Yes No

Most helpful rated by users:

©2020 WithoutBook