delphi 简单的发送邮件

delphi 编程用outlook发送邮件的方法,以下是关键 代码:

// 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([email protected]', 'I Love Eva Zhang','She Is My Wife!',opendialog1.FileName);
  end;
   finally
   Opendialog1.Destroy;
   end;

end;

你可能感兴趣的:(delphi)