程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> Delphi >> 利用Delphi編程發送E-mail

利用Delphi編程發送E-mail

編輯: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
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved