//---------------------------------------------------------------------------
#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