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:
download these jar files (or) go to the Oracle site to download the latest version.
Example of forwarding email in Java
- import java.util.*;
- import javax.mail.*;
- import javax.mail.internet.*;
-
- public class ForwardMail{
- public static void main(String[] args)throws Exception {
- final String user="sonoojaiswal@javatpoint.com";
- final String password="xxxxx";
-
-
-
- Properties props = new Properties();
- props.put("mail.smtp.host", "mail.javatpoint.com");
- props.put("mail.smtp.auth", "true");
-
- Session session = Session.getDefaultInstance(props,
- new javax.mail.Authenticator() {
- protected PasswordAuthentication getPasswordAuthentication() {
- return new PasswordAuthentication(user,password);
- }
- });
-
-
-
- Store store = session.getStore("pop3");
- store.connect("mail.javatpoint.com",user,password);
-
-
- Folder folder = store.getFolder("inbox");
- folder.open(Folder.READ_ONLY);
-
- Message message = folder.getMessage(1);
-
-
- 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());
-
-
- Message message2 = new MimeMessage(session);
- message2.setSubject("Fwd: " + message.getSubject());
- message2.setFrom(new InternetAddress(from));
- message2.addRecipient(Message.RecipientType.TO,
- new InternetAddress(to));
-
-
- BodyPart messageBodyPart = new MimeBodyPart();
- messageBodyPart.setText("Oiginal message:\n\n");
-
-
- Multipart multipart = new MimeMultipart();
- multipart.addBodyPart(messageBodyPart);
-
-
- messageBodyPart = new MimeBodyPart();
- messageBodyPart.setDataHandler(message.getDataHandler());
-
-
- multipart.addBodyPart(messageBodyPart);
-
-
- message2.setContent(multipart);
-
-
- Transport.send(message2);
-
- System.out.println("message forwarded ....");
- }
- }
-
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 |
|
No comments:
Post a Comment