Setting multiple content encodings in MimeMessage - java

When using javax.mail.*, I'm trying to send a message with the content encoded in both text/plain and text/html. How can I add both encodings to the MimeMessage?
Does setText override the previous text set? ie: if I do setText("", "text/plain") then setText("", "text/html"), will the secord call override the message text previously set or will they both be present in the message?

Q: How do I send mail with both plain text as well as HTML text so that each mail reader can choose the format appropriate for it?
A: You'll want to send a MIME multipart/alternative message. You construct such a message essentially the same way you construct a multipart/mixed message, using a MimeMultipart object constructed using new MimeMultipart("alternative"). You then insert the text/plain body part as the first part in the multpart and insert the text/html body part as the second part in the multipart. You'll need to construct the plain and html parts yourself to have appropriate content. See RFC2046 for details of the structure of such a message.
http://www.oracle.com/technetwork/java/faq-135477.html#sendmpa

Related

Does javax.email support adding email signatures in the body?

I am using 'javax mail' to send emails, and I want to send emails with a signature in the body, but 'javax mail' doesn’t seem to support setting signatures, what should I do?
"Signatures" don't exist structurally. They're just text at the end of the body. You can prepare whatever content you like to go in the body before sending the message.

SAAJ replaces xop:Include link with inline attachment

We prepare multipart SOAP request using SAAJ implementation provided by WAS 8.5.5 and it looks to be correct until we don't add XOP link referencing attachment being sent as a part:
<xop:Include href="cid:my_attachment_id" xmlns:xop="http://www.w3.org/2004/08/xop/include"/>
Once such link is added into envelope, it's replaced by SAAJ with base64-encoded content of attachment. As a result, attachment is sent twice, in binary and base64 forms.
Any idea how such unexpected inlining could be prevented?
UPD trying to followup the comment
We fill element named "data" of type base64Binary and then check the request with wireshark. Say we have 2 cases:
1) Non-XOP link like below is sent as is, confirmed by wireshark
<data><link href="cid:my_attachment_id"/></data>
2) XOP link formed initially as
<data><xop:Include href="cid:my_attachment_id"
xmlns:xop="http://www.w3.org/2004/08/xop/include"/></data>
according to wireshark is translated into
<data>PGh0dHA6Ly9mb28uY29tPg0KW2Zvb10oaHR0cDov....</data>
The content of "data" element here is exactly base64-encoded attachment identified by the given content-id (my_attachment_id).

JavaMail content-transfer-encoding issue

I have some Java code which sends out an email with code somewhat like the following: Actually i got Mimemessage from Httprequest param and in that mimemessage i'm going to append some content to existing body.
If Mimemessage is of Multipart content-type , i'm not facing any issue while sending message.
If the message is of text/plain and text/html content-type, the content-transfer encoding which i set didn't applied to body.
Based on this docs
Q: Even though JavaMail does all the encoding and decoding for me, I need to manually control the encoding for some body parts. A: In the rare case that you need to control the encoding, there are several ways to override JavaMail's default behavior. A simple approach is as follows. After creating the entire message, call msg.saveChanges() and then use something like mbp.setHeader("Content-Transfer-Encoding", "base64") to force base64 encoding for the given body part.
Another approach is to subclass MimeBodyPart and override the updateHeaders method so that it first calls super.updateHeaders() and then sets the Content-Transfer-Encoding header as above.
Applied above also. But it doesn't works for me.
InputStream ins = request.getInputStream();
MimeMessage msg = new MimeMessage(session,ins);
msg.setContent("some non-Ascii content","text/plain; charset="UTF-8"");
//Tried setheader before saveChanges() method, also doesn't work for me
//msg.setHeader("Content-Transfer-Encoding","base64");
msg.saveChanges();
//Now tried based on above docs after saveChanges method, also doesn't work
msg.setHeader("Content-Transfer-Encoding","base64");
please help to solve this.
You duplicated most of this question in your other post, and I answered part of it there.
You would probably be better off sending the content for the mail message in the http request, then creating a new message on the server based on that content, instead of trying to send a complete MIME message to the server that you then edit.

GAE: Incoming emails can not preserve format

I've set up my GAE/Java project to receiving emails and it works pretty fine excepting it can not preserve the incoming mail's format(e.g. bold, italic, font size, text color, bulleted list...), and the content type of incoming mails are always "text/plain", as a result from the end user's view the mail content huddled and unreadable.
For example I send a formatted mail from Gmail, when I receiving the mail in GAE all formats is tripped off and leaves a bulk of plain text.
Is there any way I can get incoming mail type as HTML so the format would be preserved?
While sending the mail through server. Set the body content type text/html.
.
.
.
htmlPart = new MimeBodyPart();
htmlPart.setContent("<b>html content</b>", "text/html");
This should work for you..
Looks like a duplicate of this question and answer
Moreover, I am copying a few excerpts from Google App Engine Documentation here which says:
The message contains a subject, a plaintext body, and an optional HTML body.
It can also contain file attachments, as well as a limited set of headers.
And I am guessing the content type should be text/html

Setting Content-Type for MimeMessage?

I have one confusion about content type of mime message. Say I have a mime message. It is a multipart message and the body parts are like this
Mime body part containing plain text, html text(like some letters in
bold in body)
Second mime body part containing an attachment,
Third mime body part containing one inline image (which is being referred from body with cid)
When I am creating the body part, should I explicitly set the content type for top mime message and then each body part?
If yes, what should they be in the above example?
multipart/alternative is suggested for html, multipart/mixed is suggested for attachments, multipart/related is suggested for inline. I am using all of them, so what should be content-Type for complete message and different body parts?
Just for information I tried to replicate above scenario where I did not set the content type neither for the overall MimeMessage nor for body parts.
But still I get the expected stuff like plain text, Bold letters in body, attachment, inline image on james at right place
How come James is interpreting the mime message and body parts without setting the content type, and how come it is displaying them in right fashion?
Code For Reference
MimeMessage msg = new MimeMessage(mailSession);
MimeMultipart mpart = new MimeMultipart();
MimeBodyPart bp = new MimeBodyPart();
bp.setText("plain text and html text like<b>Test</>", CHARSET_UTF_8, MESSAGE_HTML_CONTENT_TYPE);
// add message body
mpart.addBodyPart(bp);
// adding attachment
MimeBodyPart bodyPart = new MimeBodyPart();
bodyPart.setFileName("WordFile1");
file = new File("word file");
DataSource source = new FileDataSource(file);
bodyPart.setDataHandler(new DataHandler(source));
mpart.addBodyPart(bodyPart);
// adding image inline
MimeBodyPart bodyPart2 = new MimeBodyPart();
bodyPart2.setFileName("inline image");
file2 = new File("image1");
DataSource source2 = new FileDataSource(file);
bodyPart2.setDataHandler(new DataHandler(source));
bodyPart2.setDisposition(MimeBodyPart.INLINE);
bodyPart2.setHeader("Content-ID", "Unique-CntentId");
bodyPart2.setHeader("Content-Type", "image/jpeg");
mpart.addBodyPart(bodyPart2);
// At last setting multipart In MimeMessage
msg.setContent(mpart);
With the above code, I get the correct html text, plain text, inline image and attachments at right place in ThunderBird integrated with James.
So I don't understand when and where to set multipart/mixed, multipart/alternative, multipart/related as Content-Type or does the mail server internally set it?
If I understand what you're trying to do, you want a message with this structure:
multipart/mixed
multipart/alternative
text/plain - a plain text version of the main message body
multipart/related
text/html - the html version of the main message body
image/jpeg - an image referenced by the main body
application/octet-stream (or whatever) - the attachment
That means three nested multipart pieces. You'll need to specify the subtype for each multipart piece other than the default "mixed".
The multipart/mixed and multipart/alternative pieces are relatively straightforward. The multipart/related piece is more complicated and you might want to read RFC 2387 and/or find some other tutorials to help you with that.
You can simplify the structure by getting rid of the multipart/related and just having the html text reference an image somewhere on the internet.
You should also test that a message with this structure is going to be displayed properly by all the mail readers you care about. Some mail readers will do a better job than others with a complicated structure such as this.

Categories

Resources