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

📄 mainform.cpp

📁 关于书籍《Borland c++Builder工程实践》的源代码
💻 CPP
字号:
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "MainForm.h"
#include "SettingForm.h"
#include "SendForm.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TMainForm *MainForm;
//---------------------------------------------------------------------------
void __fastcall TMainForm::GetMail(int i)
{
    MainForm->NMPOP31->GetMailMessage(i);
    MainForm->txtBody->Lines->Assign(MainForm->NMPOP31->MailMessage->Body);
}
//---------------------------------------------------------------------------
__fastcall TMainForm::TMainForm(TComponent* Owner)
    : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::mnuSettingClick(TObject *Sender)
{
    SettingForm->ShowModal();
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::mnuExitClick(TObject *Sender)
{
    Close() ;
}
//---------------------------------------------------------------------------
int TMainForm::FindRealtMailNo(int i)
{
    int j;
    for(j=i;j<MailList.Length;j++)
        if (MailList[j]==1) return j;
    for(j=1;j<i;j++)
        if (MailList[j]==1) return j;
    return 0;
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::mnuDeleteMailClick(TObject *Sender)
{
    int i,nextmail;

    i = lstmail->Selected->Index+1;
    //从本程序邮件列表删除邮件信息
    lstmail->Selected->Delete();
    // 在服务器邮件列表中对给定邮件打上删除标记
    NMPOP31->DeleteMailMessage(FindRealtMailNo(i));
    //设置邮件删除标记
    MailList[FindRealtMailNo(i)]=0;
    //剩余邮件总数
    MailLeft--;
    //刷新邮件内容
    if (MailLeft > 0)
    {
        txtBody->Lines->Clear();
        nextmail=FindRealtMailNo(i);
        GetMail(nextmail) ;

        StatusBar1->SimpleText = "# of E-Mail: " + IntToStr(nextmail) ;
    }
    else
    {
        //无剩余邮件,设置相关参数 
        txtBody->Lines->Clear();

        SPeedButton3->Enabled = false ;
        SPeedButton4->Enabled = false ;
        SPeedButton5->Enabled = false ;
        mnuReply->Enabled = false ;
        mnuDelete->Enabled = false ;
        mnuForward->Enabled = false ;
        lstmail->Enabled = false;

        StatusBar1->SimpleText = "There is no E-Mail." ;
    }
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::NMPOP31Connect(TObject *Sender)
{
    StatusBar1->SimpleText = "Status: Connect to POP3 Server " + SettingForm->txtPOP3Host->Text ;
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::NMPOP31ConnectionFailed(TObject *Sender)
{
    StatusBar1->SimpleText = "Status: Connection Failed to POP3 Server " + SettingForm->txtPOP3Host->Text ;
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::NMPOP31ConnectionRequired(bool &Handled)
{
    if (MessageDlg("Connection Required ?", mtConfirmation, TMsgDlgButtons() << mbYes << mbNo, 0) == mrYes)
    {
        Handled = true;
        NMPOP31->Connect() ;
    }
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::NMPOP31DecodeEnd(TObject *Sender)
{
    StatusBar1->SimpleText = "Status: End Decoding";
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::NMPOP31DecodeStart(AnsiString &FileName)
{
    StatusBar1->SimpleText = "Status: Start Decoding " + FileName;
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::NMPOP31Disconnect(TObject *Sender)
{
    StatusBar1->SimpleText = "Status: Disconnect from POP3 Server " + SettingForm->txtPOP3Host->Text ;
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::NMPOP31Failure(TObject *Sender)
{
    StatusBar1->SimpleText = "Status: Failure.";
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::NMPOP31HostResolved(TComponent *Sender)
{
    StatusBar1->SimpleText = "Status: Host Resolved - " + SettingForm->txtPOP3Host->Text ;
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::NMPOP31InvalidHost(bool &Handled)
{
    AnsiString strHost ;

    if (InputQuery("Invalid POP3 Mail Server", "Please enter a valid POP3 Server", strHost))
    {
        NMPOP31->Host = strHost;
        Handled = true;
    }
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::NMPOP31Status(TComponent *Sender,
      AnsiString Status)
{
    StatusBar1->SimpleText = Status;
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::NMPOP31Success(TObject *Sender)
{
    StatusBar1->SimpleText = "Status: POP3 Success";
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::mnuReplyClick(TObject *Sender)
{
    int i;

    i = lstmail->Selected->Index+1;
    NMPOP31->GetSummary( FindRealtMailNo(i) );

    SendForm->Caption="邮件回复";
    SendForm->txtTo->Text = NMPOP31->Summary->From ;
    SendForm->txtCC->Text = "";
    SendForm->txtBCC->Text = "";
    SendForm->txtSubject->Text = "Re: " + NMPOP31->Summary->Subject ;
    SendForm->txtBody->Clear() ;
    SendForm->lstAttach->Clear() ;
    SendForm->Caption = "Reply Mail" ;
    SendForm->ShowModal();
}
//---------------------------------------------------------------------------

void __fastcall TMainForm::mnuForwardClick(TObject *Sender)
{
    int i, j;

    i = lstmail->Selected->Index+1;
    NMPOP31->GetSummary( FindRealtMailNo(i) );

    SendForm->Caption="邮件转发";
    SendForm->txtTo->Text = "" ;
    SendForm->txtCC->Text = "" ;
    SendForm->txtBCC->Text = "" ;
    SendForm->txtSubject->Text = "Fw: " + NMPOP31->MailMessage->Subject ;

    SendForm->txtBody->Lines->Assign(NMPOP31->MailMessage->Body) ;

    SendForm->lstAttach->Clear() ;

    if (NMPOP31->MailMessage->Attachments->Text != "")
    {
        for(j = 0; j <= NMPOP31->MailMessage->Attachments->Count - 1; i++)
        {
            SendForm->lstAttach->Items->Add(NMPOP31->MailMessage->Attachments->Strings[j]);
        }
    }
    SendForm->Caption = "Forward Mail" ;
    SendForm->ShowModal() ;
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::FormShow(TObject *Sender)
{
    AnsiString Pop3SeverIP;
    AnsiString Pop3Login;
    AnsiString Pop3Password;
    AnsiString SmtpServerIP;
    AnsiString SmtpLogin;
    AnsiString DisplayName;
    AnsiString EmailAddress;

    //用户邮箱设置信息存放在HKEY_LOCAL_MACHINE下的SOFTWARE\\MyMail\\Info中
    Registry=new TRegistry;
    Registry->RootKey=HKEY_LOCAL_MACHINE;
    Registry->OpenKey("SOFTWARE\\MyMail\\Info",true);
    //用户邮箱设置信息读入
    try
    {
        Pop3SeverIP=Registry->ReadString("Pop3SeverIP");
        Pop3Login=Registry->ReadString("Pop3Login");
        Pop3Password=Registry->ReadString("Pop3Password");
        SmtpServerIP=Registry->ReadString("SmtpServerIP");;
        SmtpLogin=Registry->ReadString("SmtpLogin");
        DisplayName=Registry->ReadString("DisplayName");
        EmailAddress=Registry->ReadString("EmailAddress");

        SettingForm->txtPOP3Host->Text=Pop3SeverIP;
        SettingForm->txtPOP3UserID->Text=Pop3Login;
        SettingForm->txtPOP3Password->Text=Pop3Password;
        SettingForm->txtSMTPHost->Text=SmtpServerIP;
        SettingForm->txtSMTPUserID->Text=SmtpLogin;
        SettingForm->txtFromName->Text=DisplayName;
        SettingForm->txtFromAddress->Text=EmailAddress;
    }
    catch(...)  {}

    //设置按钮初始状态
    SPeedButton3->Enabled = false ;
    SPeedButton4->Enabled = false ;
    SPeedButton5->Enabled = false ;
    mnuDelete->Enabled=false;
    mnuReply->Enabled=false;
    mnuForward->Enabled=false;
    lstmail->Enabled = false ;
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::mnuWriteClick(TObject *Sender)
{
    SendForm->ShowModal();
}
//---------------------------------------------------------------------------

void __fastcall TMainForm::mnuReceiveClick(TObject *Sender)
{
    int i;

    // 设置POP3邮件服务器
    NMPOP31->Host = SettingForm->txtPOP3Host->Text ;

    // 设置 POP3 Port 为 110
    NMPOP31->Port = StrToInt(SettingForm->txtPOP3Port->Text);

    // 设置POP3邮件服务器 Login
    NMPOP31->UserID = SettingForm->txtPOP3UserID->Text ;

    // 设置邮件服务器 Password
    NMPOP31->Password = SettingForm->txtPOP3Password->Text;

    NMPOP31->AttachFilePath = "." ;
    NMPOP31->DeleteOnRead = false;  //设置是否保存邮件副本
    NMPOP31->ReportLevel = Status_Basic;
    NMPOP31->TimeOut = 30000;


    // 收件端尝试连接至POP3邮件服务器
    if (!NMPOP31->Connected) NMPOP31->Connect();

    if (NMPOP31->Connected)
    {
        /*//防止二次点击Receive出错
        NMPOP31->Abort();
        NMPOP31->Connect();*/
        //接收邮件
        if (NMPOP31->MailCount > 0)
        {
            lstmail->Items->Clear();
            MailList.Length=NMPOP31->MailCount+2;
            MailLeft=NMPOP31->MailCount;
            for(i = 1; i <= NMPOP31->MailCount; i++)
            {   
                //获取邮件信息
                NMPOP31->GetSummary(i);
                NMPOP31->GetMailMessage(i);
                //记录邮件
                MailList[i]=1;
                //设置附件标记为★
                NewItems=lstmail->Items->Add();
                if (NMPOP31->MailMessage->Attachments->Text != "")
                   NewItems->Caption="★";
                else
                   NewItems->Caption="";
                //添加其他邮件信息
                NewItems->SubItems->Add(NMPOP31->Summary->From);
                NewItems->SubItems->Add(NMPOP31->Summary->Subject);
                NewItems->SubItems->Add(NMPOP31->Summary->Bytes);
            }

            GetMail(1) ;

            SPeedButton3->Enabled = true ;
            SPeedButton4->Enabled = true ;
            SPeedButton5->Enabled = true ;
            mnuDelete->Enabled = true ;
            mnuReply->Enabled = true ;
            mnuForward->Enabled = true ;
            lstmail->Enabled = true;

            StatusBar1->SimpleText = "# of E-Mail: " + IntToStr(NMPOP31->MailCount) ;
        }
        else
        {
            ShowMessage("There is no new E-Mail!");
            StatusBar1->SimpleText = "There is no new E-Mail." ;
        }
    }
    else
    {
        ShowMessage("Connection Fail!");
        StatusBar1->SimpleText = "Status: Connection Fail.";
    }
}
//---------------------------------------------------------------------------

void __fastcall TMainForm::lstmailChange(TObject *Sender, TListItem *Item,
      TItemChange Change)
{
    if(Item->Index<NMPOP31->MailCount && NMPOP31->Connected)
    {
       if(MailList[Item->Index+1]==0)
            GetMail(FindRealtMailNo(Item->Index+1));
       else
            GetMail(Item->Index+1);
    }
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::FormClose(TObject *Sender, TCloseAction &Action)
{
    delete Registry;     //释放内存
    NMPOP31->Abort();    //断开与服务器连接
}
//---------------------------------------------------------------------------

⌨️ 快捷键说明

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