<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<p>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.</p>
<h2>Forwarding Messages using Java Mail</h2>
<p>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.</p>
<p>For better understanding of this example, learn the steps of sending email using JavaMail API first.<br />
For receiving or sending the email using JavaMail API, you need to load the two jar files:</p>
<ul style="text-align: left;">
<li>mail.jar</li>
<li>activation.jar</li>
</ul>
<h2><b>Create Java Class</b></h2>
<p>Create a java class file <i><b>ForwardEmail</b></i>, the contents of which are as follows:</p>
<pre class="highlight">import java.util.*; 
import javax.mail.*; 
import javax.mail.internet.*; 
 
public class ForwardMail { 
 public static void main(String[] args) { 
 Properties props = new Properties(); 
 props.put("mail.smtp.host", "smtp.gmail.com"); 
 props.put("mail.smtp.socketFactory.port", "465"); 
 props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory"); 
 props.put("mail.smtp.auth", "true"); 
 props.put("mail.smtp.port", "465"); 
 final String username="dineshonjava@gmail.com"; 
 final String password="******"; 
 Session session = Session.getDefaultInstance(props, 
 new javax.mail.Authenticator() { 
 protected PasswordAuthentication getPasswordAuthentication() { 
 return new PasswordAuthentication(username,password); 
 } 
 }); 
 try { 
 //Message message = new MimeMessage(session); 
 //message.setFrom(new InternetAddress("dineshonjava@gmail.com")); 
 //message.setRecipients(Message.RecipientType.TO,InternetAddress.parse("admin@dineshonjava.com")); 
 // Get a Store object and connect to the current host 
 Store store = session.getStore("imaps"); 
 store.connect("imap.gmail.com","dineshonjava@gmail.com", 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:nn"); 
 
 // 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 ...."); 
 } catch (Exception e) { 
 throw new RuntimeException(e); 
 } 
 } 
} 
</pre>
<h2><b>Compile and Run</b></h2>
<p>Now that our class is ready, let us compile the above class. I&#8217;ve saved the class <b><i>ForwardEmail</i></b>.java</p>
<p>We would need the jars <b><i>javax.mail.jar </i></b>and activation.jar in the classpath. Execute the command below to compile the class</p>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2014/01/1111-5.png" border="0" /></div>
<h2><b>Verify Output</b></h2>
<p>You should see the following message on the command console:</p>
<p><i>From: Gmail Team <;mail-noreply@google.com>;<br />
Reply-to: Gmail Team <;mail-noreply@google.com>;<br />
To: Dinesh Rajput <;dineshonjava@gmail.com>;<br />
Subject: Get Gmail on your mobile phone<br />
Sent: Sun Apr 15 21:31:38 IST 2012<br />
javax.mail.internet.MimeMultipart@1719d5b<br />
message forwarded &#8230;.</i></p>
<p> ;</p>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2014/01/1111-6.png" border="0" /></div>
</div>
<div style="background-color: pink; border-width: thin; text-align: center;"><b><;<;<a href="https://dineshonjava.com/sending-email-with-inline-images/">Previous</a> <;<; || <a href="https://dineshonjava.com/java-mail-api-tutorial/">Index </a>|| >;>;<a href="https://dineshonjava.com/deleting-email-in-java-mail-api/" target="_blank" rel="noopener">Next</a> >;>;</b></div>
<p> ;</p>
</div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/sending-email-with-inline-images/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/deleting-email-in-java-mail-api/">Next</a> 
									 </div> 
									</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
 $.post('https://dineshonjava.com/wp-admin/admin-ajax.php', {action: 'mts_view_count', id: '241'});
});
</script>
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…