GAE: Incoming emails can not preserve format - java

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

Related

html email body sent using javamail api appears garbled in outlook

I am using below code to send mail to multiple users across different domains.
String emailBody = "<html>Some html text goes here</html>"
String from = "from#domain1.com"
String bcc = "bcc#domain2.com"
String subject = "Some subject..."
List attachments = "Some image attachments ..."
MimeMessage message = new MimeMessage(session);
message.setHeader("Content-Type", "text/html; charset=UTF-16")
message.setHeader("Accept-Language", "en-US")
message.setHeader("Content-Language", "en-US")
message.setFrom(new InternetAddress(from));
message.setRecipients(Message.RecipientType.BCC,InternetAddress.parse(bcc));
message.setSubject(subject);
// creates message part
MimeBodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setContent(emailBody, "text/html; charset=UTF-16");
// creates multi-part
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
addAttachmentsToMultiPart(multipart,attachments)
message.setContent(multipart);
Transport.send(message);
The email body is entirely is in English. But users of few domains are recieving it in Chinese characters. When I viewed the html source of the body, I found it is partially garbled. Some orginal html text is intact and some junk characters in between text with no particular order. This mail travels this path "Tomcat application -> company mail server -> outlook.office365.com server -> target mail server -> End user outlook client".
After some research, I found this issue could be due to Character encoding. For testing it, I tried sending this mail to gamil.com. The mail body appears correctly in gmail, but I notice following headers are found in headers part and entire body part is base64 encoded.
Content-Type: text/html; charset="UTF-16"
Content-Transfer-Encoding: base64
I am not sure how "Content-Transfer-Encoding" is being set on headers and where is mail body getting encoded. But I don't see similar headers in End User mail properties. I tried explicitly setting "Content-Transfer-Encoding" to "base64", "quoted-printable" ( and other types), but it is always ignored and gmail always recieves it as base64. And this setting has no impact end user mails. They always recieves same garbage.
Can anyone help me understand what could be issue and how to resolve it
We have code that is very similar to what you have written, the only differences which you could try:
We don't set any headers in the message
We use a slightly different body content type - text/html; charset=utf-8
As an aside, we use com.google.common.net.MediaType to pass the media type into the e-mail sending class, so then you can use MediaType.toString() to get the content type.
I resolved after lot of research and experiment. There were basically two issues
Character-set should not be set at MimeMessage level but at individual MimeBodyPart level.
Changed the UTF-16 to UTF-8. Some email client/server cannot handle UTF-16 properly or they are not configured to handle UTF-16.

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.

Mail Inline Images display Error?

Is there a way that online mail clients like gmail display images that are sent inline, i.e., embedded in the mail?
What is the best way to send images in email? Is sending online the link the best, because most online clients support that?
Normally, mail clients like Gmail display the inline image appropriately if the image is properly inlined in the mail. In context to mail plugin in grails, I was able to achieve the inline image in gmail by doing something like:
sendMail {
multipart true
// we send the image type regardless if the email client renders in html or plain text. If plain text
// jpg will be attached. Not a lot can be done since we do not know how the email client will render.
// if html, then image will be embedded in html and will not be attached and will be downloaded since image is not
// being loaded from external site.
if(filesToAttach){
filesToAttach.each{ file ->
inline file.tokenize(".").get(0), "image/jpeg", new ClassPathResource("/${file}", this.getClass())
}
}
to recipient
from from
subject subject
html view: view, model: [//my model]
}
Generally, it depends on the email client to render the message according to its setup. However, the actual implementation according to the above example has shown positive results.

Some questions related to implementation of image inside email signature?

i need to implement the email signature with image.As of now we only support the text in email signature which is already working.i need to provide the functionality
where i can insert the image inside mail signature. i can send the email to user within myapplication and also to user on external mail domain like gmail,yahoo etc. When
mail is sent to some user with in my application system, system makes entryt o DB and when receiver receives in inbox (which internally read the mail from db). Now if user
send the mail to external user on gmail it makes use of javax mail api . Similary i can receive the email from external mail domains(gmail,yahoo etc) Now i have
few questions based on tis requirement:-
1)Is there any standard for how the external mail domains like gmail send the image inside signature to another domains like (my application mail domain)?
Another point related to it gmail user can have two images ,one for signature and another image inside body. How will i determine which image belongs to
signature? Is there any defined property for that?
2)Also not able to make out what is the best/consistent approach to send(whether to internal application user or external mail domain user ) the email signature containing
image so that it renders correctly when user receives it?
what I had in my mind for point 2:- i earlier thought i can use solution suggested at How to display an image in jsp?. where
with tag <.img src="/getImage.action?imageId=123">, i can fetch the image from db in action class or servlet and return. But keeping in mind
once i send the mail to the user on gmail , he will not be able to access the servlet.So this approach does not seems to fit in requirement.
Then i came across the another great stackoverflow link base64 encoded images in email signatures where
solution by Tim Medora looked great but again the comment below the solution Gmail doesn't seem to support it again ended my Folks
really i think i should be done if mail domain like gmail,yahoo support the solution suggested by because in that case i can send image as base64 string instead
of image as attachment.
Folks would be really grateful if you can provide me some pointer/approach regarding both points 1 and 2
To include images in the email message, first you have to include the images as MIME attachments in the email. Each of these attachments must have a "Content-ID" header.
--f46d0444ea0d6991ba04b91c92e6
Content-Type: image/gif; name="theImage.gif"
Content-Transfer-Encoding: base64
Content-ID: <theImage#abcd>
[base64 string]
--f46d0444ea0d6991ba04b91c92e6--
2) Then, in the email message, include the Content-ID in the src attribute of the <img> tag.
<img src="cid:theImage#abcd" />
For Gmail to see the embedded image from byte array, I posted an answer on another similar question which is to use ByteArrayDataSource and embed it to the HtmlEmail. Here's the code snippet:
import javax.mail.util.ByteArrayDataSource;
import org.apache.commons.mail.ImageHtmlEmail;
...
ImageHtmlEmail email = new ImageHtmlEmail();
byte[] qrImageBytes = createQRCode(); // get your image byte array
ByteArrayDataSource qrImageDataSource = new ByteArrayDataSource(qrImageBytes, "image/png");
String contentId = email.embed(qrImageDataSource, "QR Image");

Setting multiple content encodings in MimeMessage

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

Categories

Resources