Sending images by mail - java

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

Related

Apple or Mac Mail opens all attachment images even when they are embedded

When we send an email from our tomcat server, implementing MimeMultiPart, it opens in most mail software just fine, e.g. Gmail, Outlook, & Android Mail.
But when it is opened on Apple Mail, it automatically opens PDF and images, which is permanent in mobile(phone and tablet, as laptops can be changed in command).
This is how it is designed for Apple, as I have read in a couple of websites.
The problem is, even the embedded, supposedly a hidden attachment, is also shown.
This results in double image, as we call the embedded via html in the mail.
The image is a logo, so this gets emailed always. I was hoping that there is a different protocol I can use that also works well in Apple mail. I haven't seen a similar issue in the web, so I have hope that we are just using some different protocol.
BodyPart messageBodyPart = new MimeBodyPart();
String htmlText = message + "<img src=\"cid:image123\">";
messageBodyPart.setContent(htmlText, "text/html; charset=UTF-8");
MimeMultipart mp = new MimeMultipart("mixed");
mp.addBodyPart(messageBodyPart);
BodyPart imageBodyPart = new MimeBodyPart();
String file = this.getClass().getClassLoader().getResource("images/Logo.gif").getFile();
DataSource fds = new FileDataSource(file);
imageBodyPart.setFileName("Logo.gif");
imageBodyPart.setHeader("Content-ID","<image123>");
imageBodyPart.setDisposition(Part.INLINE);
mp.addBodyPart(imageBodyPart);
When I remove the HTML code, it still shows the attached image in Apple mail, however, It will not show completely in other email software.
I've seen this behaviour before as well, which I recall was due to a difference in the MIME header parsing logic on the iOS devices.
This other post (and corresponding answers) refers and should guide you towards a working solution: Problem sending multipart mail using ActionMailer
Good luck and please let us know how you get on.
Defect finally on its way to production.
With slight variation in MIME structure to https://stackoverflow.com/a/23853079/4558510
What I did was to construct,
mixed +
related +
html
inline image
attachment
attachment
Leaving out alternative with attachments because somehow, in the time of writing, Yahoo online client was not displaying them. Sticking them in mixed worked fine.
Tested and works with,
Apple/IOS Mail (Tablet Ipad 2)
Outlook Windows 7 client
Outlook mobile (Android)
Gmail Web Client
Gmail mobile (Android)
Android mobile Email (Lollipop)
Yahoo web client
Yahoo mobile Email (Android)
Lotus Notes Windows 7 client
Note: Android used is Samsung Note 4 Lollipop.
Code:
BodyPart messageBodyPart = new MimeBodyPart();
String htmlText = message + "<img src=\"cid:image123\">";
messageBodyPart.setContent(htmlText, "text/html; charset=UTF-8");
MimeMultipart mpRelated = new MimeMultipart("relative");
mpRelated.addBodyPart(messageBodyPart);
BodyPart imageBodyPart = new MimeBodyPart();
String file = this.getClass().getClassLoader().getResource("images/Logo.gif").getFile();
DataSource fds = new FileDataSource(file);
imageBodyPart.setFileName("Logo.gif");
imageBodyPart.setHeader("Content-ID","<image123>");
imageBodyPart.setDisposition(Part.INLINE);
mpRelated.addBodyPart(imageBodyPart);
MimeMultipart mpMixed = new MimeMultipart("mixed");
//Nest Related into mixed
BodyPart relatedInMixed = new MimeBodyPart();
relatedInMixed.setContent(mpRelated);
mpMixed.addBodyPart(relatedInMixed);
//TODO Add attachement to mpMixed

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.

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

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

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

Categories

Resources