十年

  :: 首页 :: 新随笔 :: 联系 :: 订阅 :: 管理 ::
Outlook 2000 不 仅 是 一 个 功 能 强 大 的 电 子 邮 件 软 件, 而 且 还 是 一 个 自 动 化 服 务 器(Automation servers)。 第 三 方 的 软 件 开 发 人 员 根 据 需 要 可 以 对Outlook 进 行 定 制 和 扩 充, 同 时 开 发 者 还 可 以 通 过 编 写 Outlook 自 动 化 服 务 器 的 客 户 方 程 序 来 有 效 利 用 它 的 电 子 邮 件 功 能。 本 文 将 介 绍 在Delphi 程 序 中 如 何 利 用Outlook 自 动 化 服 务 器 的 强 大 的 电 子 邮 件 功 能, 调 用Outlook 来 发 送email。

一、 步 骤

----1. 创 建Application 应 用 程 序 对 象

----为 了 使 用Outlook 的 服 务, 首 先 我 们 需 要 创 建 一 个Outlook 的Application 自 动 化 对 象。

----使 用CreateOleObject 函 数 我 们 可 以 创 建 一 个 自 动 化 对 象, 此 函 数 的 原 型 为:

----function CreateOleObject(const ClassName: string): IDispatch;

----使 用CreateOleObject 函 数 创 建Outlook 应 用 程 序 实 例 的 代 码 如 下:

var
  Outlook: Variant;
...
  Outlook:= CreateOleObject (‘Outlook.Application');

----2. 创 建MailItem 对 象

----在Outlook 中, 一 个MailItem 代 表 一 个 邮 件 对 象。Outlook 的CreateItem 方 法 可 以 创 建 一 个MailItem 对 象:

Var
  Outlook: Variant;
  Mail: Variant;
...
  Outlook := CreateOLEObject (‘Outlook.Application');
  // 创 建 一 个MailItem 对 象
  Mail:=Outlook.CreateItem(olMailItem);

----MailItem 对 象 之 下 还 包 含 有 子 对 象 以 及 方 法, 其 中 比 较 重 要 的 有 如 下 一 些:

----(1) Recipients 属 性

----用 于 指 定 邮 件 的 接 收 人, 我 们 可 以 指 定3 类 收 信 人,To、CC( 抄 送) 以 及BCC( 暗 送)。

Var
  Recipient: Variant;
...
  Outlook := CreateOLEObject (‘Outlook.Application');
  // 创 建 一 个MailItem 对 象
  Mail:=Outlook.CreateItem(olMailItem);
  // 添 加 类 型 为To 的 收 件 人
  Recipient:=Mail.Recipients.Add(‘daisy@mhliu.dhs.org');
  Recipient.Type:=olTo;
  // 添 加 类 型 为CC( 抄 送) 的 收 件 人
  Recipient:=Mail.Recipients.Add(‘simon@xyz.com');
  Recipient.Type:=olCc;
  // 添 加 类 型 为BCC( 暗 送) 的 收 件 人
  Recipient:=Mail.Recipients.Add(‘mailback@mhliu.dhs.org');
  Recipient.Type:=olCc;
    2Subject 属 性: 邮 件 的 标 题。
    Body 属 性: 邮 件 的 正 文 内 容。
    Attachments 属 性: 邮 件 的 全 部 附 件。
    增 加 附 件:
    Mail.Attachments.Add(‘D:\Simon\Unit1.pas');
    删 除 附 件:
      if Mail.Attachments.Count>0
      then Mail.Attachments.Remove(1);
    { 如 果 附 件 的 数 目 不 为0, 则 去 掉 第1 个 附 件}
    Send 方 法: 发 送 邮 件。

二、 示 例 程 序

----下 面 是 一 个Outlook 自 动 化 服 务 器 的 客 户 程 序 的 代 码, 你 可 以 参 考vbaoutl9.chm 文 档 并 对 此 程 序 进 行 改 进 和 扩 充。

unit Unit1;
interface
uses  Windows, Messages, SysUtils, Classes, Graphics,
  Controls, Forms, Dialogs, StdCtrls, ExtCtrls,
  ShellAPI, ComObj, ActiveX, ComCtrls;
type
  TForm1 = class(TForm)
    Memo1: TMemo;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Edit_To: TEdit;
    Edit_CC: TEdit;
    Edit_BCC: TEdit;
    Edit_Subject: TEdit;
    ListView1: TListView;
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    OpenDialog1: TOpenDialog;
    procedure FormResize(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
  end;
const
{Microsoft Outlook 常 量}
//OlItemType 常 量( 用 于CreateItem 方 法)
 olMailItem=0;
// 邮 件 接 收 者 类 型( 默 认 类 型 为“To”)
  olTo=1;
  olCC=2;
  olBCC=3;
var
  Form1: TForm1;
  ImageList:TImageList;
  TheIcon:TIcon;
implementation
{ $R *.DFM}
procedure TForm1.FormCreate(Sender: TObject);
begin
  TheIcon:=TIcon.Create;
  ImageList:=TImageList.CreateSize(32,32);
  ListView1.LargeImages:=ImageList;
  ListView1.Visible:=False;
end;
// 创 建Outlook Automation 对 象, 并 发 送 邮 件
procedure TForm1.Button1Click(Sender: TObject);
var
   i:integer;
   Outlook: Variant;
   Mail: Variant;
   Recipient:Variant;
begin
  // 收 件 人 是 否 为 空
  if Trim(Edit_To.Text)=‘' then exit;
  // 创 建Outlook.Application 自 动 化 对 象
  Outlook := CreateOLEObject (‘Outlook.Application');
  if VarIsEmpty(Outlook) then
  begin
    MessageBox(handle,‘Outlook 自 动 化 对 象 创 建 失 败 !',nil,0);
    Exit;
  end;
  // 创 建 一 个MailItem 对 象
  Mail:=Outlook.CreateItem(olMailItem);
  //  Mail.Display;
  // 填 写 收 件 人
  Mail.Recipients.Add(Trim(Edit_To.Text));
  // 下 面 的 代 码 将 Recipient 对 象 的 类 型 从 默 认 的(“To”) 更 改 为
“CC”。
  if Trim(Edit_CC.Text)<>‘' then
  begin
    Recipient:=Mail.Recipients.Add(Edit_CC.Text);
    Recipient.Type:= olCC;
  end;
  // 下 面 的 代 码 将 Recipient 对 象 的 类 型 从 默 认 的(“To”) 更 改 为
“BCC”。
  if Trim(Edit_BCC.Text)<>‘' then
  begin
    Recipient:=Mail.Recipients.Add(Edit_BCC.Text);
    Recipient.Type:=olBCC;
  end;
  // 填 写 邮 件 主 题
  Mail.Subject:= Edit_Subject.Text;
  // 填 写 邮 件 正 文
  Mail.Body:=Memo1.Text;
  // 增 添 附 件
  for i:=0 to ListView1.Items.Count -1 do
  Mail.Attachments.Add(ListView1.Items[i].SubItems[0]);
  // 发 送 邮 件
  Mail.Send;
  // 释 放Microsoft Outlook 自 动 化 对 象
  Outlook:=Unassigned;
end;
procedure TForm1.FormResize(Sender: TObject);
begin
  Edit_TO.Width:=ClientWidth -Edit_TO.Left -2;
  Edit_CC.Width:=ClientWidth -Edit_CC.Left -2;
  Edit_BCC.Width:=ClientWidth -Edit_BCC.Left -2;
  Edit_Subject.Width:=ClientWidth -Edit_Subject.Left -2;

  Memo1.Top:=Edit_Subject.Top +Edit_Subject.Height +2;
  Memo1.Width:=ClientWidth;
  Button1.Top:=ClientHeight -4 -Button1.Height;
  Button1.Left:=ClientWidth -Button1.Width -6;
  Button2.Left:=2;
  Button3.Left:=Button2.Left +Button2.Width +2;
  Button2.Top:=Button1.Top;
  Button3.Top:=Button1.Top;
  ListView1.Width:=ClientWidth;
  ListView1.Top:=Button1.Top -4 -ListView1.Height;
  if ListView1.Items.Count>0 then
    Memo1.Height:=Listview1.Top -4 -Memo1.Top
else
    Memo1.Height:=Button1.Top -4 -Memo1.Top;
end;
// 增 添 附 件
procedure TForm1.Button2Click(Sender: TObject);
var
  i:integer; w:Word; L,h:LongWord;
  S,S2:String;
  NewItem:TListItem;
begin
  w:=0;
  if not OpenDialog1.Execute then exit;
  for i:=0 to OpenDialog1.Files.Count -1 do
  begin
    S:=OpenDialog1.Files.Strings[i];
    h:=CreateFile(PChar(S),GENERIC_READ,
FILE_SHARE_READ,nil,OPEN_EXISTING,0,0);
    L:=GetFileSize(h,nil);
    L:=(L +1023)div 1024;
    if L<1000 then
       begin S2:=‘ (' +IntToStr(L) +‘K)' end else
       begin Str(L/1024:0:2,S2); S2:=‘ (' +S2 +‘M)'; end;
    TheIcon.Handle:=ExtractAssociatedIcon(hInstance,PChar(S),w);
    NewItem:=ListView1.Items.Add;
    NewItem.ImageIndex:=ImageList.AddIcon(TheIcon);
    NewItem.Caption:=ExtractFileName(S) +S2;
    NewItem.SubItems.Add(S);// 保 存 文 件 的 完 全 路 径 名
    end;
if ListView1.Items.Count>0 then ListView1.Visible:=True else ListView1.Visible:=False;
FormResize(self);
end;
// 删 除 附 件
procedure TForm1.Button3Click(Sender: TObject);
var
   item:TListItem;
begin
   item:=ListView1.Selected;
   ListView1.Items.BeginUpdate;
   while item<>nil do
   begin
     ListView1.Items.Delete(Item.Index);
     Item:=ListView1.Selected;
   end;
   ListView1.Items.EndUpdate;
   if ListView1.Items.Count<=0 then ListView1. 
Visible:=False;
   FormResize(self);
end;
end.
posted on 2005-01-25 08:46  留不住的时光  阅读(3044)  评论(0)    收藏  举报