Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Javamail Forwarding Messages using Java Mail

Related Tutorials


 
 

Share on Google+Share on Google+

Forwarding Messages using Java Mail

Advertisement
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.

Forwarding Messages using Java Mail

     

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.
Java code given below gives the functionality to forward same message to the next email address. With this code user can change Subject line and can edit the message content that is going to forward.

ForwardMail.java


import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;

public class ForwardMail {

  public static void main(String args[]) throws Exception {
  String host = "192.168.10.205";
  String user = "test";
  String password = "test";

  // 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, user, 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("msg forward ....");
  }
} 

Output:

From: test@localhost
Reply-to: test@localhost
To: test@localhost
Subject: Fwd: Re: hi..!
Sent: Fri Jul 18 21:59:24 IST 2008
javax.mail.internet.MimeMultipart@b0f13d
msg forward ....

Download code

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: December 19, 2008

Related Tutorials

Discuss: Forwarding Messages using Java Mail  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:5
Nilaxi
February 3, 2012
Error for the following code

im getting below error when i run the script Exception in thread "main" javax.mail.MessagingException: Connect failed; nested exception is: java.net.ConnectException: Connection refused at com.sun.mail.pop3.POP3Store.protocolConnect(POP3Store.java:148) at javax.mail.Service.connect(Service.java:275) at javax.mail.Service.connect(Service.java:156) at ForwardMail.main(ForwardMail.java:24) Caused by: java.net.ConnectException: Connection refused at java.net.PlainSocketImpl.socketConnect(Native Method) at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:327) at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:193) at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:180) at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:384) at java.net.Socket.connect(Socket.java:546) at java.net.Socket.connect(Socket.java:495) at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:232) at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:189) at com.sun.mail.pop3.Protocol.<init>(Protocol.java:81) at com.sun.mail.pop3.POP3Store.getPort(POP3Store.java:201) at com.sun.mail.pop3.POP3Store.protocolConnect(POP3Store.java:144) ... 3 more Java Result: 1
alok
February 8, 2012
Forwarding Messages using Java Mail

On compiling above program is giving error: Exception in thread "main" javax.mail.MessagingException: IOException while sending message; nested exception is: java.io.IOException: javax.mail.MessagingException: No MimeBodyPart content at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1141) at javax.mail.Transport.send0(Transport.java:195) at javax.mail.Transport.send(Transport.java:124) at ForwardMail.main(ForwardMail.java:86) Caused by: java.io.IOException: javax.mail.MessagingException: No MimeBodyPart content at com.sun.mail.handlers.multipart_mixed.writeTo(multipart_mixed.java:105) at javax.activation.ObjectDataContentHandler.writeTo(DataHandler.java:869) at javax.activation.DataHandler.writeTo(DataHandler.java:302) at javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:1476) at javax.mail.internet.MimeMessage.writeTo(MimeMessage.java:1772) at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1099) ... 3 more
alok
February 8, 2012
Forwarding Messages using Java Mail

Exception in thread "main" javax.mail.MessagingException: IOException while sending message; nested exception is: java.io.IOException: javax.mail.MessagingException: No MimeBodyPart content at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1141) at javax.mail.Transport.send0(Transport.java:195) at javax.mail.Transport.send(Transport.java:124) at ForwardMail.main(ForwardMail.java:86) Caused by: java.io.IOException: javax.mail.MessagingException: No MimeBodyPart content at com.sun.mail.handlers.multipart_mixed.writeTo(multipart_mixed.java:105) at javax.activation.ObjectDataContentHandler.writeTo(DataHandler.java:869) at javax.activation.DataHandler.writeTo(DataHandler.java:302) at javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:1476) at javax.mail.internet.MimeMessage.writeTo(MimeMessage.java:1772) at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1099) ... 3 more
Deepak Singh
July 20, 2012
No Working getting Exception

javax.mail.MessagingException: IOException while sending message; nested exception is: java.io.IOException: javax.mail.MessagingException: No MimeBodyPart content at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1141) at javax.mail.Transport.send0(Transport.java:195) at javax.mail.Transport.send(Transport.java:124) at com.airtel.mail.ForwardEmail.SendMail(ForwardEmail.java:136) at com.airtel.mail.ForwardEmail.main(ForwardEmail.java:101) Caused by: java.io.IOException: javax.mail.MessagingException: No MimeBodyPart content at com.sun.mail.handlers.multipart_mixed.writeTo(multipart_mixed.java:105) at javax.activation.ObjectDataContentHandler.writeTo(Unknown Source) at javax.activation.DataHandler.writeTo(Unknown Source) at javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:1476) at javax.mail.internet.MimeMessage.writeTo(MimeMessage.java:1772) at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1099) ... 4 more
Deepak Singh
July 20, 2012
No Working getting Exception

I am getting exception while using code for forwarding Mail. Exception in mentioned below: javax.mail.MessagingException: IOException while sending message; nested exception is: java.io.IOException: javax.mail.MessagingException: No MimeBodyPart content at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1141) at javax.mail.Transport.send0(Transport.java:195) at javax.mail.Transport.send(Transport.java:124) at com.airtel.mail.ForwardEmail.SendMail(ForwardEmail.java:136) at com.airtel.mail.ForwardEmail.main(ForwardEmail.java:101) Caused by: java.io.IOException: javax.mail.MessagingException: No MimeBodyPart content at com.sun.mail.handlers.multipart_mixed.writeTo(multipart_mixed.java:105) at javax.activation.ObjectDataContentHandler.writeTo(Unknown Source) at javax.activation.DataHandler.writeTo(Unknown Source) at javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:1476) at javax.mail.internet.MimeMessage.writeTo(MimeMessage.java:1772) at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1099) ... 4 more
DMCA.com