0

I have the Mimemessage object already. I tried to append some content to the existing content. Using message.getContent() method, i have content object. Then i set setContent using msg.setContent(appendContent, contenttype). Afterwards, i called msg.saveChanges() also. Then from the same mimemessage object i tried to get Rawinputstream from that using msg.getRawInputstream() method. But this returns the oldcontent not the new one. Why?

MimeMessage msg = new MimeMessage(session,inputstream);
String contenttype = msg.getContentType(); // text/plain; charset=utf-8
String content = msg.getContent();  //oldContent
String newContent = content + "\n some new content";
msg.setContent(newContent,contenttype);
msg.saveChanges();
InputStream ins = msg.getRawInputStream(); // returns oldContent stream why???????????
Roshan
  • 2,019
  • 8
  • 36
  • 56

1 Answers1

0

What version of JavaMail are you using?

JavaMail was designed to handle reading a message from a mail server or creating a new message to send to a mail server. What you're trying to do is "edit" an existing message. There have been bugs in this area of JavaMail in previous releases, and there are probably still bugs in this area in the current release.

In this particular case, there is no "raw input stream" yet for the new content. The content isn't converted to its MIME (raw) format until you write out the message.

The primary purpose of the getRawInputStream method is to allow access to the raw data in cases where the data is incorrectly formatted and JavaMail isn't able to decode or interpret it correctly. Why are you using it in this case?

Bill Shannon
  • 29,579
  • 6
  • 38
  • 40
  • I'm using javamail 1.4.5. I know when i write the Mimemessage object to outputstream using writeTo() method and again constructing mime object from the stream i got, its works fine. but it decreases performance. Shall i write any override method so that i can change the stream of Mime object to represent new content. Can you help to teach how to override method? – Roshan Oct 22 '13 at 05:15
  • You should probably try JavaMail 1.5, just in case this is any better there, although I don't expect it to be. But I think the problem is that JavaMail has no way to produce the raw stream corresponding to the content of a part without doing all the same work it would need to do to write out that part. Again, why do you need the raw content in this case? – Bill Shannon Oct 25 '13 at 00:22
  • Are there any updates regarding this question? Specifically, is there a better way to edit an existing message (ie: retrieve message, manipulate and resend)? Or is the best way to do this still to create a new `MimeMessage` and populate it from the old `MimeMessage` Object? – theyuv Apr 11 '16 at 16:16
  • It depends on exactly what you're trying to do. In general, you can't change a message that you read from a folder, so you need to make a copy and change the copy. If you have a particular case that's not working, you should post a new question with the details. – Bill Shannon Apr 11 '16 at 18:49