// 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(xuedelphi@163.com', 'I Love Eva Zhang','She Is My Wife!',opendialog1.FileName);
end;
finally
Opendialog1.Destroy;
end;
end;