Send mail to javamail (or ftpmail?) and save attachment - java

I have been searching for a solution for my problem for a while now.
It work to send mail through javamail, and get an attachment to save. But the problem is that I cant get swedish letters like 'åäö' to show. The file is saved in ISO-Latin-1. (Filename is like "ISO-8859-1HwhajkAWJKHWo..."). I have tried to decode it and every solution that I've found searching.
But it wont work, doesn't matter how much I try, it wont work. Anyone have a similar problem?
And then I was thinking is it possible to send an email to ftpmail(?) and using a java program to catch the attachment in the file and save it. With all the letters copied correctly (åäö).
Or does anyone have another solution to fix this? Send an email to a server who will look through the mail and if it got attachment, save it.

Really, javax.mail works fine with UTF-8. You have to set it for subject, content and text attachments.
MimeMessage message = new MimeMessage(session);
message.setSubject(subject, "UTF-8");
message.setHeader("Content-Type", "text/plain; charset=UTF-8");
message.setText(body, "UTF-8");

Related

Missing email using JavaMail on Google App Engine

I have this really weird problem and I'm not able to find a solution... I hope you can help me...
I'm working in Google App Engine to build my App (lets call it "MyApp"), to test, I have a cloned app renamed as "sandbox-MyApp".
I need to allow my users send a mail with some data, so I have a form where they can fill some information that will be added to the message.
I've working with this scenario a long time ago, but now I'm having issues, because, for some reason, my out-coming mails are not being received by the recipients...
It's a really weird thing about this, because, I can send one or two mails without problem, but after that, they suddenly stop, and after some code-changes, they work again.
I'm using Java.Mail to do the work,
I'm trying to send a simple HTML,
My "from" address is something like "userName#sandbox-myApp.appspotmail.com"
My Subject its something like: "Hello userName! There is some important message for you"
The message it's really simple, includes an image logo (served by an https://sandbox-myApp.appspot.com/img/logo.png), an invitation text and a single link to my app URL... (https://sandbox-myApp.appspot.com/)
My code it's real simple, based on the Google Documentation.
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(senderAddress, MimeUtility.encodeText(senderLabel, "UTF-8", "B"), "UTF-8"));
msg.addRecipient(javax.mail.internet.MimeMessage.RecipientType.TO, new InternetAddress(receiverAddress, receiverLabel, "UTF-8"));
if(responseAddress != null && !responseAddress.trim().isEmpty()){
msg.setReplyTo(new Address[] {
new InternetAddress(responseAddress, MimeUtility.encodeText(senderLabel, "UTF-8", "B"))
});
}
msg.setSubject(MimeUtility.encodeText(subject, "UTF-8", "B"), "UTF-8");
msg.setContent(msgBody, "text/html;charset=UTF-8");
Transport.send(msg);
I've tried changing "from" to something like "app_admin#mydomain.com" and it works for a while, but after some mails (about 5 or 6), stop working too.
Most shocking thing: There aren't any error message on logs... The Cuota Viewer counts every sent mail (so I suppose it must being blocked somewhere else),
I've modiffied the message to omit any URL on the body and It works better, but I need to include it!.
Problem is tracked on https://code.google.com/p/googleappengine/issues/detail?id=12786
Workaround which worked for my application is to not use appspot.com domain.
Register custom domain for the application and then mails to the application using the custom domain work.

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

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");

Sending images by mail

I've written a mail that'll send emails with images as attachments. The images are displaying in yahoo and gmail. But hotmail is displaying a square grey box wherever there should be an image. The following is the code that builds the image. Of course I'm reffering to it in the mail using cid. Thanks a lot in advance.
l_embedImage = new MimeBodyPart();
l_dataSource = new FileDataSource(new File(l_imagesBaseDirectory + "/" + l_completeImagePath));
l_dataHandler = new DataHandler(l_dataSource);
l_embedImage.setDataHandler(l_dataHandler);
l_embedImage.setHeader("Content-ID", "<" + l_cid + ">");
l_embedImage.setHeader("Content-Type", "image/gif");
l_embedImage.setHeader("discrete-type","image");
l_embedImage.setHeader("content-transfer-encoding", "base64");
p_multipart.addBodyPart(l_embedImage);
I was having troubles with delivering e-mails until I went through the steps to become a trusted mail server.
Check out step 4 ("Authenticate your outbound e-mail: Publish Sender Policy Framework (SPF) records") in the following URL:
http://postmaster.msn.com/Guidelines.aspx
I think that hotmail doesn't load them automatically, the user should agree on this message first:
Attachments, pictures, and links in
this message have been blocked for
your safety. Show content
As usual Microsoft must complete programmers life ;)

Categories

Resources