JAVA MAIL API

JavaMail Projects

JavaMail Project 1

Replying Message or Mail Using JavaMail API
Previous Home Next

The Reply Message or mail using JavaMail API. In this example the Message class includes a reply() method. The reply() method to configure a newMessage with the proper recipient and subject. and then adding "Re" if not already there. no other content add to the message, only copying the from or reply-to header to the new recipient. This method takes a Boolean parameter. That's indicating whether to reply to only the sender (false) or reply to all (true).

MimeMessage reply = (MimeMessage)message.reply(false);
reply.setFrom(new InternetAddress("hellor4r@gmail.com"));
reply.setText("Thanks");
Transport.send(reply);

When sending a message or mails then configure the reply-to address . And using setReplyTo () method.

package R4R;
import java.io.*;
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
public class MailReply {
 @SuppressWarnings("deprecation")
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 ("hello r4r this message is reply...."); } }
Previous Home Next