关于sakemail,本人也是听从了一位富翁的建议使用的,详细的用法其实不是太复杂,sakemail本身已经有源代码及测试程序。
要给出我的程序的源码是不可能的,因为这是公司的产品;不过可将邮件部分的处理摘出来,其实各位还是看sakemail本身的样板程序可能来得更有效。关于sakemail控件,网络上很多地方都有,请各位自己用google找一下。
也可到该控件的发源地找: http://sak.org.ar
sakemail论坛在: http://groups.yahoo.com/group/sakemail/
部分程序摘录如下,没有经过整理,但愿各位能看明白。用的是3.5.1版本,估计有更新的版本。
//------------------------- for email send out ------------------------------
function TjobForm.mailConnectToServer(host: String; bsmtpneedpass: Boolean; acc, pass: String): Boolean;
begin
//不支持需要密码的SMTP连接
sakSMTP.Disconnect;
Cfg.iMailProcState := ST_FREE;
sakSMTP.Host := host;
try
sakSMTP.Connect;
Result := sakSMTP.Connected;
except
Result := False;
end;
end;
function TjobForm.mailSetAccountInfo(myname, myacc, replyto: String): Boolean;
begin
try
sakMsg.UserName := myname;
sakMsg.From := myacc;
sakMsg.ReplyTo := replyto;
Result := True;
except
Result := false;
end;
end;
function TjobForm.mailSetSendData(subject: String; info, acclst: TStrings): Boolean;
var
i: Integer;
begin
try
if Not bSakExists then
begin
SakMsg := TSakMsg.Create(nil);
bSakExists := True;
end;
sakMsg.Subject := subject;
sakMsg.Text.Assign(info);
sakMsg.AttachedFiles.Clear;
for i:=0 to acclst.Count-1 do
sakMsg.AttachedFiles.Add(acclst);
Result := True;
except //文件不存在时,可能产生异常
Result := False;
end;
end;
function TjobForm.mailSendMail(sendto: String; bwait: Boolean): Boolean;
begin
try
if Not sakSmtp.Connected then
sakSmtp.Connect;
sakMsg.SendTo := sendto;
sakSmtp.SendTheMessage(sakMsg);
if bwait then
begin
while Not Cfg.bMailProcEnd do
Delay(100);
Result := Cfg.iMailProcState = ST_SUCESS;
end
else Result := True;
except
Result := False;
end;
end;
procedure TjobForm.mailDisconnect;
begin
sakSmtp.Disconnect;
end;
//------------------------- for email receive -------------------------------
//return code: 0-Sucess, 1-Not Connected, 2-login error, else other error
function TjobForm.mailRecvConnectToServer(host: String; acc, pass: String): Integer;
begin
SakPOP.Disconnect;
SakPOP.Host := host;
SakPOP.UserId := acc;
SakPOP.UserPasswd := pass;
try
SakPOP.connect;
If SakPOP.POPError then
Result := 1
else if not SakPOP.login then
Result := 2
else Result := 0;
except
Result := 1;
end;
if Result <> 0 then
SakPOP.Disconnect;
end;
function TjobForm.mailRecvGetMessages(bdelete: Boolean): Integer; //return messages count
begin
SakMsgList.Clear;
if Not SakPop.Connected then
begin
Result := 0;
Exit;
end;
try
SakPOP.Init;
Result := SakPop.MsgsCount;
mailrecvProgressBar.Max := sakPop.MsgsCount;
if Result > 0 then
begin
try
sakPOP.retrieveAllMessages(SakMsgList);
except
end;
Result := SakMsgList.Count;
If Result > 0 then
iMailRecvCurrentMsg := 0;
if (Result > 0) and bdelete then
mailRecvDeleteMessages();
end;
except
Result := 0;
end;
end;
function TjobForm.mailRecvGetMessage(var msg: TSakMsg): Boolean;
begin
Result := SakMsgList.Count > iMailRecvCurrentMsg;
if Result then
begin
msg := SakMsgList.Items[iMailRecvCurrentMsg];
Inc(iMailRecvCurrentMsg);
end;
end;
function TjobForm.mailRecvDeleteMessages: Boolean; //delete all message in sakmsglist
var
i: Integer;
begin
try
for i:=0 to SakMsgList.Count-1 do
SakPop.DeleteMessage(i+1);//StrToInt(SakMsgList.Items.MessageID));
Result := True;
except
Result := False;
end;
end;
procedure TjobForm.mailRecvDisconnect;
begin
try
SakPop.Disconnect;
except
end;
end;
//------------------- for mail send test ------------------------------------
procedure TjobForm.btnSendClick(Sender: TObject);
begin
SakSMTP.host := EditHost.text;
SakMsg.UserName := EditUserName.text;
SakMsg.From := EditFrom.text;
SakMsg.SendTo := EditTo.text;
SakMsg.Subject := EditSubject.text;
SakMsg.Text.Assign(Memo2.Lines);
SakMsg.AttachedFiles.Add('logo1.jpg');
SakSMTP.connect;
if not SakSMTP.SMTPError then
begin
SakSMTP.SendTheMessage(SakMsg);
Delay(5000);
SakSMTP.Disconnect;
end;
SakMsg.Free;
SakMsg := TSakMsg.Create( self);
end;
procedure TjobForm.btnSndConnectClick(Sender: TObject);
begin
mailConnectToServer(edithost.Text, false, '', '');
end;
procedure TjobForm.btnSetAccInfoClick(Sender: TObject);
begin
mailSetAccountInfo(editusername.Text, editfrom.Text, edit1.Text);
end;
procedure TjobForm.btnSetDataClick(Sender: TObject);
var
lst: TStrings;
begin
lst := TStringList.Create();
lst.Add('logo1.jpg');
mailSetSendData(editsubject.text, memo2.Lines, lst);
lst.Free;
end;
procedure TjobForm.btnSendMailClick(Sender: TObject);
begin
mailSendMail(editto.text, false);
end;
procedure TjobForm.btnSndDiscClick(Sender: TObject);
begin
mailDisconnect;
end;
//------------------- for mail receive test ---------------------------------
procedure TjobForm.N7Click(Sender: TObject);
begin
mailrecvPanel.Visible := False;
mailsendPanel.Visible := True;
end;
procedure TjobForm.N8Click(Sender: TObject);
begin
mailrecvPanel.Visible := True;
mailsendPanel.Visible := False;
end;
procedure TjobForm.N10Click(Sender: TObject);
begin
mailrecvPanel.Visible := false;
mailsendPanel.Visible := false;
end;
procedure TjobForm.btnConnectClick(Sender: TObject);
begin
mailRecvConnectToServer(recvHost.Text, recvAcc.Text, recvPass.Text);
end;
procedure TjobForm.btnGetMsgClick(Sender: TObject);
begin
if mailRecvGetMessages(False) > 0 then
begin
showRecvMsg;
end
else Application.MessageBox('你没有新邮件!', '邮件服务', MB_OK);
end;
procedure TjobForm.btnDeleteMailClick(Sender: TObject);
begin
mailRecvDeleteMessages();
end;
procedure TjobForm.btnDisconnectClick(Sender: TObject);
begin
mailRecvDisconnect;
end;
procedure TjobForm.btnNextAccClick(Sender: TObject);
begin
if iMailRecvCurrentMsg < SakMsgList.Count-1 then
begin
Inc(iMailRecvCurrentMsg);
showRecvMsg;
end;
end;
procedure TjobForm.btnPrevAccClick(Sender: TObject);
begin
if iMailRecvCurrentMsg > 0 then
begin
Dec(iMailRecvCurrentMsg);
showRecvMsg;
end;
end;
procedure TjobForm.showRecvMsg;
var
i: Integer;
begin
if (iMailRecvCurrentMsg >= 0) and (iMailRecvCurrentMsg < sakMsgList.Count) then
begin
recvFrom.Text := sakMsgList[iMailRecvCurrentMsg].From;
recvTo.Text := sakMsgList[iMailRecvCurrentMsg].SendTo;
recvSubject.Text := sakMsgList[iMailRecvCurrentMsg].Subject;
recvMemo.Text := sakMsgList[iMailRecvCurrentMsg].Text.Text;
recvAtach.Items.Clear;
for i:=0 to sakMsgList[iMailRecvCurrentMsg].AttachedFiles.Count-1 do
recvAtach.Items.Add(sakMsgList[iMailRecvCurrentMsg].AttachedFiles.FileName);
//btnSave.Enabled := recvAtach.Items.Count > 0;
end;
end;
procedure TjobForm.recvAtachChange(Sender: TObject);
begin
btnSave.Enabled := recvAtach.Text <> '';
end;
procedure TjobForm.btnSaveClick(Sender: TObject);
begin
SaveDialog1.FileName := recvAtach.Text;
if SaveDialog1.Execute then
SakMsgList[iMailRecvCurrentMsg].AttachedFiles[recvAtach.ItemIndex].SaveToFile(saveDialog1.FileName);
end;
procedure TjobForm.btnGetnDelClick(Sender: TObject);
begin
if mailRecvGetMessages(True) > 0 then
begin
showRecvMsg;
end;
end;