您的位置:首页 > 编程语言 > Delphi

delphi 简单的发送邮件

2013-07-16 23:24 387 查看
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(xuedelphi@163.com', 'I Love Eva Zhang','She Is My Wife!',opendialog1.FileName);

end;

finally

Opendialog1.Destroy;

end;

end;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: