//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include <IdAttachmentFile.hpp>
#include "Unit1.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
	: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
    String stQuery, stCClist, stBCClist, stHost, stPort, stMyEmail, stMyPassword, stFile, stMyClientEmail ;

	stHost = Form1->EditHost->Text ;
	stPort = Form1->EditPort->Text ;
	stMyEmail 	 = Form1->EditMyEmail->Text ;
	stMyPassword = Form1->EditMyPass->Text ;

	stMyClientEmail = Form1->EditMyClient->Text ;
	stCClist  = Form1->EditCC->Text ;
	stBCClist = Form1->EditBCC->Text ;
	stFile    = Form1->EditAttachFile->Text ;

    Form1->IdSMTP1->Disconnect() ;
	Form1->IdSSLIOHandlerSocketOpenSSL1->MaxLineAction      = maException ;
	Form1->IdSSLIOHandlerSocketOpenSSL1->SSLOptions->Method = sslvSSLv23 ;
	Form1->IdSSLIOHandlerSocketOpenSSL1->SSLOptions->Mode   = sslmUnassigned ;
    Form1->IdSSLIOHandlerSocketOpenSSL1->SSLOptions->VerifyDepth = 0 ;
    Form1->IdSSLIOHandlerSocketOpenSSL1->Destination = stHost + ":" + stPort ;
	Form1->IdSSLIOHandlerSocketOpenSSL1->Host        = stHost ;
	Form1->IdSSLIOHandlerSocketOpenSSL1->Port        = StrToInt(stPort) ;

    Form1->IdSMTP1->IOHandler = Form1->IdSSLIOHandlerSocketOpenSSL1 ;
    Form1->IdSMTP1->AuthType = satDefault;

	if (Form1->ComboBoxTLS->ItemIndex==0) {
		Form1->IdSMTP1->UseTLS  = utNoTLSSupport  ;
	} else if (Form1->ComboBoxTLS->ItemIndex==1) {
		Form1->IdSMTP1->UseTLS  = utUseImplicitTLS ;
	} else if (Form1->ComboBoxTLS->ItemIndex==2) {
		Form1->IdSMTP1->UseTLS  = utUseExplicitTLS   ;
	} else if (Form1->ComboBoxTLS->ItemIndex==3) {
		Form1->IdSMTP1->UseTLS  = utUseRequireTLS   ;
	} else {
		ShowMessage("Error: TLS") ;
	} ;

	Form1->IdSMTP1->Host     = stHost ;
	Form1->IdSMTP1->Port     = StrToInt(stPort) ;
	Form1->IdSMTP1->Username = stMyEmail ;
	Form1->IdSMTP1->Password = stMyPassword ;

	Form1->IdMessage1->Clear() ;
	Form1->IdMessage1->ClearHeader() ;
	Form1->IdMessage1->ClearBody() ;
	Form1->IdMessage1->MessageParts->Clear() ;
	Form1->IdMessage1->Body->Clear() ;
	Form1->IdMessage1->CharSet = "utf-8";
	Form1->IdMessage1->From->Address = stMyEmail ;
	Form1->IdMessage1->Recipients->EMailAddresses = stMyClientEmail ;

	Form1->IdMessage1->CCList->EMailAddresses     = stCClist ;
	Form1->IdMessage1->BccList->EMailAddresses    = stBCClist;

	Form1->IdMessage1->Subject = Form1->EditSubject->Text ;
	Form1->IdMessage1->Body->Add( Form1->Memo1->Lines->Text ) ;

	if ( stFile.Length() > 1 ) {
		Form1->IdMessage1->ContentType = "multipart/form-data";
		TIdAttachmentFile(IdMessage1->MessageParts, stFile);  // #include <IdAttachmentFile.hpp>
	} ;
	try {
		IdSMTP1->Connect() ;
		Form1->IdSMTP1->Authenticate();
		Form1->IdSMTP1->Send(IdMessage1);
		Form1->IdSMTP1->Disconnect();
		ShowMessage("eMail sent.") ;
	}  catch(...) {
		Form1->IdSMTP1->Disconnect();
		ShowMessage("eMail sent failed.") ;
	} ;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::EditAttachFileEnter(TObject *Sender)
{
    this->SpeedButtonAttachFileClick(Sender) ;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormShow(TObject *Sender)
{
	Form1->EditHost->Text = "smtp.gmail.com" ;
	Form1->ComboBoxTLS->ItemIndex = 1 ;
	Form1->EditPort->Text = "465" ;
	Form1->EditMyEmail->Text = "" ;
	Form1->EditMyPass->Text = "" ;
	Form1->EditMyClient->Text = "" ;
	Form1->EditCC->Text = "" ;
	Form1->EditBCC->Text = "" ;
	Form1->EditAttachFile->Text = "" ;
	Form1->EditSubject->Text = "" ;
	Form1->Memo1->Lines->Clear() ;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ComboBoxTLSCloseUp(TObject *Sender)
{
	if (Form1->ComboBoxTLS->ItemIndex==1) {
		Form1->EditPort->Text = "465" ;
	} else if (Form1->ComboBoxTLS->ItemIndex==2) {
		Form1->EditPort->Text = "587" ;
	} ;
	Form1->EditPort->SelectAll() ;
	Form1->EditPort->SetFocus() ;
}

//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
int __fastcall TForm1::countStr(String stText, String stCheck)
{
	int i, iLength ;
	iLength = stText.Length() ;
	i   = 1 ;
	while( iLength >= i ) {
		if (stText.SubString(i,1)==stCheck) {
			break ;
		};
		i++;
	};
	if (i>iLength) {
		i = 0 ;
	};
	return i ;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::EditMyEmailKeyUp(TObject *Sender, WORD &Key, TShiftState Shift)

{
	int i, iL ;
	i = this->countStr( this->EditHost->Text.Trim(), "." ) ;
	iL = this->EditHost->Text.Trim().Length() ;

	String stSvr ;
	stSvr = "@" + this->EditHost->Text.Trim().SubString(i+1, iL - i ) ;

	if ( this->EditHost->Text.Trim().SubString(this->EditHost->Text.Trim().Length() -3,4) !=
		 this->EditMyEmail->Text.Trim().SubString(this->EditMyEmail->Text.Trim().Length() -3,4) )
	{
		this->EditMyEmail->Text = this->EditMyEmail->Text + stSvr ;
	}

}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButtonAttachFileClick(TObject *Sender)
{
	if (Form1->OpenDialog1->Execute()) {
		Form1->EditAttachFile->Text = Form1->OpenDialog1->FileName ;
	} ;
}
//---------------------------------------------------------------------------
