Board index » delphi » Indy9, embedding images in html emails

Indy9, embedding images in html emails


2004-08-11 05:24:16 AM
delphi124
Hi all
This has probably been asked and answered a million times already :-)
I just want to send a simple html email with an <img dynasrc="blah">in it.
Could someone please explain to me how this is achieved?
Thanks
--
Pete
====
Audio compression components, DIB graphics controls, FastStrings
www.droopyeyes.com
Read or write articles on just about anything
www.HowToDoThings.com
 
 

Re:Indy9, embedding images in html emails

No problem, I found this....
procedure TForm1.Button1Click(Sender: TObject);
var
html: TStrings;
htmpart, txtpart: TIdText;
bmppart: TIdAttachment;
email: TIdMessage;
filename: string;
begin
filename := ExtractFilePath(Application.ExeName) + 'us.jpg';
html := TStringList.Create();
html.Add('<html>');
html.Add('<head>');
html.Add('</head>');
html.Add('<body><h1>Hello</h1>');
html.Add('<img src="cid:us.jpg" />');
html.Add('This is a picture of us!</body>');
html.Add('</html>');
email := TIdMessage.Create(nil);
email.From.Text := 'XXXX@XXXXX.COM';
email.Recipients.EMailAddresses := 'XXXX@XXXXX.COM';
email.Subject := 'Hello';
email.ContentType := 'multipart/mixed';
email.Body.Assign(html);
txtpart := TIdText.Create(email.MessageParts);
txtpart.ContentType := 'text/plain';
txtpart.Body.Text := '';
htmpart := TIdText.Create(email.MessageParts, html);
htmpart.ContentType := 'text/html';
bmppart := TIdAttachment.Create(email.MessageParts, filename);
bmppart.ContentType := 'image/jpeg';
bmppart.FileIsTempFile := true;
bmppart.ContentDisposition := 'inline';
bmppart.ExtraHeaders.Values['content-id'] := 'us.jpg';
bmppart.DisplayName := 'us.jpg';
try
idSMTP.Connect();
try
idSMTP.Send(email);
ShowMessage('Sent');
except
on E: Exception do
ShowMessage('Failed: ' + E.Message);
end;
finally
idSMTP.Disconnect();
email.Free();
html.Free();
end;
end;
--
Pete
====
Audio compression components, DIB graphics controls, FastStrings
www.droopyeyes.com
Read or write articles on just about anything
www.HowToDoThings.com
 

Re:Indy9, embedding images in html emails

Quote
I just want to send a simple html email with an <img dynasrc="blah">in it.
Could someone please explain to me how this is achieved?
What is the dynasrc attribute? it is not HTML.
 

Re:Indy9, embedding images in html emails

"Peter Morris" <XXXX@XXXXX.COM>writes
Quote
I just want to send a simple html email with an <img dynasrc="blah">in
it.
Could someone please explain to me how this is achieved?
To embed an image in an HTML e-mail, you need to attach the image to the
message and assign its ContentID property, and then refer to that ID in the
HTML using the "cid" syntax in the <img>tag's "Src" attribute, ie:
with TIdAttachment.Create(IdMessage1.MessageParts, 'c:\somefile.gif') do
begin
ContentType := 'image/gif';
ContentID := 'somefile.gif'; // anything you want
end;
with TIdText.Create(IdMessage1.MessageParts, nil) do
begin
ContentType := 'text/plain';
Body.Text := 'plain text here';
end;
with TIdText.Create(IdMessage1.MessageParts, nil) do
begin
ContentType := 'text/html';
Body.Text := '<html><body><img
src="cid:somefile.gif"></body</html>';
end;
Gambit