很久沒有發過博客了,不知道為什么沒這個思想來博客上轉轉。今天把這個前段時間做的試驗發上來。
郵件與我們的工作生活息息相關,很多的信息傳遞都是通過郵件完成的。所以郵件對我們來說很重要,雖然可以每次登錄郵箱進行操作,但郵件資料總是要通過網站登錄,而且還要一個一個自己下載,這樣操作在網速較慢時比較費力。現在有一些很郵件管理工具,如FOXMAIL等,這些工具功能相當完善了,但功能一多也就天花亂綴了,使用起來就不怎么爽心悅目了,么那如果我們能自己打造一個適合自己需要的郵件管理工具是一個挺不錯的選擇。
經過測試,用DELPHI中的TIdPOP3 和TIdSMTP這兩個組件完全能夠對大部份郵件服務器進行郵件收發操作。
TIdPOP3組件簡介
TIdPOP3 是用來接收郵件服務器的郵件信息到用戶端的一個組件。它實現了RFC 1939協議。
在使用TIdPOP3組件時需設置它的幾個成員屬性。
Host :指定郵件服務器,一般為pop3郵件服務器的地址,如 pop3.126.com。
Username :用戶名,也就是郵箱名,如billanking2002@126.com。
Password :郵箱密碼,在進行收發郵件時組件需要憑密碼進行登錄。
其它成員屬性
Connected:返回它與郵件服務器的連接狀態,這true表示已經連接。
CheckMessages:郵件數,如果連接服務器成功,則可以獲得服務器端的郵件數。
成員函數
procedure Connect(const ATimeout: Integer = IdTimeoutDefault);
與服務器連接函數。參數為無效連接時等待的毫秒數。
function RetrieveHeader(const MsgNum: Integer; AMsg: TIdMessage): Boolean;
接收郵件頭信息,它有兩個參數,MsgNum表示在接收第幾個郵件,從1開始,AMsg為郵件消息組件實例。
function Retrieve(const MsgNum: Integer; AMsg: TIdMessage): Boolean;
接收郵件主體信息,它與 RetrieveHeader的參數是一樣的。接收的郵件內容將保存在AMsg中。
function Delete(const MsgNum: Integer): Boolean;
刪除郵件服務器中第幾個郵件。從1開始。
procedure Disconnect; override;
關閉連接。
TIdMessage組件簡介
TIdMessage用來支持郵件消息協議,如POP3,SMTP,NNTP等。TIdMessage支持多用途Internet郵件擴展(MIME)協議。
常用的TIdMessage的屬性:
Subject:郵件主題,這個字符串經過BASE64編碼的。所以在使用時需對它進行解碼。
MessageParts:這是TIdMessageParts類的一個實例,它用來存儲郵件的信息。如郵件內容及附件信息。在進行解析時需要判斷它是否為附件或文本,如果為附件時,其文件名是經過BASE64編碼的。判斷常量分別為TIdText , TIdAttachment。
Body:這是個字符串列表,包含構成該郵件的正文內容。
Form:發送郵件者的地址信息。
Recipients:收件人地址信息。
BccList:抄送地址列表。
CharSet:指示郵件信息中使用的字符集。
ContentType:指定MIME媒體數據類型,描述正文中包含的數據,使用戶代理決定如何顯示數據,常用的有text/html,text/xml。
TIdSMTP組件簡介
TIdSMTP是TIdMessageClient派生出的一個簡單郵件傳輸協議和SMTP客戶端。
它的主要功能是發送郵件信息。
常用的屬性:
Host :SMTP郵件服務器的地址,如smtp.126.com。它與上面的POP3地址不一樣。
AuthenticationType:服務器認證類型,它有atNone,atLogin兩種,即不需要認證和需要憑用戶名和密碼進行認證。
Username:用戶名,這里與TIdPOP3 有點不一樣,即它不需要后綴,如billanking2002
Password:郵箱登錄密碼。如果AuthenticationType設置了atLongin則必須設置密碼和用戶名。
下面來看下程序源代碼:
新建一個DELPHI工程,在窗口中加入TIdPOP3,TIdMessage,TIdSMTP三個組件
加入兩個按鈕,一個為收郵件,一個為發郵件。加入三個編輯框,一個為服務器地址,一個為用戶名,一個為密碼。和一個Memo,WebBrowser控件。
下面為其工程的源文件代碼。
判斷輸入的字符串是不是為一個郵件地址。
function TForm1.IsEMail(EMail: String): Boolean;
var
s: String;
ETpos: Integer;
begin
ETpos:= pos('@',EMail); //主要判斷有沒有@符
if ETpos> 1 then
begin
s:= copy(EMail,ETpos+1,Length(EMail));
if (pos('.',s) > 1) and (pos('.',s) < length(s)) then Result:= true
else Result:= false;
end
else Result:= false;
end;
BASE64編碼轉換,這個很重要,因為郵件的標題,及附近的文件名都是經過了編碼轉換的。
function TForm1.Base64Decode(strInput:string):string;
var
strDecode : string;
posStart : Integer;
posEnd : Integer;
begin
while pos('=?gb2312?b?',lowercase(strInput))>0 do
begin
try
posStart:=pos('=?gb2312?b?',lowercase(strInput));
posEnd:=pos('?=',lowercase(strInput));
strDecode:=strDecode+copy(strInput,1,posStart-1)+IdDecoderMIME1.DecodeString (copy(strInput,posStart+11,posEnd-posStart-11));
strInput:=copy(strInput,posEnd+2,length(strInput)-posEnd-1);
finally
Application.ProcessMessages;
end;
end;
strDecode := strDecode + strInput;
result := strDecode;
end;
收郵件功能函數:
procedure TForm1.BitBtn1Click(Sender: TObject);
var
i,j,mnnn,mailcount:integer;
tmp:string;
begin
IdPOP31.Host := e_address.text;
dPOP31.Username := e_username.text
dPOP31.Password := e_password.text;
while true do
begin
IdPOP31.Connect(); //連接到POP3服務器
if (IdPOP31.Connected = true) then
begin
break;
end
else begin
IdPOP31.Password :='gozhyflkmcfs';
end;
end;
mailcount := IdPOP31.CheckMessages; //得到郵箱中郵件的個數
mnnn := 0;
for i:=1 to mailcount do /遍歷每一封郵件
begin
IdMessage1.Clear;
IdPOP31.retrieveHeader(i,IdMessage1); //得到郵件的頭信息
tmp := IdMessage1.Subject; //得到郵件的標題
Memo1.Lines.Add('-----------------------------------------------------');
tmp := Base64Decode(tmp); //郵件標題BASE64解碼
memo1.Lines.Add(tmp);
IdPOP31.Retrieve(i,IdMessage1); //接收到郵件所有內容
for j:=0 to idmessage1.MessageParts.Count-1 do
begin
try
if (IdMessage1.MessageParts.Items[j] is TIdAttachment) then
//匹配郵件條目是否是附件
Begin tmp:=Base64Decode(TIdAttachment(IdMessage1.MessageParts.tems[j]).FileName);
tmp := 'C:\'+tmp;
deletefile(tmp); TIdAttachment(IdMessage1.MessageParts.Items[j]).SaveToFile(tmp); //以原有文件名保存附件在指定目錄
end
else begin
if (idmessage1.MessageParts.Items[j] is TidText) then
begin
if Message1.Body.Text<>'' then
sethtml(WebBrowser1,tidtext(idmessage1.MessageParts.Items[j]).Body.text)
else memo1.Lines.Add(tidtext(idmessage1.MessageParts.Items[j]).Body.text);
end;
end;
except
continue;
end;
end;
if checkbox1.Checked = true then
begin
try
idpop31.Delete(i); //刪除收到的郵件
except
continue;
end;
end;
end;
idpop31.DisconnectSocket;
IdPOP31.Disconnect;
end;
發送郵件功能函數:
procedure TForm1.BitBtn2Click(Sender: TObject);
var
filename:string;
begin
IdSMTP1.Host := e_address.text;
idsmtp1.AuthenticationType := atLogin; //{atNone,atLogin}
IdSMTP1.Username := e_username.text;
IdSMTP1.Password := e_password.text;
filename := 'd:\comunition.rar1';
TIdattachment.Create(IdMessage1.MessageParts,filename); //加入附件
filename := 'd:\comunition.rar';
TIdattachment.Create(IdMessage1.MessageParts,filename); //加入多個附件
IdMessage1.From.Address := 'billanking2002@126.com'; //郵件來自哪里
IdMessage1.Recipients.EMailAddresses:='billanking2002@hotmail.com; //收件人
idMessage1.BccList.Add.Text := 'billanking2002@hotmail.com'; //抄送地址列表
idMessage1.BccList.Add.Text := 'billanking2002@yahoo.com.cn';
idMessage1.BccList.Add.Text := 'hyh@ctrl-tech.com';
IdMessage1.Subject:= '郵件客戶端';
IdMessage1.Body.Text := Memo1.Text; //此處為郵件正文
IdMessage1.CharSet := 'gb2312'; //保證附件正文漢字的正常顯示
idmessage1.ContentType := 'text/html'; //超文本方式傳輸
IdMessage1.Body.Assign(Memo1.Lines);
if IdSMTP1.AuthSchemesSupported.IndexOf('LOGIN')>-1 then
begin
IdSMTP1.AuthenticationType := atLogin;
//連接前要保存為Login 上面己設,此處不必要
IdSMTP1.Authenticate;
end;
try
IdSMTP1.Connect(); //連接SMTP服務器
IdSMTP1.Authenticate;
IdSMTP1.Send(IdMessage1); //向服務器發送郵箱
finally
if IdSMTP1.Connected then IdSMTP1.Disconnect; //斷開與服務器的連接
end;
end;
至此,郵件收發功能基本完成。要做一個功能完整的郵件收發管理工具,還需要加入郵件管理功能。這些功能主要是通過在本地操作文件實現。這樣可以以自己的方式保管理這些郵件。
本文來自CSDN博客,轉載請標明出處:http://blog.csdn.net/billanking/archive/2010/12/18/6083616.aspx