⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.~pas

📁 求是科技出版的《Delphi串口通信工程开发实例导航》所有的源代码。是一本很好的书。拿出来与大家共享。
💻 ~PAS
📖 第 1 页 / 共 3 页
字号:
                // 依次获取账号连表中的每一个账号
                //
                pAccount := Account.m_lstAccount.Items[act_index];

                //
                // 确保 lstNewMail 已经初始化
                //
                lstNewMail := TList.Create();

                //
                // 确保 NMPOP3 已经初始化
                //
                if NMPOP3 = nil then
                        NMPOP3 := TNMPOP3.Create(self);

                //
                // 设置POP3连接信息
                //
                NMPOP3.Host := pAccount^.m_Server;
                NMPOP3.Port := StrToInt(pAccount^.m_Port);
                NMPOP3.UserID := pAccount^.m_User;
                NMPOP3.Password := pAccount^.m_Pwd;

                //
                //  加入连接日志
                //
                AddLog('**********' + '正在检测账号' + pAccount^.m_Account + '**********', SCHEDULE);
                frmRight.frmBottom.SetStatus('正在检测账号' + pAccount^.m_Account);
                AddLog('正在连接服务器 ' + pAccount^.m_Server + '...', INFO);

                //
                // 如果没有连接,则重新连接
                // m_IsCancel 标志是否因用户名密码不匹配而取消连接
                // m_IsReLogin 标志是否因用户名密码不匹配而重新登录
                //
                m_IsCancel := False;
                m_IsReLogin := False;
                m_IsFirstConn := True;
                try
                        if NMPOP3.Connected then
                                NMPOP3.Disconnect();
                        NMPOP3.Connect();
                except
                       on E : Exception do
                       m_IsReLogin := True;
                end;
                m_isFirstConn := False;
                //
                // 如果取消连接,则停止以下代码的执行,继续检测下一账号
                //
                if m_IsCancel then
                        Continue;
               // if not NMPOP3.Connected then
               //         continue;

                //
                // 此时登录成功,以下开始获取邮件信息
                // 首先根据选项来决定是否记住新用户名和密码
                //
                if (frmLogin.chkStore.Checked = True) and (m_IsReLogin = True) then
                begin
                        pAccount^.m_User := frmLogin.edtUser.text;
                        pAccount^.m_Pwd := frmLogin.edtPwd.text;
                        UpdateLeftView();
                        UpdateTopView();
                end;
                //
                // 加入连接日志
                //
                AddLog('正在读取邮件列表...', INFO);
                AddLog('收件箱中共有邮件 ' + IntToStr(NMPOP3.MailCount), INFO );
                //
                //  然后首先获取邮件数目
                //
                for mail_index := 1 to NMPOP3.MailCount do
                begin
                        if m_IsStop then
                        begin
                                AddLog('用户取消操作', WARNING);
                                exit;
                        end;
                        if not NMPOP3.Connected then
                                NMPOP3.Connect();
                        //
                        // 加入检测日志
                        //
                        AddLog('正在分析第 ' + IntToStr(mail_index) + ' 封邮件', INFO);
                        //
                        // 检测邮件是否已经存在
                        //
                        tmp := NMPOP3.UniqueID(mail_index);
                        if IsMailExist(NMPOP3.UniqueID(mail_index), pAccount^) then
                                continue;

                        // 有新邮件
                        m_HaveNewmail := True;
                        //
                        // 加入检测日志
                        //
                        AddLog('正在接收第' + IntToStr(mail_index) + ' 的摘要...', INFO);
                        //
                        // 获取邮件摘要
                        //
                        NMPOP3.GetSummary(mail_index);
                        //
                        // 如果不存在,给新邮件分配空间并初始化
                        //
                        new(pEmail);
                        pEmail^.m_Id := NMPOP3.UniqueID(mail_index);
                        pEmail^.m_From := NMPOP3.Summary.From;
                        pEmail^.m_Subject := NMPOP3.Summary.Subject;
                        pEmail^.m_Size := IntToStr(NMPOP3.Summary.Bytes);
                        pEmail^.m_Date := NMPOP3.Summary.Header.Values['Date'];
                        pEmail^.m_IsNew := True;

                        NMPOP3.Disconnect();

                        //
                        // 加入到新邮件链表和账号邮件信息列表中
                        //
                        lstNewMail.Add(pEmail);
                        pAccount^.m_lstEmail.Add(pEmail);
                        //
                        // 如果是当前活动账号,更新显示列表
                        //
                        if act_index = frmRight.frmTop.tabAccount.TabIndex then
                        begin
                                frmRight.frmTop.AddToMailList(pEmail^);
                                UpdateLeftView();
                        end;
                end;  // end of storing new mails for one account;

                //
                // 加入连接日志
                //
                AddLog('账号 ' + pAccount^.m_Account + ' 共有新邮件 ' + IntToStr(lstNewMail.count), INFO );
                AddLog('正在从服务器 ' + pAccount^.m_Server + ' 断开连接...', INFO);
                AddLog('************************完成对 ' + pAccount^.m_Account + '的检测******************************', SCHEDULE);
                //
                // 将新邮件编辑短信发送到手机
                //
                if (pAccount^.m_SendToMobile) and (lstNewMail.Count > 0) then
                begin
                        //
                        // 发送短信日志在 SendSMS函数中
                        //
                        frmRight.frmBottom.SetStatus('正在向手机 ' + pAccount^.m_Mobile + '发送短信');
                        SendSMS(lstNewMail, pAccount^.m_Mobile);
                end;
        end; // end of checking mail for all the accounts;
        //
        // 如果有新邮件,播放声音
        //
        if (Config.m_Config.m_WaveFile <> '') and  (m_HaveNewMail) then
                sndPlaySound(PChar(Config.m_Config.m_WaveFile), SND_ASYNC);

        //sndPlaySound(PChar(edtWaveFile.Text), SND_ASYNC);

        //
        // 设置当前状态
        //
        frmRight.frmBottom.SetStatus('空闲');
end;

//
// 将 lst 中的邮件编辑短信发送到手机 Mobile 上
//
procedure TfrmMain.SendSMS(lst: TList; Mobile: string);
var
        i : integer;
        strSMS : string;
        strLog : string;
        pEmail : ^TEmailInfo;
 begin
        //
        // 加入发送短消息日志
        //
        AddLog('###################开始向手机 ' + Mobile + ' 发送短消息######################', SMS);
        if length(Mobile) <> 11 then
        begin
                ShowMessage('目标手机号不正确,发送短信失败!');
                AddLog('目标手机号不正确,发送短信失败!', ERROR);
                exit;
        end;
        if length(Config.m_Config.m_SMSCenter) <> 13 then
        begin
                ShowMessage('短信中心地址不正确,发送短信失败!');
                AddLog('短信中心地址不正确,发送短信失败!', ERROR);
                exit;
        end;

        //
        // 打开端口
        //
        if not OpenPort() then
                exit;
        //
        // 初始化短消息,指定编码模式和短消息中心地址
        //
        IniSMS();
        for i := 0 to lst.Count - 1 do
        begin
                pEmail := lst.Items[i];

                strLog := '向手机 ' + Mobile + ' 发送短信息:';
                if Config.m_Config.m_SMSMode = 'Text' then
                begin
                        strSMS := 'you have new mail from ' + pEmail^.m_From;
                        strSMS := strSMS + 'the subject is ' + pEmail^.m_Subject;
                        SendInEncodeText(strSMS, Mobile);
                end
                else
                begin
                        strSMS := strSMS + '您有新邮件来自 ' + pEmail^.m_From;
                        strSMS := strSMS + '邮件主题是: ' + pEmail^.m_Subject;
                        SendInEncodePdu(strSMS, Mobile);
                end;
                strLog := strLog + strSMS;
                //
                // 加入连接日志
                //
                AddLog(strLog, SMS);
        end;
        //
        // 关闭串口
        //
        MSComm.PortOpen := False;
        AddLog('###################向手机' + Mobile + '发送短消息完毕,共发送' + IntToStr(lst.Count) + '条短消息#####################', SMS);
end;

//
// 检测 ID 为 MsgId的邮件在 act 的邮链表中是否已经存在,如果存在,返回 true
//
function TfrmMain.IsMailExist(MsgId: string;
  act: TAccountInfo): Boolean;
var
        i : integer;
        pEmail : ^TEmailInfo;
begin
        for i := 0 to act.m_lstEmail.Count -1 do
        begin
                pEmail := act.m_lstEmail.Items[i];
                if MsgId = pEmail^.m_Id then
                begin
                        IsMailExist := true;
                        exit
                end;
        end;
        IsMailExist := false;
end;

procedure TfrmMain.NMPOP3ConnectionFailed(Sender: TObject);
begin
        ShowMessage('与服务器连接失败,请检查网络联接');
end;

procedure TfrmMain.NMPOP3Failure(Sender: TObject);
begin
        AddLog('有错误发生,错误代码:' + IntToStr(NMPOP3.LastErrorNo), ERROR );
end;

//
// 登录失败时触发此事件
//
procedure TfrmMain.NMPOP3AuthenticationFailed(var Handled: Boolean);
begin
        //
        // 捕获异常,加入自己的处理代码
        //
        Handled := True;

        if NMPOP3.Connected then
        begin
                NMPOP3.Abort();
                NMPOP3.Disconnect();
        end;

        AddLog('用户 ' + NMPOP3.UserID + ' 登录失败,等待重新输入用户名和密码...', ERROR);
        //
        // 弹出登录对话框,重新输入用户名和密码
        //
        frmLogin.ShowModal();
        m_IsCancel := not frmLogin.m_IsOk;
        //
        // 如果用户没有取消登录,则用新的用户名和密码重新尝试
        //
        if not m_IsCancel then
        begin
                NMPOP3.UserID := frmLogin.edtUser.Text;
                NMPOP3.Password := frmLogin.edtPwd.Text;
                NMPOP3.Connect();
        end;
end;

//
// 记录日志
//
procedure TfrmMain.AddLog(msg: string; const LogTp : integer);
var
        Item : TListItem;
begin
        Item := frmRight.frmBottom.lstLog.Items.Add();
        Item.Caption := msg;
        Item.ImageIndex := LogTp;
        frmRight.frmBottom.lstLog.Scroll(0, 20 );
        frmRight.frmBottom.lstLog.Refresh();
end;

procedure TfrmMain.NMPOP3InvalidHost(var Handled: Boolean);
begin
        AddLog('无法连接服务器...', ERROR);
end;

procedure TfrmMain.NMPOP3Success(Sender: TObject);
begin
        AddLog('连接成功', INFO);
end;

procedure TfrmMain.NMPOP3HostResolved(Sender: TComponent);
begin
        
        if m_IsFirstConn then
        begin
                AddLog('判断主机 ' + NMPOP3.Host + ' 为 [' + NMPOP3.RemoteIP + ']', INFO);
                m_IsFirstConn := False;
        end;
end;

//
// 当前活动账号改变时触发此事件
//
procedure TfrmMain.frmToptabAccountChange(Sender: TObject);
var
        pAccount : ^TAccountInfo;
begin
        //
        // 更新左侧视图
        //
        UpDateLeftView();
        //
        // 更新邮件显示列表
        //
        pAccount := Account.m_lstAccount.Items[frmRight.frmTop.tabAccount.TabIndex];
        UpdateEmailList(pAccount^.m_lstEmail);
end;

//
// 更新当前账号的邮件列表
//

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -