JavaMail API - SPLessons

JavaMail Forwarding Email

Home > Lesson > Chapter 8
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

JavaMail Forwarding Email

JavaMail Forwarding Email

shape Description

Forwarding an email to others is a simple task but practically it is very interesting through the coding. The JavaMail Forwarding Email concept is just like to follow the procedure steps of sending email as already seen in previous lessons to send email. But here sending email what ever the email already received by the user. Steps to follow for forwarding emails are shown below. Here by using the JangoSMTP server sending the emails which was received already from another email address.

shape Conceptual figure

Follow the flow steps to forward an email: For the process of forwarding an email involves the session object, POP3 store object, folder, multipart object and transport object after processing done have to close the all connections and objects.

shape Conceptual figure

Flow cycle for receving, sending, forwardind messages.

shape Examples

Let us see how to forward the email with example [c]package firstmail; import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.Date; import java.util.Properties; import javax.mail.BodyPart; import javax.mail.Folder; import javax.mail.Message; import javax.mail.Multipart; import javax.mail.PasswordAuthentication; import javax.mail.Session; import javax.mail.Store; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeBodyPart; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMultipart; public class ForwardMail { public static void main(String[] args) { Properties properties = new Properties(); properties.put("mail.store.protocol", "pop3"); properties.put("mail.pop3s.host", "pop.gmail.com"); properties.put("mail.pop3s.port", "995"); properties.put("mail.smtp.host", "smtp.gmail.com"); properties.put("mail.smtp.socketFactory.port", "465"); properties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); properties.put("mail.smtp.auth", "true"); properties.put("mail.smtp.port", "465"); Session session = Session.getDefaultInstance(properties); try { // session.setDebug(true); // Get a Store object and connect to the current host Store store = session.getStore("pop3s"); store.connect("pop.gmail.com", "9saireddy@gmail.com", "hellosai");//change the user and password accordingly // Create a Folder object and open the folder Folder folder = store.getFolder("inbox"); folder.open(Folder.READ_ONLY); BufferedReader reader = new BufferedReader(new InputStreamReader( System.in)); Message[] messages = folder.getMessages(); if (messages.length != 0) { for (int i = 0, n = messages.length; i < n; i++) { Message message = messages[i]; // 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.print("Do you want to reply [y/n] : "); String ans = reader.readLine(); if ("Y".equals(ans) || "y".equals(ans)) { Message forward = new MimeMessage(session); // Fill in header forward.setRecipients(Message.RecipientType.TO, InternetAddress.parse(from)); forward.setSubject("Fwd: " + message.getSubject()); forward.setFrom(new InternetAddress(to)); // Create the message part MimeBodyPart messageBodyPart = new MimeBodyPart(); // Create a multipart message Multipart multipart = new MimeMultipart(); // set content messageBodyPart.setContent(message, "message/rfc822"); // Add part to multi part multipart.addBodyPart(messageBodyPart); // Associate multi-part with message forward.setContent(multipart); forward.saveChanges(); // Send the message by authenticating the SMTP server // Create a Transport instance and call the sendMessage Transport t = session.getTransport("smtp"); try { //connect to the smpt server using transport instance //change the user and password accordingly t.connect("9saireddy@gmail.com", "splesson"); t.sendMessage(forward, forward.getAllRecipients()); } finally { t.close(); } System.out.println("message forwarded successfully...."); // close the store and folder objects folder.close(false); store.close(); }// end if }// end for }// end if } catch (Exception e) { e.printStackTrace(); } } }[/c] Output When compile the program output will be as follows in the console. Now check the mail.

Summary

shape Key Points

  • JavaMail Forwarding Email - The JavaMail Forwarding Email concept is just like to follow the procedure steps of sending email as already seen in previous lessons to send email.
  • JavaMail Forwarding Email - But here sending email what ever the email already received by the user.
  • For the process of forwarding an email involves the session object, POP3 store object, folder, multipart object and transport object after processing done have to close the all connections and objects.