JAVA MAIL API

JavaMail Projects

JavaMail Project 1

Forwarding Message or Mail Using JavaMailAPI
Previous Home Next
adplus-dvertising

When you want to forward Mails or Messages. for directly forwarding for one or many E-mails. Then in Forwarding Messages is a little more involved. You forwarding the message for using JavaMail API. there is no only one method to call .Actually a Mail Messages can be made up of multiple parts. The Every part is BodyPart, or more specifically, and when working you MIME Messages then a MimeBodyPart.

The other or different BodyPart is get combined into a container, this container also called Multipart or, again, more specifically a MimeMultipart. When forwarding messages then it's very essentially using DataHandler. to copy the content one message to another, just copy over it's DataHandler. The DataHandler class from JavaBean Activation Framework.

package R4R;
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
public class MsgForward {
  public static void main(String args[]) throws Exception {
  String host = "192.168.10.205";
  String username = "hellor4r@localhost";
  String password = "hellor4r@localhost";
  // 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, username, 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("The total messages are forward ...."); } }
Previous Home Next