DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Delphi: Send An Email With An Attachment Using OLE

09.10.2008
| 10480 views |
  • submit to reddit
        In Delphi, this allows you to launch a new outlook window with an attachment. This will not work with Outlook Express.

This code was modified from the version seen <a href="http://delphi.about.com/cs/adptips2000/a/bltip0800_3.htm">here</a>
More info <a href="http://www.delphi3000.com/articles/article_1424.asp?SK=">here</a>

uses ComObj //Delphi 5 or later
procedure TfrmHUD1_1.EmailDoc( path : String );
const
	olMailItem = 0;
var
	Outlook: OLEVariant;
	MailItem: Variant;
begin
  try
	Outlook:=GetActiveOleObject('Outlook.Application') ;
  except
	Outlook:=CreateOleObject('Outlook.Application') ;
  end;
	MailItem := Outlook.CreateItem(olMailItem) ;
	MailItem.Recipients.Add('johndoe@hotmail.com') ;
	MailItem.Subject := 'Subject: Outlook Mail From Delphi';
	MailItem.Body := 'Body of the email';
	MailItem.Attachments.Add(path) ;
	MailItem.Display; //MailItem.Send to skip the window and just send the email
	VarClear(Outlook);
end;