Forwarding email in Java

Forwarding email in Java

In this guide we will discuss Forwarding email in Java. about We can forward the received mail to someone else as we send emails. There are many javamail classes that are used to forward the messages to the destination resource.

For better understanding of this example, learn the steps of sending email using JavaMail API first.

For receiving or sending the email using JavaMail API, you need to load the two jar files:

  • mail.jar
  • activation.jar

Example of forwarding email in Java

import java.util.*;  
import javax.mail.*;  
import javax.mail.internet.*;  
  
public class ForwardMail{  
 public static void main(String[] args)throws Exception {  
  final String user="[email protected]";//change accordingly  
  final String password="xxxxx";//change accordingly  
  
  
  //get the session object  
  Properties props = new Properties();  
  props.put("mail.smtp.host", "mail.adglob.in");  
  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");  
  store.connect("mail.adglob.in",user,password);  
  
  //Create a Folder object and open the folder  
  Folder folder = store.getFolder("inbox");  
  folder.open(Folder.READ_ONLY);  
    
  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(  
  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());  
  
  // compose the message to forward  
  Message message2 = new MimeMessage(session);  
  message2.setSubject("Fwd: " + message.getSubject());  
  message2.setFrom(new InternetAddress(from));  
  message2.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  
  message2.setContent(multipart);  
  
  // Send message  
  Transport.send(message2);  
  
  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 filec:\> set classpath=mail.jar;activation.jar;.;
compile the source filec:\> javac ForwardMail.java
run byc:\> java ForwardMail

Next Topic : Click Here

This Post Has 3 Comments

  1. Albert Fang

    I should say also believe that mesothelioma is a rare form of cancers that is commonly found in all those previously subjected to asbestos. Cancerous tissues form inside mesothelium, which is a protecting lining that covers most of the body’s areas. These cells normally form from the lining with the lungs, tummy, or the sac which encircles one’s heart. Thanks for sharing your ideas.

  2. ikaria juice

    Great work! This is the kind of information that are meant to be shared around the net. Disgrace on the search engines for now not positioning this publish upper! Come on over and seek advice from my web site . Thank you =)

Leave a Reply