how to embed image like a jpg or png into email

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

how to embed image like a jpg or png into email

squeakman
Greetings,

I would like to send an email with an embedded .png file in it. The code is:

   msg := MailMessage new.
   msg from: '[hidden email]'.
   msg to: '[hidden email]'.
   msg subject: 'Test'.
   relMsg := MimeEntity newcontentType:'multipart/alternative';
yourself.

   " add the plain text message"
   partMsg := MimeEntity newTextPlain.
   partMsg contents: 'this is a test message'.
   relMsg addPart: partMsg.

   "add the png file"
   img := MimeEntity new.
   img contentType: (MimeTypeDescriptor guessFromExtension: 'png').
   img contents: self getLogo.  "answers a ByteArray of logo.png"
   relMsg addPart: img.
   msg addPart: relMsg.

I send the msg using aSMTPClient.  The sending mechanism works but I
never get the .png file in the sent email.

Is this the correct way to use MimeEntity to send an embedded .png file?

Thanks,
Frank

_______________________________________________
vwnc mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/vwnc
Reply | Threaded
Open this post in threaded view
|

Re: how to embed image like a jpg or png into email

squeakman
I figured that I have to use something like <img src=cid:img-1/> to
reference my embedded image.

My code now looks like:

        | msg client  partMsg img |

        msg := MailMessage new.
        msg from: '[hidden email]'.
        msg to: '[hidden email]'.
        msg subject: 'Test'.

        partMsg := MimeEntity newTextPlain.
        partMsg contents: 'this is a test message'.
        msg  addPart: partMsg.
        partMsg := MimeEntity newTextHTML.

        "the HTML that references the image (png) file"
        partMsg contents: '<html><body><h2>A title</h2> Some text in here<br/>
<img src=cid:img-1/>  </body></html>'.
        msg addPart: partMsg.

        img := MimeEntity fileName: 'logo.png'.
        img contentId: 'img-1'.
        msg addPart: img.


This successfully downloads the logo.png file but it is shown as an
attachment in hotmail - logo.png is not embedded in the email.

Is this the right approach?

Thanks,
Frank

_______________________________________________
vwnc mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/vwnc
Reply | Threaded
Open this post in threaded view
|

Re: how to embed image like a jpg or png into email

Holger Kleinsorgen
squeakman wrote
I figured that I have to use something like <img src=cid:img-1/> to
reference my embedded image.
You're almost there  ;) You need to combine HTML and image with content type "multipart/related"

HTML + image:

| msg client  img  html |

msg := MailMessage new.
msg contentType: 'multipart/related'.
msg from: '<sender>'.
msg to: '<receiver>'.
msg subject: 'Test'.

html := MimeEntity  newTextHTML.
html contents: '<html><body>

A title

 Some text in here<br/> 
<img src="cid:img-1"/>  </body></html>'.
msg addPart: html.

img := MimeEntity fileName: '<path to image file>'.
img contentId: 'img-1'.
img contentDisposition: 'inline'.
msg addPart: img.

client  := Net.SMTPClient host: '<mailserver>'.
....client setup....
client send: msg.



Plain text and HTML + image as alternatives:


| msg client  img  html plainMsg htmlMsg |

msg := MailMessage new.
msg contentType: 'multipart/alternative'.
msg from: '<sender>'.
msg to: '<receiver>'.
msg subject: 'Test'.

plainMsg := MimeEntity newTextPlain.
plainMsg contents: 'Some text in here'.
msg addPart: plainMsg.

htmlMsg := MimeEntity new.
htmlMsg contentType: 'multipart/related'.
msg addPart: htmlMsg.

html := MimeEntity  newTextHTML.
html contents: '<html><body>

A title

 Some text in here<br/> 
<img src="cid:img1"/>  </body></html>'.
htmlMsg addPart: html.

img := MimeEntity fileName: '<path to image file>'.
img contentId: 'img1'.
htmlMsg addPart: img.

client  := Net.SMTPClient host: '<mailserver>'.
....client setup....
client send: msg.


Reply | Threaded
Open this post in threaded view
|

Re: how to embed image like a jpg or png into email

squeakman
On 18/07/2012 8:38 AM, Holger Kleinsorgen wrote:

> You're almost there  ;) You need to combine HTML and image with content type
> "multipart/related"
>

Thanks for you help, I am a bit closer to a solution.

I am sending the message to hotmail.com but it is not display the image
inline, instead it is an attachment.

Below is the code I am using and the contents of the MimeEntity that I
am sending.

Do you see anything wrong?

Thanks once again for you help,

Frank


---------------------------------

        | msg client  html img |
        msg := MailMessage new.
        msg contentType: 'multipart/related'.
        msg from: <sender>.
        msg to: <receiver>.
        msg subject: 'Test - related'.

        html := MimeEntity newTextHTML.
        html contents: '<html><body><h2>A title</h2>Some text in
here<br/>cid:img1 </body></html>'.
        msg addPart: html.

        img := MimeEntity fileName: 'logo.png'.
        img contentId: 'img1'.
        img contentDisposition: 'inline'.
        msg addPart: img.



---------------------------
Here is the MimeEntity that results from the above code:


From: <sender>
Subject: Test - related
To: <reveiver>
Content-type: multipart/related;boundary="=_vw0.51176011213649d_="

This message is in Mime format

--=_vw0.51176011213649d_=
        Content-type: text/html;charset=iso-8859-1

        <html><body><h2>A title</h2>Some text in here<br/>cid:img1 </body></html>

--=_vw0.51176011213649d_=
        Content-disposition: inline;filename=logo.png
        Content-type: image/png
        Content-id: img1

        ------ from logo.png (5493 bytes) -----

--=_vw0.51176011213649d_=--


_______________________________________________
vwnc mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/vwnc
Reply | Threaded
Open this post in threaded view
|

Re: how to embed image like a jpg or png into email

Holger Kleinsorgen
squeakman wrote
Thanks for you help, I am a bit closer to a solution.

I am sending the message to hotmail.com but it is not display the image
inline, instead it is an attachment.
....
        html contents: '<html><body>

A title

Some text in
here<br/>cid:img1 </body></html>'.
I don't know if it's just Nabbles web interface for this mailing list, but the HTML contents lacks a proper image tag.

I've tested the code with Outlook 2010, Thunderbird 14, Google Mail and Live/Hotmail. All can display the HTML with the inlined image. Hotmail asks if the sender should be trusted, but after that it works too.