Mega Code Archive

 
Categories / Delphi / Functions
 

Automate your Email with the SendMailWithAttachments function

Title: Automate your Email with the SendMailWithAttachments function. Question: How to send your Email with attachments. Answer: // uses ComObj,Dialogs function TForm1.SendMailWithAttachments(Email, Subject : string; Body : Widestring ; Filename : string): boolean; var outlook : variant; item : variant; begin try outlook := CreateOLEObject('outlook.application'); try item := outlook.CreateItem(0); item.Subject := Subject; // You can use "Body := Memo1.text". item.Body := Body; // You can add more Attachments by adding the same line. item.Attachments.Add(FileName,1,1,FileName); item.To := email; item.Send; finally // To make sure Outlook don't stay open. outlook.quit; end; except result := false; exit; end; result := true; end; // Here is an example how the function works. procedure TForm1.Button1Click(Sender: TObject); var Opendialog1 : TOpenDialog; begin // Create an OpenDialog to get the Attachment. // Is the Dialogs unit in the uses line? Opendialog1 := TOpendialog.Create(application); try if OpenDialog1.Execute then begin SendMailWithAttachments('Info@Cleys.com', 'Delphi3000 function','Have fun!',opendialog1.FileName); end; finally Opendialog1.Destroy; end; end; // If you have some problems? Let me now and i'll will send some examples // Have fun!!!