Retrieve images from DB and attach them to email through javamail

Hi,
I'm trying to use javamail to send emails containing text and images (HTML mail), if I use a physical file in my computer as the image source and use FileDataSource everything works just fine. The problem is that the images in my app are stored into a DB (I'm using MySQL, java struts & DAO), so in order to retrieve & display them I use a jsp that returns a byte array as shown in the example bellow
<img  src='getImage.jsp?itemId=<bean:write name="item" property="itemId"/>&heigth=180&width=140&imageIndex=1'/>getImage.jsp reads the image from the DB for the specified item and returns an image of size "heigth*witdh", this works fine when invoked from any jsp or java action class, but I can't get it to work to insert an image into an email as attachment
I've tried to use ByteArrayDataSource and the mail is sent correctly and can be read but the image won't show
bds = new ByteArrayDataSource("/getImage.jsp?itemId="+itemId+"&heigth=180&width=140&imageIndex=1", "image/jpeg");
imageMime.setDataHandler(new DataHandler(bds));
imageMime.setHeader("Content-ID", "<\" + itemId + \">"); // backslashes aren't there in the real code, but if I remove them '+ itemId +' won't show
mailParts.addBodyPart(imageMime);I've also tried with URLDataSource but can't get it to work either, can somebody plz point me in the right direction?
thanks in advance
Edited by: informacionCubica on Jun 8, 2009 3:46 PM

Hello bshannon,
Thanks for your answer, I tried your suggestion but when I use URLDataSource I get the error message http 500 (in my original post I said that using URLDataSource the mail was sent but without the image... my mistake, I was getting this same error). I traced the error and I found that it is caused by a null pointer exception... let me explain
This is the code for the jsp that returns the image:
4        <jsp:useBean id="coverImage" class="com.mqm.struts.getImageAction" scope="session" />
5       <%
6           // Desired size of the image to return
7           int heigth = Integer.parseInt(request.getParameter("heigth"));
8          int width = Integer.parseInt(request.getParameter("width"));
9          
10          // Each item can have up to 6 images (ordered by id), this indicates wich of one we have to return
11            int imageIndex = Integer.parseInt(request.getParameter("imageIndex"));
12        byte[] imgData = coverImage.getItemImageAction(request.getParameter("itemId"), request,heigth, width, imageIndex );
13        response.setContentType("image/jpeg");
14        OutputStream o = response.getOutputStream();
15         o.write(imgData);
16         o.flush();
17         out.clearBuffer();
18         o.close();
        %> getImageAction is my DAO class that actually reads the image data from the DB, when I use this 'getImage.jsp' inside any other jsp in the app it works, but when used as described in the first post the request parameter in the line 12 doesn't have the session info, apparently creates a new session or something and since I use information in the session to create the DAO factory I can't connect to the DB and get this error..
pls tell me if I explained my self or I'm just talking non sense
regards

Similar Messages

  • Taking images from Photoshop and taking them to Dreamweaver

    Why is it that when i take my images from photoshop and save them for the web, that when i open them in my dreamweaver html file they have lost quality and saturation in the image?

    There are 2 ways to do it correctly one is better then the other. The best way to bring an image into
    DW is to first create a layer based sliced and the copy & paste it direc
    tly into DW. When you do that you will get a Image optimize window, this is a built-in Firework image
    optimizer it does a much better job a maintaing quality will compressing image size. The second optioin is to use the save to web feature. Tehy work the same DW/FW does it better. If your image quality still is not what you want play around with your optimization settings without seeein the image I can't recommend the best setting but play around in your 2up/4up window.

  • Problem in Retrieve Image from DB and display in the JSP page

    Hi All,
    I did one JSP Program for retriveing image from DB and display in the JSP Page. But when i run this i m getting "String Value" output. Here i have given my Program and the output. Please any one help to this issue.
    Database Used : MS Access
    DSN Name : image
    Table Name: image
    Image Format: bmp
    Output : 1973956
    Sample Program:_
    <%@ page contentType="text/html;charset=windows-1252"%>
    <%@ page import="java.io.*" %>
    <%
         try{
              Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
              Connection conn = DriverManager.getConnection("jdbc:odbc:image");
              Statement st = conn.createStatement();
              ResultSet rs = st.executeQuery("SELECT images FROM image");
              String imgLen="";
              if(rs.next()){
                   imgLen = rs.getString(1);
                   out.println(imgLen.length());
              if(rs.next()){
                   int len = imgLen.length();
                   byte [] rb = new byte[len];
                   InputStream readImg = rs.getBinaryStream(1);
                   int index=readImg.read(rb, 0, len);
                   System.out.println("index"+index);
                   st.close();
                   response.reset();
                   response.setContentType("image/jpg");
                   response.getOutputStream().write(rb,0,len);
                   response.getOutputStream().flush();
         }catch(Exception ee){
              out.println(ee);
    %>
    Thanks,
    Senthilkumar S

    vishruta wrote:
    <%
    %>Using scriptlets is asking for trouble. Java code belongs in Java classes. Use a Servlet.
                   out.println(imgLen.length());Your JSP was supposed to write an image to the output and you wrote some irrelevant strings to the output before? This will corrupt the image. It's like opening the image in a text editor and adding some characters before to it.
                   byte [] rb = new byte[len];Memory hogging. Don't do that.
              out.println(ee);You should be throwing exceptions and at least printing its trace, not sending its toString() to the output.
    You may find this article useful to get an idea how this kind of stuff ought to work: [http://balusc.blogspot.com/2007/04/imageservlet.html].

  • I recently upgraded to a new iPhone.  Everything transferred except my photos.  Can I retrieve my photos from iCloud and transfer them to my new phone?  My old phone has already been transferred to a new user.

    I recently upgraded to a new iphone.  Everything transferred except my photos.  Can I retrieve my photos from iCloud and transfer them to my new phone?  My old phone has already been transferred to a new user.

    Did you restore your iCloud backup?  That should recover camera roll photos.

  • Retrieve image from my sql database using jsp

    I want to retrieve image from my sql (blob type) & save it in given relative path in server .(using jsp and servlets)
    please give me some sample codes.

    PreparedStatement pst = null;
      ResultSet rs=null;
    pst = conn.prepareStatement("select image from imagedetails where imageid='"+imageid+"'");
    rs=pst.executeQuery();
    while(rs.next())
                                byte[] b=rs.getBytes("image");
                                FileOutputStream fos=new FileOutputStream("c://image.jpg");
                                fos.write(b);
                            } hi this the code to retrieve the image from DB and write to a file.

  • Question on retrieve image from mysql

    i want to retrieve image from mysql database and display on GUI
    can anyone tell me how to do
    Thank you

    You can use the JDBC API for Java Database Connectivity. With this you can obtain data from the database using Java. There is an excellent tutorial here at Sun.com: http://www.google.com/search?q=jdbc+tutorial+site:sun.com You can get the MySQL JDBC driver at their own site: http://www.google.com/search?q=download+jdbc+driver+site:mysql.com Get the most recent version which suits your environment. There is also good documentation available over there.
    For displaying in the UI, the approach differs per UI. As you didn't mention which UI you are using, I can't help you further in detail.

  • Retrieving music from icloud and keeping it

    I upgraded my phone and subsequently all my music went to the cloud. Can anyone tell me how to retrieve this for itunes and my iphone and stop it disappearing again. thanks

    If you want to delete songs from iTunes and keep them on the iPod you need to change the update option of your iPod to "manually manage music and videos". The content of iTunes and the iPod are not syncronised in this mode so the two can be different, you can get details here:
    Managing content manually on iPod
    iPod - Syncing Music
    One major word of caution. If you delete songs or videos from your computer and have no back up other than the iPod you will risk losing them permanently if your iPod fails or you find you have to restore it to solve a problem. You should consider investing in an additional internal or external hard drive to store/back up your music. Alternatively at the very least back up anything you can't replace such as iTunes downloads to CD or DVD: How to back up your media in iTunes
    Something else to be aware of when using an iPod in manual mode is that the "Do Not Disconnect" message will remain on the display until you physically eject the device. In that case use Safely Remove Hardware icon in the Windows system tray on your desktop or check this link: Safely Disconnect IPod

  • Good morning, i would like to know how to take pictures from iphoto and put them in a folder on desktop

    Good morning: would like to know how to take pictures from my iphoto and put them in a folder on my desktop

    To take photos from iPhoto and put them onto a folder on Desktop, first create a folder on Desktop. Right-click on Desktop and create a new folder.
    Then, open iPhoto, and you have two ways to export photos:
    1. Select the photos you want to copy to Desktop with Command key (or go to Edit menu > Select All, if you want to copy all photos), and then, drag them to the folder on Desktop.
    2. Select the photos you want to copy to Desktop with Command key, go to File menu > Export, and follow the steps.
    You can choose the one you prefer

  • Export scenarios from XI and import them into PI

    Dear experts
    We are planning to upgrade our exitsting XI 3.0 to PI7.0.
    Due to system requeriments, we need to install PI on new hardware and migrate all the existing scenario's from XI 3.0 PI7.'0
    How to migrate the existing ABAP developements from XI to PI  ?
    It's possible to export scenarios from XI and import them into PI?
    Thanks, in advance
    Carme

    Hi,
    you should go to PI 7.1 and not PI 7.0 otherwise it makes not so much sense.
    And yes it's possible to migrate your scenarios. If you have UDFs you have to check them because the java version has changed.
    Regards
    Patrick

  • How can you pull in pdf's from iBooks and bring them into the Box app?

    How can you pull in pdf's from iBooks and bring them into the Box app?

    That sounds great, but I'm lost.
    I followed these steps:
    1) Open your PDF (in Adobe Acrobat Pro)
    2) Chose print
    The print/printer options pop-up will show.
    3) In the printer section, do not use your normal printer, a printer named Adobe PDF should be an option. Chose it.
    4) Under the "Page handling" section, change the Page Scaling drop-down menu to "Multiple pages per sheet".
    It works fine, but it looses all of the font information and the search no longer works. How can you do it and save the font info, so the search tool continues to operate.
    Am I missing anything?

  • How do I copy image from web and paste into Open Office Document

    Whenever I choose Copy Image from Safari and then paste into open office, I only get the image url and not the image. I am sure that I am not selecting Copy Image Address.
    When I copy images from my iTunes album artwork and paste into open office it works fine.
    Any thoughts?

    I have done the same thing using Firefox and it works just fine. It seems to be an issue with Safari.

  • How do I get an image from Picasa and transfer it to photoshop to work on it.?

    how do I obtain an image from picasa and bring it to ps to work on it?

    Are you using a Windows system, and do you have File Explore set to show extensions?  I am wondering if the file name might look like:
    'Starbirds Picture.jpg.tiff'  ?
    The fact that you say the image looks different after reducing its bit depth makes me think you are right about it being 32 bit, but as Chris said, it can't be a JPG.
    Your computer monitor is not capable of displaying the full tonal range of a 32bit image produced by an HDR program like Photomatix or Photoshop, so it has to make compromises.  You have to chose what process to use to reduce its bit depth to give it the look you are after.  I prefer Photomatix for HDR.  If you install a trial version and open your 32 bit image in it, you can then chose one of the options to convert it to 16 or 8 bit.  These might be Tone Mapping, Exposure Fusion, or Tone Compression.   Tone Mapping is the option that produces the typical HDR look.
    But you are using Photoshop.  I haven't noticed what version you are using, but Google Photoshop (your version) HDR processing, and you'll see how to control the end look of the image.

  • How to pull data from EJB and present them using Swing ?

    Hi all,
    I've written stateful session bean which connect to Oracle database, and now I must write stand alone client application using Swing.
    The client app must present the data and then let users add,delete and edit data and it must be flexible enough to iterate through the records.
    The swing components can be JTextField,JTable etc.
    How to pull the data from EJB and present them to users with the most efficient network trip ?
    Thanks in advance
    Setya

    Thanks,
    Since the whole app originally was client-server app and I want to make it more scalable, so I decide to separate business logic in the EJB but I also want to keep the performance and the userfriendliness of the original user interface, and I want to continue using Swing as the original user interface does.
    I've read about using Rowset and I need some opinions about this from you guys who already have some experience with it.
    Any suggestions would be greatly appreciated.
    Thanks
    Setya

  • I somehow changed a video in my imovie to zero fps, so it just plays black.  The video still exists on my desktop but even when I delete the files from imovie and reimport them the file is still in zero fps and play black.

    somehow changed a video in my imovie to zero fps, so it just plays black.  The video still exists on my desktop but even when I delete the files from imovie and reimport them the file is still in zero fps and play black.

    I'm not sure of your question.
    Are you asking how to use Photoshop?
    Or are you asking for the dimensions of video iMovie uses?
    It edits in 720P HD, which is 1280x720.
    You can make it a bit bigger so you can zoom in.
    Much bigger then this is a waste of time.

  • HT2470 how would i go about taking multiple files from documents and make them part of 1 master file

    how would i go about taking multiple files from documents and make them into 1 master file? I want to put all documents associated with this project together for easier access.

    From the finder window, click File, click new folder, you can drag all documents into that Folder making it the master folder. You can ajso drag folders into the Master folder, and have them as sub-folders if you wish to  do that. Either way they will all be in one master folder.

Maybe you are looking for