4 Replies Latest reply: Feb 6, 2013 3:11 AM by Deepika R RSS

    Want to send image as part of the message body in apex mail

    Deepika R
      Dear All,

      I have an important requirement to include image in the massage body itself along with text message and send email to customer.

      But I am unable to send image successfully in the mail. Only a box appears in its place, and the actual image does not come.

      Here is the code I have tried:

      DECLARE

      l_id NUMBER;

      l_body CLOB;

      l_body_html CLOB;

      x blob;

      BEGIN



      select blob_content into x from apex_application_files where name=:p17_attachment;



      l_id := APEX_MAIL.SEND(

      p_to => :p17_to,

      p_from => :p17_from,

      p_subj => 'APEX_MAIL with attachment',

      p_body => 'Please review the attachment.',

      p_body_html => :p17_body ||

      '<img src="x" alt="x" height="42" width="42">' );

      FOR c1 IN (SELECT filename, blob_content, mime_type

      FROM APEX_APPLICATION_FILES

      WHERE name=:p17_attachment) LOOP



      APEX_MAIL.ADD_ATTACHMENT(

      p_mail_id => l_id,

      p_attachment => c1.blob_content,

      p_filename => c1.filename,

      p_mime_type => c1.mime_type);

      END LOOP;

      COMMIT;

      APEX_APPLICATION.G_PRINT_SUCCESS_MESSAGE := 'Mail was successfully sent';

      APEX_MAIL.PUSH_QUEUE;

      END;

      Please let me know how to solve this problem.


      Thanking in advance.

      Regards,
      Deepika.
        • 1. Re: Want to send image as part of the message body in apex mail
          Carsten Czarski-Oracle
          Hi Deepika,

          I think what you want to achieve is to embed the image directly with the HTML Mail, so that the recipient's Mailclient will not have
          to load it remotely. That is not an easy task. To really embed an image into an HTML mail you have to ...

          * Add the image as attachment and give the attachment a name
          * Refer to this attachment name in the <img> tag in the HTML code

          So you have to process the HTML code of the email. I once authored a Java Stored Procedure doing this ... While adding the attachments I used the following Java calls to make them available to the HTML email body ...

          bpAttachment= new MimeBodyPart();
          bpAttachment.setHeader("Content-ID","<imagename}>");
          bpAttachment.setDisposition(Part.INLINE);

          In the HTML body the image is being referenced as <img src="cid:{imagename}"/> The email client will then take the image from the attachments and display it. No remote loading will occur.

          I'm not sure whether this is possible with APEX_MAIL; but I don't think so. You might play a bit with the filename in the ADD_ATTACHMENT call and "cid:{filename}" within the <img> tag. If APEX_MAIL does not work, a java stored procedure or UTL_SMTP might be an option.

          Best regards

          -Carsten
          • 2. Re: Want to send image as part of the message body in apex mail
            Deepika R
            Hi Carsten,

            Thanks a lot for the reply. Will try the options mentioned and get back.

            Regards,
            Deepika.
            • 3. Re: Want to send image as part of the message body in apex mail
              Carsten Czarski-Oracle
              Hi Deepika,

              I have played a bit for myself - and I can say now that this will not be possible with APEX_MAIL. The reason is that
              APEX mail does not set the "Content-ID" Header for an attachment - which is a requirement for embedding images.

              So you could fo for either UTL_SMTP (the PL/SQL way) or for a Java Stored Procedure (the java way using javax.mail).

              Regards

              -Carsten
              • 4. Re: Want to send image as part of the message body in apex mail
                Deepika R
                Thanks again Carsten. I will try the UTL_SMTP option.

                Best Regards,
                Deepika.