接發E-mail |
|
領航天使
站長 發表:12216 回覆:4186 積分:4084 註冊:2001-07-25 發送簡訊給我 |
接發E-mail是許多“網虫”必修的功課,E-mail工具軟件也很多,國外有Microsoft的Outlook Express 、The Bat等,國內則有FoxMail這樣的精品。實,利用可視化編程工具Delphi4.0也能夠制作出自己的E- mail軟件。 Delphi4.0有關E-mail的組件有兩個:NmPOP3和NmSTMP,它們都位于Internet 選項卡上,其中,NmPOP3 組件封裝并實現POP3協議,用來從Internet POP3服務器上讀取電子郵件﹔NmSTMP封裝并實現STMP協議, 可用來向Internet的STMP服務器發送電子郵件。限于篇幅,我們不能兩者都介紹,這里只用NmSTMP編寫一 個發送E-mail的程序,至于NmPOP3,以后有機會再談,或者在看完本文后你有興趣,也可以借助于Delphi 的幫助文檔嘗試用NmPOP3編程。 我們先來看一下程序運行界面。圖1是程序主窗體,上面有一個字符串網格(StringGrid)和三個按鈕,它 們的容器是僅有一個標簽的PageControl。單擊“Clear”鈕清除網格內容,單擊“Send”鈕發送E-mail ,單擊“New Mail” 鈕彈出圖2所示的對話框,此對話框用來設置STMP服務器名稱、發件人地址、收件人地址、發件人名稱和郵 件主題,其中前三項必須填寫,“Select File”按鈕用來打開Open對話框,以便選取要發送的附件。 NmSTMP的屬性和方法不多,關鍵的屬性是Host(STMP服務器名稱)和PostMessage (包含郵件信息),正 確設置了Host屬性和PostMessage屬性后,就可以用Connect 方法(連接服務器)和SendMail方法發送E- mail了。 編寫代碼之前先要改變StringGrid1的一些缺省屬性:ColCount屬性為6, FixedCols屬性為0,RowCount 屬性為2,另外,將PageControl1的Align屬性置為alClient。 以下是Unit1(主窗體)代碼清單: unit Unit1; 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; procedure FormCreate(Sender: TObject); procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); procedure Button3Click(Sender: TObject); procedure NMSMTP1Failure(Sender: TObject); procedure NMSMTP1SendStart(Sender: TObject); procedure NMSMTP1Success(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation uses Unit2; {$R *.DFM} procedure TForm1.FormCreate(Sender: TObject); begin PageControl1.Pages[0].Caption:=‘Send Mail’; self.Caption:=‘My Mailer’; self.BorderIcons:=[biSystemMenu,biMinimize]; self.BorderStyle:=bsSingle; Application.Icon:=self.Icon; Application.Title:=self.Caption; with StringGrid1 do begin Cells[0,0]:=‘Host’; Cells[1,0]:=‘To Address’; Cells[2,0]:=‘From Address’; Cells[3,0]:=‘Your Name’; Cells[4,0]:=‘Subject’; Cells[5,0]:=‘File’; end; Button2.Enabled:=False; Button3.Enabled:=False; end; procedure TForm1.Button1Click(Sender: TObject); begin Form2.Show; end; procedure TForm1.Button2Click(Sender: TObject); var i:Integer; begin for i:=1 to StringGrid1.RowCount-2 do with Nmsmtp1 do 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; procedure TForm1.Button3Click(Sender: TObject); var i:Integer; begin with StringGrid1 do begin for i:=1 to RowCount-2 do 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; procedure TForm1.NMSMTP1Failure(Sender: TObject); begin StatusBar1.SimpleText:=‘Mail send failure!’; end; procedure TForm1.NMSMTP1SendStart(Sender: TObject); begin StatusBar1.SimpleText:=‘Now Sending...’; end; procedure TForm1.NMSMTP1Success(Sender: TObject); begin StatusBar1.SimpleText:=‘Send Success!’; end; end. Button1是“New Mail”按鈕,Button 2是“Send”按鈕,Button3是“Clear” 按鈕。 以下是Unit2代碼清單: unit Unit2; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm2 = class(TForm) OpenDialog1: TOpenDialog; GroupBox1: TGroupBox; GroupBox2: TGroupBox; Edit1: TEdit; Edit2: TEdit; Edit3: TEdit; Edit4: TEdit; Edit5: TEdit; Button1: TButton; Button2: TButton; Button3: TButton; Label1: TLabel; Label2: TLabel; Label3: TLabel; Label4: TLabel; Label5: TLabel; Label6: TLabel; procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); procedure Button3Click(Sender: TObject); procedure FormActivate(Sender: TObject); procedure FormCreate(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form2: TForm2; implementation uses Unit1; {$R *.DFM} procedure TForm2.FormCreate(Sender: TObject); begin self.Caption:=‘New Mail’; self.BorderStyle:=bsDialog; end; procedure TForm2.FormActivate(Sender: TObject); begin Edit1.Text:=‘’; Edit2.Text:=‘’; Edit3.Text:=‘’; Edit4.Text:=‘’; Edit5.Text:=‘’; Label1.Caption:=‘No File’; end; procedure TForm2.Button1Click(Sender: TObject); begin if OpenDialog1.Execute then Label1.Caption:=Opendialog1.Filename; end; procedure TForm2.Button2Click(Sender: TObject); var i:Integer; begin if (Edit1.Text<>‘’) and (Edit2.Text<>‘’) and (Edit5.Text<>‘’) and (Label1.Caption<>‘No File’) then begin with Form1.StringGrid1 do begin RowCount:=RowCount+1; i:=RowCount-2; Cells[0,i]:=Edit1.Text; Cells[1,i]:=Edit2.Text; Cells[2,i]:=Edit5.Text; Cells[3,i]:=Edit3.Text; Cells[4,i]:=Edit4.Text; Cells[5,i]:=Label1.Caption; end; Form1.Button2.Enabled:=True; Form1.Button3.Enabled:=True; end; self.Hide; end; procedure TForm2.Button3Click(Sender: TObject); begin self.Hide; end; end. Edit1、Edit2、Edit3、Edit4、Edit5編輯框分別用于填寫服務器名稱、收件人地址、發件人名稱、 郵件主題和發件人地址。
------
~~~Delphi K.Top討論區站長~~~ |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |