February 9, 2009

sending html email with inline images from delphi with indy

hii





unit Unit1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient,
IdMessageClient, IdSMTP, IdMessage;

type
TForm1 = class(TForm)
Button1: TButton;
IdSMTP1: TIdSMTP;
IdMessage1: TIdMessage;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
var
IdAttach: TIdAttachment;
IdText1: TIdText;
IdText2: TIdText;
i:Smallint;
tmp:TIdMessagePart;
begin
IdSMTP1.Host := 'smtp.gmail.com';
IdSMTP1.Port := 25;
IdSMTP1.Username := 'username@gmail.com';
IdSMTP1.Password := 'password';
IdSMTP1.AuthenticationType := atLogin ;


IdMessage1.From.Address := 'from@gmail.com';
IdMessage1.Recipients.EMailAddresses := 'to@gmail.com,muriki_sathishkumar@yahoo.co.in';
IdMessage1.Subject := 'test';
IdMessage1.Sender.Address := 'from@gmail.com';

IdMessage1.ContentType := 'multipart/mixed';
IdMessage1.Body.Add('');
IdMessage1.Body.Add('');
IdMessage1.Body.Add('');
IdMessage1.Body.Add('

Here is the image:


');
IdMessage1.Body.Add('');
IdMessage1.Body.Add('');
IdMessage1.Body.Add('');

IdText1 := TIdText.Create(IdMessage1.MessageParts, IdMessage1.Body);
IdText1.ContentType := 'text/html';
IdText2 := TIdText.Create(IdMessage1.MessageParts);
IdText2.ContentType := 'text/plain';
IdText2.Body.Text := '';

IdAttach := TIdAttachment.Create(IdMessage1.MessageParts,'c:\tms42\tms.jpg');
IdAttach.ContentType := 'image/jpg';
// IdAttach.FileIsTempFile := True;
IdAttach.ContentDisposition := 'inline';
IdAttach.ExtraHeaders.Values['content-id'] := 'tms.jpg';
IdAttach.DisplayName := 'tms.jpg';
try
try
IdSMTP1.Connect;

// i:= idSMTP1.SendCmd('STARTTLS', 220);
// if i>0 then
begin
// IdSMTP1.Authenticate;
IdSMTP1.Send(IdMessage1);
end;
Showmessage('Email Sent');
except
on E:Exception do
ShowMessage('Failed to send Email!'+#13#10+e.message);
end;
finally
if IdSMTP1.Connected then
IdSMTP1.Disconnect;
end;

end;


end.