Forwarding email using java mail api

We can forward the got mail to another person as we send messages. There are numerous javamail classes that are utilized to forward the messages to the destination asset.

For better comprehension of this case, take in the progressions of sending email utilizing JavaMail API first.

For accepting or sending the email utilizing JavaMail API, you have to stack the two container records:

  • mail.jar
  • activation.jar

download these jar files go to the Oracle site to download the latest version.

Illustration of sending email utilizing JavaMail API

import java.util.*;  
import javax.mail.*;  
import javax.mail.internet.*;  
public class ForwardMail{  
 public static void main(String[] args)throws Exception {  
  final String user="";//change as needs be  
  final String password="******";//change as needs be  
  //get the session object  
  Properties props = new Properties();  
  props.put("", "");  
  props.put("mail.smtp.auth", "true");  
  Session session = Session.getDefaultInstance(props,  
    new javax.mail.Authenticator() {  
    protected PasswordAuthentication getPasswordAuthentication() {  
        return new PasswordAuthentication(user,password);  
  // Get a Store object and connect to the current host   
  Store store = session.getStore("pop3");  
  //Create a Folder object and open the folder  
  Folder folder = store.getFolder("inbox");;  
  Message message = folder.getMessage(1);  
  // Get all the information from the message  
  String from = InternetAddress.toString(message.getFrom());  
  if (from != null) {  
  System.out.println("From: " + from);  
  String replyTo = InternetAddress.toString(  
  if (replyTo != null) {  
  System.out.println("Reply-to: " + replyTo);  
  String to = InternetAddress.toString(  
  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);  
  // compose the message to forward  
  Message message2 = new MimeMessage(session);  
  message2.setSubject("Fwd: " + message.getSubject());  
  message2.setFrom(new InternetAddress(from));  
  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();  
  // Create and fill part for the forwarded content  
  messageBodyPart = new MimeBodyPart();  
  // Add part to multi part  
  // Associate multi-part with message  
  // Send message  
  System.out.println("message forwarded ....");  

As you can see in the above example, we are able to forward the email to the destination resource. Now run this program by :

Load the jar file c:\> set classpath=mail.jar;activation.jar;.;
compile the source file c:\> javac
run by c:\> java ForwardMail