Forwarding emails using JavaMail API

We are explaining here an example of forwarding emails using JavaMail API.

Steps of forwarding email using JavaMail API:

    1. Get a session instance from getDefaultInstance() or getInstance() method of Session class.
    2. Create the store object (POP3) and connect to the pop store.
    3. Create the folder object by calling getFolder() method on store object and open it in mailbox.
    4. Get the messages from the folder object.
    5. Iterate the emails and get all information like to, from, subject, content etc.
    6. Retrieve the email message to be forwarded.
    7. Create the forward message object using Message.reply() method.
    8. Use send() method of javax.mail.Transport class to send the reply message.
    9. Close the Transport, folder and store objects.

Example:

EmailForwordTest.java
import java.io.*;
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
 
/**
* This class is used to reply email.
* @author javawithease
*/

public class EmailForwordTest {
final String username = "javawithease@gmail.com";
final String password = "****";
final String receiverEmail = "jaisingh@javawithease.com";
final String emailSMTPserver = "smtp.gmail.com";
final String emailSMTPPort = "587";
final String mailStoreType = "pop3s";
 
public EmailForwordTest(String forwardedText){
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", emailSMTPserver);
props.put("mail.smtp.port", emailSMTPPort);
 
try {
Authenticator auth = new SMTPAuthenticator();
Session session = Session.getInstance(props, auth);
 
Store mailStore = session.getStore(mailStoreType);
mailStore.connect(emailSMTPserver, username, password);
 
Folder folder = mailStore.getFolder("INBOX");
folder.open(Folder.READ_ONLY);
 
Message[] messages = folder.getMessages();
System.out.println("Total Message - " + messages.length);
 
BufferedReader reader =
new BufferedReader(new InputStreamReader(System.in));
 
for (int i = 0; i < messages.length; i++) {
Message emailMessage = messages[i];
System.out.println();
System.out.println("Email " + (i+1) + " -");
System.out.println("Subject - " + emailMessage.getSubject());
System.out.println("From - " + emailMessage.getFrom()[0]);
}
 
System.out.print("Enter email number to " +
"which you want to forward: ");
String emailNo = reader.readLine();
 
Message emailMessage =
folder.getMessage(Integer.parseInt(emailNo) - 1);
 
Message mimeMessage = new MimeMessage(session);
mimeMessage = (MimeMessage) emailMessage.reply(false);
mimeMessage.setFrom(new InternetAddress(username));
mimeMessage.setSubject("Fwd: " + mimeMessage.getSubject());
mimeMessage.setText(forwardedText);
mimeMessage.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(receiverEmail));
 
Transport.send(mimeMessage);
System.out.println("Email message " +
"forwarded successfully.");
 
folder.close(false);
mailStore.close();
} catch (Exception e) {
e.printStackTrace();
System.err.println("Error in forwarding email.");
}
}
 
private class SMTPAuthenticator extends
javax.mail.Authenticator {
public PasswordAuthentication
getPasswordAuthentication() {
return new PasswordAuthentication(username,
password);
}
}
 
public static void main(String[] args) {
new EmailForwordTest("Forwarded Email Text.");
}
}

Output:

DEBUG POP3: server doesn't support TOP, disabling it
Total Message - 260
 
Email 1 -
Subject - Welcome to Twitter, javawithease
From - Twitter <info@twitter.com>
 
Email 2 -
Subject - Hindustan Times retweeted a tweet from Twirus India on Twitter!
From - Twitter <info@twitter.com>
Enter email number to which you want to forward: 2
Email message forwarded successfully.

No comments: