利用Delphi编程发送E-mail

2016-01-29 14:02 3 1 收藏

利用Delphi编程发送E-mail,利用Delphi编程发送E-mail

【 tulaoshi.com - Delphi 】

 
接发E-mail是许多“网虫”必修的功课,E-mail工具软件也很多,国外
的有Microsoft的OutlookExpress、TheBat等,国内则有FoxMail这样的精品。
其实,利用可视化编程工具Delphi4.0也能够制作出自己的E-mail软件。
Delphi4.0有关E-mail的组件有两个:NmPOP3和NmSTMP,它们都位于Internet
选项卡上,其中,NmPOP3组件封装并实现POP3协议,用来从InternetPOP3服务
器上读取电子邮件;NmSTMP封装并实现STMP协议,可用来向Internet的STMP服
务器发送电子邮件。限于篇幅,我们不能两者都介绍,这里只用NmSTMP编写一
个发送E-mail的程序,至于NmPOP3,以后有机会再谈,或者在看完本文后你有
兴趣,也可以借助于Delphi的帮助文档尝试用NmPOP3编程。
我们先来看一下程序运行界面。图1是程序主窗体,上面有一个字符串网格
(StringGrid)和三个按钮,它们的容器是仅有一个标签的PageControl。单击
“Clear”钮清除网格内容,单击“Send”钮发送E-mail,单击“NewMail”
钮弹出图2所示的对话框,此对话框用来设置STMP服务器名称、发件人地址、收
件人地址、发件人名称和邮件主题,其中前三项必须填写,“SelectFile”按
钮用来打开Open对话框,以便选取要发送的附件。
NmSTMP的属性和方法不多,关键的属性是Host(STMP服务器名称)和PostMessage
(包含邮件信息),正确设置了Host属性和PostMessage属性后,就可以用Connect
方法(连接服务器)和SendMail方法发送E-mail了。
编写代码之前先要改变StringGrid1的一些缺省属性:ColCount属性为6,
FixedCols属性为0,RowCount属性为2,另外,将PageControl1的Align属性置
为alClient。
以下是Unit1(主窗体)代码清单:
unitUnit1;
interface
uses
Windows,Messages,SysUtils,Classes,Graphics,Controls,Forms,
Dialogs,Grids,ComCtrls,StdCtrls,Psock,NMsmtp;
type
TForm1=class(TForm)
PageControl1:TPageControl;
TabSheet1:TTabSheet;
StatusBar1:TStatusBar;
StringGrid1:TStringGrid;
Button1:TButton;
Button2:TButton;
Button3:TButton;
NMSMTP1:TNMSMTP;
procedureFormCreate(Sender:TObject);
procedureButton1Click(Sender:TObject);
procedureButton2Click(Sender:TObject);
procedureButton3Click(Sender:TObject);
procedureNMSMTP1Failure(Sender:TObject);
procedureNMSMTP1SendStart(Sender:TObject);
procedureNMSMTP1Success(Sender:TObject);
private
{Privatedeclarations}
public
{Publicdeclarations}
end;
var
Form1:TForm1;
implementation
usesUnit2;
{$R*.DFM}
procedureTForm1.FormCreate(Sender:TObject);
begin
PageControl1.Pages[0].Caption:=‘SendMail’;
self.Caption:=‘MyMailer’;
self.BorderIcons:=[biSystemMenu,biMinimize];
self.BorderStyle:=bsSingle;
Application.Icon:=self.Icon;
Application.Title:=self.Caption;
withStringGrid1do
begin
Cells[0,0]:=‘Host’;
Cells[1,0]:=‘ToAddress’;
Cells[2,0]:=‘FromAddress’;
Cells[3,0]:=‘YourName’;
Cells[4,0]:=‘Subject’;
Cells[5,0]:=‘File’;
end;
Button2.Enabled:=False;
Button3.Enabled:=False;
end;
procedureTForm1.Button1Click(Sender:TObject);
begin
Form2.Show;
end;
procedureTForm1.Button2Click(Sender:TObject);
var
i:Integer;
begin
fori:=1toStringGrid1.RowCount-2do
withNmsmtp1do
begin
Host:=StringGrid1.Cells[0,i];
PostMessage.ToAddress.Add(StringGrid1.Cells[1,i]);
PostMessage.FromAddress:=StringGrid1.Cells[2,i];
PostMessage.FromName:=StringGrid1.Cells[3,i];
PostMessage.Subject:=StringGrid1.Cells[4,i];
PostMessage.Attachments.Add(StringGrid1.Cells[5,i]);
Connect;
Sendmail;
DisConnect;
end;
Button2.Enabled:=False;
Button3.Click;
end;
procedureTForm1.Button3Click(Sender:TObject);
var
i:Integer;
begin
withStringGrid1do
begin
fori:=1toRowCount-2do
begin
Cells[0,i]:=‘’;
Cells[1,i]:=‘’;
Cells[2,i]:=‘’;
Cells[3,i]:=‘’;
Cells[4,i]:=‘’;
Cells[5,i]:=‘’;
end;
RowCount:=2;
end;
Button2.Enabled:=False;
Button3.Enabled:=False;
end;
procedureTForm1.NMSMTP1Failure(Sender:TObject);
begin
StatusBar1.SimpleText:=‘Mailsendfailure!’;
end;
procedureTForm1.NMSMTP1SendStart(Sender:TObject);
begin
StatusBar1

来源:https://www.tulaoshi.com/n/20160129/1492329.html

延伸阅读
标签: Delphi
看看下面代码的代码: const WM_CAP_START = WM_USER; const WM_CAP_STOP = WM_CAP_START + 68; const WM_CAP_DRIVER_CONNECT = WM_CAP_START + 10; const WM_CAP_DRIVER_DISCONNECT = WM_CAP_START + 11; const WM_CAP_SAVEDIB = WM_CAP_START + 25; const WM_CAP_GRAB_FRAME = WM_CAP_START + 60; const WM_CAP_SEQUENCE = WM_CAP_...
拖放(DragDrop)是Windows提供的一种快捷的操作方式。作为基于Windows的开发工具,Delphi同样支持拖放操作,而且开发应用系统的拖放功能十分方便,真正体现了Delphi的强大功能和方便性。 Delphi提供的所有控件(Control,即能获得输入焦点的部件)都支持拖放操作,并有相应的拖放属性、拖放事件和拖放方法。下面我们先介绍控件的拖放支持...
标签: Delphi
  拖放 (DragDrop)是 Windows 提供的一种快捷的操作方式。作为基于 Windows 的开发工具, Delphi同样支持拖放操作,而且开发应用系统的拖放功能十分方便,真正体现了 Delphi 的强大功能和方便性。 Delphi提供的所有控件 (Control ,即能获得输入焦点的部件 ) 都支持拖放操作,并有相应的拖放属性、拖放事件和拖放方法。下面我...
Mscomm是微软一个强大的串口通讯的控件,其强大,简单的特点今我们不舍,在delphi中我们也可以使用它。下面这些代码是针对发送和接收为16进制处理的。 var   senddata:array[1..10] of char;   reData:array of Variant;   sendstr:string;   restr:string;   i:longint; begin  ...
◇[DELPHI]网络邻居复制文件 uses shellapi; copyfile(pchar('newfile.txt'),pchar('//computername/direction/targer.txt'),false); ◇[DELPHI]产生鼠标拖动效果 通过MouseMove事件、DragOver事件、EndDrag事件实现,例如在PANEL上的LABEL: var xpanel,ypanel,xlabel,ylabel:integer; PAN...

经验教程

833

收藏

8
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部