//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "UnitMain.h"
#include "UnitConfigure.h"
#include "UnitAccount.h"
#include "UnitGlobe.h"
#include <registry.hpp>
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "IdBaseComponent"
#pragma link "IdComponent"
#pragma link "IdMessage"
#pragma link "IdMessageClient"
#pragma link "IdPOP3"
#pragma link "IdSMTP"
#pragma link "IdTCPClient"
#pragma link "IdTCPConnection"
#pragma resource "*.dfm"
TFormMain *FormMain;
//---------------------------------------------------------------------------
__fastcall TFormMain::TFormMain(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::SpeedButtonAddClick(TObject *Sender)
{
bool NeedAdd = false;
if ( OpenDialog1->Execute())
{
AnsiString tmpName = OpenDialog1->FileName ;
for (int j= ListBox1->Items->Count -1 ; j>= 0 ; j--)
{
if (ListBox1->Items->Strings[j] == tmpName)
{
AnsiString hintName = tmpName + " 已经存在,要重新加入码?";
if (MessageBox(NULL, hintName.c_str(), "确 认", MB_OKCANCEL |MB_ICONWARNING)== IDOK )
{
ListBox1->Items->Add(tmpName);
}
NeedAdd = true;
break;
}
}
if (!NeedAdd)
ListBox1->Items->Add(tmpName);
}
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::SpeedButtonDelClick(TObject *Sender)
{
if (ListBox1->Items->Count<= 0 )
return;
for (int i = ListBox1->Items->Count -1; i>=0 ; i--)
{
if (ListBox1->Selected
)
{
AnsiString tmpS = "确实要删除" + ListBox1->Items->Strings ;
if (MessageBox(NULL, tmpS.c_str(), "确 认", MB_OKCANCEL |MB_ICONWARNING)== IDOK )
{
ListBox1->Items->Delete(i);
}
}
} //for
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::BitBtnSendClick(TObject *Sender)
{
//读取配置文件
if ( FileExists(ExtractFilePath(ParamStr(0)) + "Mail.ini"))
{
MailSetupFile = new TIniFile(ExtractFilePath(ParamStr(0)) + "Mail.ini") ;
try
{
ReadSetup(MailSetupFile);
}
__finally
{
MailSetupFile->Free();
}
}
if (EditToAddress->Text == "")
{
MessageBox(NULL,"收件人没有填写,请正确填写收件人地址","警 告",MB_OK |MB_ICONWARNING);
return ;
}
TIdEMailAddressItem* AnItem;
TStringList *StrAddress;
StrAddress = new TStringList();
try
{
Devide_Colon_Address(EditToAddress->Text, *StrAddress);
for (int i = 0 ; i<StrAddress->Count; i++)
{
AnItem = IdMessage->Recipients->Add();
AnItem->Address = StrAddress->Strings;
}
if (EditToCC->Text !="")
{
StrAddress->Clear();
Devide_Colon_Address(EditToCC->Text , *StrAddress);
for (int i = 0 ; i < StrAddress->Count; i++)
{
AnItem = IdMessage->CCList->Add();
AnItem->Address = StrAddress->Strings;
}
} // if (EditToCC->Text !="")
if (EditToBCC->Text != "")
{
StrAddress->Clear();
Devide_Colon_Address(EditToBCC->Text , *StrAddress);
for (int i = 0 ; i < StrAddress->Count; i++)
{
AnItem = IdMessage->BccList->Add();
AnItem->Address = StrAddress->Strings;
}
} // if (EditToBCC->Text != "")
}
__finally
{
StrAddress->Free();
}
if (EditSubject->Text == "" )
{
if ( MessageBox(NULL,"主题是空的 是否要以空的主题发送","提 示", MB_OKCANCEL|MB_ICONQUESTION ) != IDOK )
return ;
}
// IdMessage->Body->Assign(MemoContent->Lines);
IdMessage->UseNowForDate = true;
IdMessage->From->Text = UserEMail;
IdMessage->ReplyTo->EMailAddresses = UserReplyEMail ;
IdMessage->ReceiptRecipient->Text = UserEMail;
IdMessage->Subject = EditSubject->Text ;
IdMessage->Recipients->EMailAddresses = EditToAddress->Text ;
//IdMessage->CCList->EMailAddresses = EditToCC->Text;
// IdMessage->BccList->EMailAddresses = EditToBCC->Text;
IdMessage->Priority = TIdMessagePriority(ComboBox1->ItemIndex) ;
Process_Body(IdMessage,MemoContent->Lines,ListBox1->Items );
IdSMTP->Host = SMTPSeverName ;
if (IdSMTP->Host=="")
{
MessageBox(NULL,"没有正确设置服务器主机,请设置","提 示", MB_OK|MB_ICONSTOP ) ;
return ;
}
IdSMTP->Port = SMTPSeverPort ;
if ( SMTPVerify )
IdSMTP->AuthenticationType = atLogin ;
else
IdSMTP->AuthenticationType = atNone ;
IdSMTP->Username = SMTPSeverUser ;
IdSMTP->Password = SMTPSeverPassword ;
IdSMTP->Connect(IdTimeoutDefault);
try
{
IdSMTP->Send(IdMessage);
}
__finally
{
IdSMTP->Disconnect();
}
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::IdSMTPConnected(TObject *Sender)
{
StatusBar1->SimpleText = "状态:已经成功连接服务器主机";
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::IdSMTPWork(TObject *Sender, TWorkMode AWorkMode,
const int AWorkCount)
{
StatusBar1->SimpleText = "状态:正在发送邮件";
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::BitBtnSetupClick(TObject *Sender)
{
FormConfigure = new TFormConfigure(this) ;
FormConfigure->ShowModal();
FormConfigure->Free();
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::Button1Click(TObject *Sender)
{
TStringList *SS;
SS = new TStringList();
try
{
Devide_Colon_Address(EditToAddress->Text, *SS);
for (int Count = 0 ; Count < SS->Count; Count++)
{
MemoContent->Lines->Add(SS->Strings[Count]);
}
}
__finally
{
SS->Free();
}
Process_Body(IdMessage,MemoContent->Lines,ListBox1->Items );
}
//---------------------------------------------------------------------------
bool __fastcall TFormMain::Ext_to_MIME(String Extension, String& MIME_String)
{
TRegistry *SysReg;
bool Result = false;
SysReg = new TRegistry();
try
{
SysReg->RootKey = HKEY_CLASSES_ROOT;
AnsiString tmpstr = "//"+ Extension ;
if ( SysReg->KeyExists(tmpstr))
{
SysReg->OpenKey(tmpstr, false);
if (SysReg->ValueExists("Content Type"))
{
MIME_String = SysReg->ReadString("Content Type");
Result = true;
}
else
Result = false;
SysReg->CloseKey();
}
else
Result = false;
}
__finally
{
SysReg->Free();
}
return Result;
}
//----------------------------------------------------------------------------
bool __fastcall TFormMain:rocess_Body(TIdMessage* Message , TStrings* Content,
TStrings* Attchment)
{
TIdText* Msg_Text ;
TIdAttachment* Msg_File;
String TheFile, Ext, MIME ;
Msg_Text = new TIdText(Message->MessageParts,Content);
Msg_Text->ContentType = "text/plain";
Msg_Text = new TIdText(Message->MessageParts,Content);
Msg_Text->ContentType = "text/plain";
if (Attchment->Count >0 )
{
for (int index= 0; index < Attchment->Count; index++ )
{
TheFile = Attchment->Strings[index];
Ext = ExtractFileExt(TheFile);
bool Res = Ext_to_MIME(Ext, MIME);
Msg_File = new TIdAttachment(Message->MessageParts, TheFile);
Msg_File->ExtraHeaders->Values["Content-Location"] = TheFile;
if ( Res )
Msg_File->ContentType = MIME;
}
}
return true;
}
//----------------------------------------------------------------------------
bool __fastcall TFormMain::ReadSetup(TIniFile* IniFile)
{
SMTPSeverName = IniFile->ReadString("Servers" ,"SMTP Server Name" ,"");
SMTPSeverPort = IniFile->ReadInteger("Servers","SMTP Server Port", 25);
SMTPVerify = IniFile->ReadBool("Servers", "SMTP Verify",false);
SMTPSeverUser = IniFile->ReadString("Servers", "SMTP User Account","");
SMTPSeverPassword = IniFile->ReadString("Servers", "SMTP User Password","");
POP3SeverUser = IniFile->ReadString("Sevrers","POP3 User Account" ,"");
POP3SeverPassword = IniFile->ReadString("Sevrers","User Password" ,"");
UserEMail = IniFile->ReadString("Persons","EMail" ,"");
UserReplyEMail = IniFile->ReadString("Persons","Reply EMail" ,"");
}
//----------------------------------------------------------------------------
void __fastcall TFormMain::FormCreate(TObject *Sender)
{
if ( FileExists(ExtractFilePath(ParamStr(0)) + "Mail.ini"))
{
MailSetupFile = new TIniFile(ExtractFilePath(ParamStr(0)) + "Mail.ini") ;
try
{
ReadSetup(MailSetupFile);
}
__finally
{
MailSetupFile->Free();
}
}
ComboBox1->ItemIndex = (int)IdMessage->Priority;
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::IdSMTPStatus(TObject *ASender,
const TIdStatus AStatus, const AnsiString AStatusText)
{
StatusBar1->SimpleText = "状态:" + AStatusText ;
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::ClearClick(TObject *Sender)
{
MemoContent->Clear();
}
//---------------------------------------------------------------------------