Reputation: 904
I am using the JavaMail API to get some emails. I would like to get a Stream of the Messages and then on the other side get the Stream back to a email. Also I don't want to lose any properties like attachments, destination, sender, body, etc....
How can accomplish this?
Upvotes: 1
Views: 2752
Reputation: 904
Ok... I managed to find out the answer how this can be accomplished
we can use the
.writeTo( out );
to write into a OutputStream, send it a InputStream and finally you can reconstruct it using this
Message receivedMail = new MimeMessage( session, inputStream );
problem fixed!
Upvotes: 2
Reputation: 34367
Please check the sample code below:
URLName url = new URLName("pop3","xxxx",123,"","user","password");
Session session = Session.getInstance(props, null);
Store store = new POP3SSLStore(session,url);
store.connect();
Folder folder = store.getFolder("INBOX");
folder.open(Folder.READ_WRITE);
Message message[] = folder.getMessages();
for (int i=0; i <message.length;i++) {
Message message = messages[i];
//Get Message Properties
System.out.println("From : " + message.getFrom()[0]);
System.out.println("Subject : " + message.getSubject());
System.out.println("Sent Date : " + message.getSentDate());
//Get Input stream for each message
InputStream is = message.getInputStream();
.....
is.close();
}
folder.close(false);
store.close();
To construct the message backwards:
MimeMessage newMessage = new MimeMessage(session);
MimeMultipart mimeMultipart = new MimeMultipart();
MimeBodyPart attachment = new MimeBodyPart(is);
attachment.setHeader("Content-Type", "contentType");
mimeMultipart.addBodyPart(attachment);
newMessage.setContent(mimeMultipart);
newMessage.setFrom(InternetAddress.parse("fromAddress")[0]);
newMessage.setReplyTo(InternetAddress.parse("toAddress"));
newMessage.setSubject("subject");
Upvotes: 1