Replying to messages using Java mail

This Example shows you how reply to message using javamail api. The Message class have a reply() method to configure a new Message with the proper recipient and subject, adding "Re: " if not already there.

Replying to messages using Java mail

Replying to messages using Java mail

     

This Example shows you how reply to message using javamail api. 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("[email protected]"));reply.setText("Thanks");Transport.send(reply);

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

ReplyMali.java


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

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

public class ReplyMali {

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

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

  Session session = Session.getDefaultInstance(properties);

  Store store = session.getStore("pop3");
  store.connect("192.168.10.205", "test", "test");

  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....");
  }

  }
} 

Output:

no. From Subject Date
 1: test@localhost  Fwd: Re: hi..!  18/6/2008
Do you want to reply [y/n
y
 2: test@localhost  Re: hi..!  18/6/2008
Do you want to reply [y/n
n

Download code