wood burning stoves*
The moose likes I/O and Streams and the fly likes Image to String to Image in a email Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Image to String to Image in a email" Watch "Image to String to Image in a email" New topic
Author

Image to String to Image in a email

Gaurav Kr. Arora
Ranch Hand

Joined: Feb 20, 2011
Posts: 37
Hi,

The query seems complicated to me because of some constraints.
Constraints:
Have to use String message which should contain image. Not aware how can I convert a gif/jpeg/bmp/png image to java string object.
This string message object will be passed to one of my framework methods send(String type, String subject, String message) to send an email. In my email I want that image to come properly and shown to the user. there is no option to pass the image. It needs to be passed as String message.
Internally the send method of the framework will create MimeMessage etc. and send email.
Type of the email will be text/html.

What could be the best possible solution to send a image as a message body when the method send takes only String argument?

Thanks
James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

I assume that behind the scenes you are using the JavaMail API to send the email. In which case you need to add a method to your framework that allows you to send a MimeMultipart attachment. There are examples of how to do this in the JavaMail distribution.

Note - JavaMail Base64 encodes binary data which means it only contains ASCII characters.

Retired horse trader.
 Note: double-underline links may be advertisements automatically added by this site and are probably not endorsed by me.
Gaurav Kr. Arora
Ranch Hand

Joined: Feb 20, 2011
Posts: 37
James Sabre wrote:I assume that behind the scenes you are using the JavaMail API to send the email. In which case you need to add a method to your framework that allows you to send a MimeMultipart attachment. There are examples of how to do this in the JavaMail distribution.

Note - JavaMail Base64 encodes binary data which means it only contains ASCII characters.


I don't want to send the image as an attachment. Instead I want to send it as a message body. How can we achieve this?
James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

Gaurav Kr. Arora wrote:

I don't want to send the image as an attachment. Instead I want to send it as a message body. How can we achieve this?


It is easy enough to Base64 encode the image and add that as the body part BUT it will not then have the correct mime type and without research I don't know how to make it have the correct mime type. Sorry I can't help.

Bye
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4642
    
    5

http://java.sun.com/developer/onlineTraining/JavaMail/contents.html#IncludingImagesWithHTML

edit http://java.sun.com/developer/onlineTraining/JavaMail/exercises/MailHtml/solution/HtmlImageExample.java


luck, db
There are no new questions, but there may be new answers.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Image to String to Image in a email