Forwarding email in Java

Example of forwarding email in Java provides examples of sending, receiving, forwarding and deleting emails, Learn how to forward email in java

Forwarding email in Java

Forwarding email in Java

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

  1. import java.util.*;  
  2. import javax.mail.*;  
  3. import javax.mail.internet.*;  
  4.   
  5. public class ForwardMail{  
  6.  public static void main(String[] args)throws Exception {  
  7.   final String user="sam@samtpoint.com";//change accordingly  
  8.   final String password="xxxxx";//change accordingly  
  9.   
  10.   
  11.   //get the session object  
  12.   Properties props = new Properties();  
  13.   props.put("mail.smtp.host""mail.javatpoint.com");  
  14.   props.put("mail.smtp.auth""true");  
  15.   
  16.   Session session = Session.getDefaultInstance(props,  
  17.     new javax.mail.Authenticator() {  
  18.     protected PasswordAuthentication getPasswordAuthentication() {  
  19.         return new PasswordAuthentication(user,password);  
  20.     }  
  21.   });  
  22.    
  23.           
  24.   // Get a Store object and connect to the current host   
  25.   Store store = session.getStore("pop3");  
  26.   store.connect("mail.javatpoint.com",user,password);  
  27.   
  28.   //Create a Folder object and open the folder  
  29.   Folder folder = store.getFolder("inbox");  
  30.   folder.open(Folder.READ_ONLY);  
  31.     
  32.   Message message = folder.getMessage(1);  
  33.   
  34.   // Get all the information from the message  
  35.   String from = InternetAddress.toString(message.getFrom());  
  36.   if (from != null) {  
  37.   System.out.println("From: " + from);  
  38.   }  
  39.   String replyTo = InternetAddress.toString(  
  40.   message.getReplyTo());  
  41.   if (replyTo != null) {  
  42.   System.out.println("Reply-to: " + replyTo);  
  43.   }  
  44.   String to = InternetAddress.toString(  
  45.   message.getRecipients(Message.RecipientType.TO));  
  46.   if (to != null) {  
  47.   System.out.println("To: " + to);  
  48.   }  
  49.   
  50.   String subject = message.getSubject();  
  51.   if (subject != null) {  
  52.   System.out.println("Subject: " + subject);  
  53.   }  
  54.   Date sent = message.getSentDate();  
  55.   if (sent != null) {  
  56.   System.out.println("Sent: " + sent);  
  57.   }  
  58.   System.out.println(message.getContent());  
  59.   
  60.   // compose the message to forward  
  61.   Message message2 = new MimeMessage(session);  
  62.   message2.setSubject("Fwd: " + message.getSubject());  
  63.   message2.setFrom(new InternetAddress(from));  
  64.   message2.addRecipient(Message.RecipientType.TO,  
  65.   new InternetAddress(to));  
  66.   
  67.   // Create your new message part  
  68.   BodyPart messageBodyPart = new MimeBodyPart();  
  69.   messageBodyPart.setText("Oiginal message:\n\n");  
  70.   
  71.   // Create a multi-part to combine the parts  
  72.   Multipart multipart = new MimeMultipart();  
  73.   multipart.addBodyPart(messageBodyPart);  
  74.   
  75.   // Create and fill part for the forwarded content  
  76.   messageBodyPart = new MimeBodyPart();  
  77.   messageBodyPart.setDataHandler(message.getDataHandler());  
  78.   
  79.   // Add part to multi part  
  80.   multipart.addBodyPart(messageBodyPart);  
  81.   
  82.   // Associate multi-part with message  
  83.   message2.setContent(multipart);  
  84.   
  85.   // Send message  
  86.   Transport.send(message2);  
  87.   
  88.   System.out.println("message forwarded ....");  
  89.   }  
  90. }   
  91.      

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 ForwardMail.java
run by c:\> java ForwardMail