Question
Sample code for replying to messages using javamail?

Answers

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 ttSubject ttDate");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 messageMimeMessage reply = (MimeMessage) message[i].reply(false);// Set the from fieldreply.setFrom(message[i].getFrom()[0]);// Create the reply content// Create the reply content, copying over the original if textMimeMessage 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 contentreply.setText(buffer.toString());// Send the messageTransport.send(reply);} else if ("n".equals(ans)) {break;}}} else {System.out.println("There is no msg....");}}}   Your Comment




More Software Questions..
What is the inputsplit in map reduce software?

What is software configuration management?

What Is Java Api For Xml-based Rpc (jax-rpc)?

How can you implement fine-grained auditing?

What is IBM’s simple explanation for Big Data’s four critical features?

What is static synchronized method in JDBC API? Give an example?

What does the NULLIF function do?

What happens if a start method is not invoked and the run method is directly invoked?

Should we override finalize method

what is the difference between mysql_fetch_array and mysql_fetch_object?

How will XML affect my document links?

Why to use Style Sheets?

What are Filters in MVC?

Can you explain Application layer in OSI model?

How to define new testplan attributes?

What are the minimum system requirements to run Photoshop? Is it possible to run Photoshop over linux?

Which oracle package is used to manage the oracle lock management services?

What is Latch Up? Explain Latch Up with cross section of a CMOS Inverter. How do you avoid Latch Up?

What is marker interface?

What types of partitioning are there for BW?



Search
Can you Answer!!
  • Q Which best reflects a so-called 'golden rule' of etiquette?
  • Q How do you differentiate between Marketing and Sales?
  • Q Puerto Rico trench is the deepest part of:
  • Q In which year was Alaska sold to the U.S.?
  • Q With rise of boiling point of a water, what will be the latent heat of steam?
  • Q Is there any way to silence the loud beeps that occur when one incorrectly clicks in the chemdraw 11.0 application?
  • Q What are the verification methods?
  • Q Why do cells of the nephron tubules present a great amount of mitochondria?
  • Q What is credentials?
  • Q Mention what are different tableau files?
  • Q Mumbai is the modern name of which city