设为首页 收藏本站
查看: 769|回复: 0

[经验分享] 利用Outlook發送郵件(l轉)

[复制链接]

尚未签到

发表于 2015-9-13 08:10:21 | 显示全部楼层 |阅读模式
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)<>&#8216;' then
begin
Recipient:=Mail.Recipients.Add(Edit_CC.Text);
Recipient.Type:= olCC;
end;
// 下 面 的 代 码 将 Recipient 对 象 的 类 型 从 默 认 的(&#8220;To&#8221;) 更 改 为
&#8220;BCC&#8221;。
if Trim(Edit_BCC.Text)<>&#8216;' 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.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;
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:=&#8216; (' +IntToStr(L) +&#8216;K)' end else
begin Str(L/1024:0:2,S2); S2:=&#8216; (' +S2 +&#8216;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.

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.iyunv.com/thread-112832-1-1.html 上篇帖子: Programming Outlook with C# 下篇帖子: Outlook Express 使用技巧大全之应用篇
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表