Subscribe

RSS Feed (xml)

Powered By

Skin Design:
Free Blogger Skins

Powered by Blogger

星期四, 9月 06, 2007

SMTP寄信程式



首先 我們先在Form上放下列元件:
7個Edit、7個Label、1個Button、1個Memo、1個IdSMTP(在Indy Client裡)、1個IdMessage(在Indy Misc裡)
接著就編輯Button1的onClick程式碼(以下內容):


procedure TForm1.Button1Click(Sender: TObject);
begin
try
IdSMTP1.AuthenticationType:=atLogin; //登陸類型
IdSMTP1.Username:=Edit1.Text; //使用者帳號
IdSMTP1.Password:=Edit2.Text; //使用者密碼
IdSMTP1.Host:=Edit3.Text; //SMTP伺服器位址
IdSMTP1.Port:=strtoint(Edit4.Text); //SMTP埠必須轉換為整數
IdSMTP1.Connect;
except
Showmessage('連線失敗!!');
Exit;
end;
IdMessage1.Body.Clear;//清除上次信件內容
IdMessage1.Body.Assign(Memo1.Lines);//信件內容
IdMessage1.From.Address:=Edit5.Text;//發信人地址
IdMessage1.Recipients.EMailAddresses:=Edit6.Text;//收信人地址
IdMessage1.Subject:=Edit7.Text;//信件主旨
try
IdSMTP1.Send(IdMessage1);
showmessage('發送成功!!');
except
showmessage('發送失敗!!有問題了!!');
end;
end;


若要加入密碼

uses
IdSASLLogin, IdUserPassProvider;

var
IdSASLLogin1 :TIdSASLLogin;
LHlogin :TIdUserPassProvider;
begin
IdSASLLogin1 :=TIdSASLLogin.Create();
LHlogin :=TIdUserPassProvider.Create();
IdSMTP1.Username:=sUsername;
IdSMTP1.Password:=sPassword;
LHlogin.Username:=sUsername;
LHlogin.Password:=sPassword;
IDSMTP1.AuthType:=atSASL;
IdSMTP1.SASLMechanisms.Add.SASL:=IdSASLLogin1;
IdSASLLogin1.UserPassProvider:=LHlogin;


try
IdSMTP1.Connect;

//IDSMTP1.ConnectTimeout := 1000;
//IdSMTP1.Connect(1000);
if IdSMTP1.Connected then
begin

if (Length(sUsername)) > 0 then
begin
if not IdSMTP1.Authenticate then
begin
//DebugStr('@@驗證失敗');//'郵件伺服器驗證失敗!'
exit;
end;
end;
IdSMTP1.Send(IdMessage1); //寄出報表
end;

finally
if IdSMTP1.Connected then IdSMTP1.Disconnect;
end;

沒有留言: