Principais perguntas e respostas de entrevista e testes online
Plataforma educacional para preparacao de entrevistas, testes online, tutoriais e pratica ao vivo

Desenvolva habilidades com trilhas de aprendizado focadas, simulados e conteudo pronto para entrevistas.

WithoutBook reune perguntas de entrevista por assunto, testes praticos online, tutoriais e guias comparativos em um unico espaco de aprendizado responsivo.

Preparar entrevista

Java Mail perguntas e respostas de entrevista

Question: Sample code for Forwarding Messages using JavaMail.
Answer: 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.


package com.withoutbook.common;

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.110";
String user = "arindam";
String password = "arindam";

// Get system properties
Properties properties = System.getProperties();
properties.setProperty("mail.smtp.host", "192.168.10.110");

// 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: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
forward.setContent(multipart);

// Send message
Transport.send(forward);

System.out.println("msg forward ....");
}
}

Salvar para revisao

Adicione este item aos favoritos, marque-o como dificil ou coloque-o em um conjunto de revisao.

Abrir minha biblioteca de aprendizado
Isto e util? Sim Nao

Mais uteis segundo os usuarios:

Copyright © 2026, WithoutBook.