Forwarding Messages using Java Mail

This Example shows you how to forward a message using javamail api. there is no method to forward a mail from one user to another user.

Forwarding Messages using Java Mail

Forwarding Messages using Java Mail

     

This Example shows you how to forward a message using javamail api. there is no method to forward a mail from one user to another user. if u want forward a message to another user firstly get all header field and get message content then send its for another user.
Java code given below gives the functionality to forward same message to the next email address. With this code user can change Subject line and can edit the message content that is going to forward.

ForwardMail.java


import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;

public class ForwardMail {

  public static void main(String args[]) throws Exception {
  String host = "192.168.10.205";
  String user = "test";
  String password = "test";

  // Get system properties
  Properties properties = System.getProperties();
  properties.setProperty("mail.smtp.host", "192.168.10.205");

  // Get the default Session object.
  Session session = Session.getDefaultInstance(properties);

  // Get a Store object that implements the specified protocol.
  Store store = session.getStore("pop3");

  //Connect to the current host using the specified username and password.
  store.connect(host, user, password);

  //Create a Folder object corresponding to the given name.
  Folder folder = store.getFolder("inbox");

  // Open the Folder.
  folder.open(Folder.READ_ONLY);

  Message message = folder.getMessage(1);

  // Here's the big change...
  String from = InternetAddress.toString(message.getFrom());
  if (from != null) {
  System.out.println("From: " + from);
  }
  String replyTo = InternetAddress.toString(
  message.getReplyTo());
  if (replyTo != null) {
  System.out.println("Reply-to: " + replyTo);
  }
  String to = InternetAddress.toString(
  message.getRecipients(Message.RecipientType.TO));
  if (to != null) {
  System.out.println("To: " + to);
  }

  String subject = message.getSubject();
  if (subject != null) {
  System.out.println("Subject: " + subject);
  }
  Date sent = message.getSentDate();
  if (sent != null) {
  System.out.println("Sent: " + sent);
  }
  System.out.println(message.getContent());

  // Create the message to forward
  Message forward = new MimeMessage(session);

  // Fill in header
  forward.setSubject("Fwd: " + message.getSubject());
  forward.setFrom(new InternetAddress(from));
  forward.addRecipient(Message.RecipientType.TO,
  new InternetAddress(to));

  // Create your new message part
  BodyPart messageBodyPart = new MimeBodyPart();
  messageBodyPart.setText("Oiginal message:\n\n");

  // Create a multi-part to combine the parts
  Multipart multipart = new MimeMultipart();
  multipart.addBodyPart(messageBodyPart);

  // Create and fill part for the forwarded content
  messageBodyPart = new MimeBodyPart();
  messageBodyPart.setDataHandler(message.getDataHandler());

  // Add part to multi part
  multipart.addBodyPart(messageBodyPart);

  // Associate multi-part with message
  forward.setContent(multipart);

  // Send message
  Transport.send(forward);

  System.out.println("msg forward ....");
  }
} 

Output:

From: test@localhost
Reply-to: test@localhost
To: test@localhost
Subject: Fwd: Re: hi..!
Sent: Fri Jul 18 21:59:24 IST 2008
javax.mail.internet.MimeMultipart@b0f13d
msg forward ....

Download code