贴一段代码给你,检测你是否连在网上.
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ComCtrls, Psock, NMsmtp;
const
WM_LOGIN =WM_USER+100;
Type
TWM = record
Msg: WORD;
wParam:Word;
lParam
WORD;
end;
type
TForm1 = class(TForm)
Label1: TLabel;
Label2: TLabel;
Edit1: TEdit;
Edit2: TEdit;
Edit3: TEdit;
Label4: TLabel;
Edit4: TEdit;
Label5: TLabel;
Label3: TLabel;
ListBox1: TListBox;
Label6: TLabel;
Edit5: TEdit;
Label7: TLabel;
Memo1: TMemo;
StatusBar1: TStatusBar;
btnSend: TButton;
Edit6: TEdit;
Button1: TButton;
NMSMTP1: TNMSMTP;
procedure btnSendClick(Sender: TObject);
procedure NMSMTP1Connect(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure NMSMTP1Disconnect(Sender: TObject);
procedure NMSMTP1ConnectionFailed(Sender: TObject);
procedure NMSMTP1HostResolved(Sender: TComponent);
procedure NMSMTP1InvalidHost(var Handled: Boolean);
procedure NMSMTP1SendStart(Sender: TObject);
procedure NMSMTP1Success(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure ListBox1KeyUp(Sender: TObject;
var Key: Word;
Shift: TShiftState);
private
{ Private declarations }
procedure WMLOGIN(var Msg:TWM);Message WM_LOGIN;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
{ TForm1 }
procedure TForm1.WMLOGIN(var Msg: TWM);
var
i:integer;
begin
//Send mail
//fix part of mail
NMSMTP1.PostMessage.FromAddress := Edit4.Text;
NMSMTP1.PostMessage.FromName := Edit3.Text;
NMSMTP1.PostMessage.Subject := Edit5.Text;
//clear old info
NMSMTP1.PostMessage.ToAddress.Clear;
//send a mail back
//NMSMTP1.PostMessage.ToAddress.Add(Edit4.Text);
NMSMTP1.PostMessage.Body.Assign(Memo1.Lines);
//set mail list
//NMSMTP1.PostMessage.ToBlindCarbonCopy.Clear;
for i:=0 to ListBox1.Items.Count -1do
begin
NMSMTP1.PostMessage.ToAddress.Add(
ListBox1.Items.Strings
);
end;
NMSMTP1.SendMail;
NMSMTP1.Disconnect;
end;
procedure TForm1.btnSendClick(Sender: TObject);
begin
//set server info
NMSMTP1.Host := Edit1.Text;
NMSMTP1.Port := StrToInt(Edit2.Text);
NMSMTP1.UserID:=Edit3.Text;
//update view
StatusBar1.SimpleText:='Connecting...';
StatusBar1.Update;
//connect
NMSMTP1.Connect;
end;
procedure TForm1.NMSMTP1Connect(Sender: TObject);
begin
//update view
StatusBar1.SimpleText:='Connected';
StatusBar1.Update;
SendMessage(Handle,WM_LOGIN,0,0);
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
//server info
Edit1.Text:='10.13.101.10';
Edit2.Text:='25';
//User info
Edit3.Text:='Bowman';
Edit4.Text:='MatthewBowman@21cn.com';
//Empty Other
Edit5.Text:='';
Edit6.Text:='';
Memo1.Text:='';
end;
procedure TForm1.NMSMTP1Disconnect(Sender: TObject);
begin
If StatusBar1 <> nil then
StatusBar1.SimpleText := 'Disconnected from server';
end;
procedure TForm1.NMSMTP1ConnectionFailed(Sender: TObject);
begin
ShowMessage('Connection Failed');
end;
procedure TForm1.NMSMTP1HostResolved(Sender: TComponent);
begin
StatusBar1.SimpleText := 'HostResolved';
end;
procedure TForm1.NMSMTP1InvalidHost(var Handled: Boolean);
begin
StatusBar1.SimpleText := 'InvalidHost';
end;
procedure TForm1.NMSMTP1SendStart(Sender: TObject);
begin
StatusBar1.SimpleText := 'Sending Envelop...';
end;
procedure TForm1.NMSMTP1Success(Sender: TObject);
begin
StatusBar1.SimpleText := 'Success';
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
//add list
ListBox1.Items.Add(Edit6.Text);
end;
procedure TForm1.ListBox1KeyUp(Sender: TObject;
var Key: Word;
Shift: TShiftState);
var
idx:integer;
begin
if ListBox1.ItemIndex=-1 then
exit;
if Key=VK_DELETE then
begin
idx:=ListBox1.ItemIndex;
ListBox1.Items.Delete(idx);
end;
end;
end.