S0t4's Blog

Hanya Catatan dan Mencoba Untuk Berbagi

Saturday, March 20, 2010

Kirim email HTML Pakai Delphi dan Komponent Indy, dengan Gambar yang di Embedded di Body

  Blogger Sejati       Saturday, March 20, 2010
contoh ini untuk mengirim gambar yang di embeded di body email pakai pemorograman delphi dan indy component.

uses
idMessage;

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 := ‘pengirim@email’;
email.Recipients.EMailAddresses := ‘penerima@email’;
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;
logoblog

Thanks for reading Kirim email HTML Pakai Delphi dan Komponent Indy, dengan Gambar yang di Embedded di Body

Previous
« Prev Post

1 comment:

imamnet said...

Bos, source code + aplikasinya kok gak sekalian di upload, biar sekalian bisa dipelajari..
thanks