/************************************************************************************************************
Methods:
1. SendMail()		send mail in blocking way
2. SendQMail()		send mail in non-blocking way, use service

Properties:
1. PutBCC()			contains the address to whom, the blind carbon copy should be sent. for multiple
					recipients, addresses will be semicolon (;) separated
2. PutBody()		the property is to assign the body of the mail. for a long body one can call it multiple
					times. embedded images can also be sent within the body using a specified tag i.e.
					<AdvImageMail: c:\test\bubbles.bmp|0>. whereas the ‘0’ specifies that do not delete the
					file after the mail is sent. A ‘1’ here tells the component to delete the file after the
					mail is sent
3. PutCC()			contains the address to whom, the carbon copy of the mail should be sent. for multiple
					recipients, addresses will be semicolon (;) separated
4. GetErrorReason()	return number of files to be uploaded
5. GetIsError()		returns 1 if any error occur during the request otherwise 0
6. PutLogFile()		if the user wants the logging to be performed in user defined path then he can specify
					his own file path for logging
7. PutLogging()		used to specifying what type of logging should be performed. valid values are
					0 No Logging
					1 Error Logging (default)
					2 Complete logging
8. PutPort()		it is the port by which the mail should be sent if no port is specified then a port 25
					is used for sending the mails
9. PutReceipt()		it is used, if mail sender wants to get information back, when mail receiver reads the
					mail message. if this value is set TRUE, then mail sender will get information that the 
					mail receiver reads the mail, otherwise mail sender will not be informed. valid values are
					TRUE
					FALSE (default)
10. PutServer()		it is the SMTP server through which the mail will be sent. if it is not specified then the
					mail will be sent by any of the default servers
11. PutSubject()	it is the subject of the mail
12. PutType()		it defines the mail type. valid values are
					HTML (default)
					TEXT
************************************************************************************************************/

/***********************************/
/*include all required header files*/
/***********************************/
#include <stdio.h>
#include <conio.h>

/************************************************************************/
/*import AdvDirectory COM library here to get its methods and properties*/
/************************************************************************/
#import "C:\AdvSMTP.dll" no_namespace

/*************************/
/*prototype for functions*/
/*************************/
void SendMail();//SendMail function
void SendQMail();//SendQMail function

/**************/
/*main program*/
/**************/
void main(void)
{
	char Ch;
	do
	{
		printf("\r\nSendMail	[S]");
		printf("\r\nSendQMail	[M]");
		printf("\r\nQuit		[Q]");
		printf("\r\n\r\nEnter Choice = ");
		scanf("%c", &Ch);
		getchar();
		Ch = toupper(Ch);
		switch (Ch)
		{
			case 'S':SendMail();break;//call SendMail function
			case 'M':SendQMail();break;//call SendQMail function
			case 'Q':printf("\r\nGood Bye\r\n");break;
			default: printf("\r\nError: Please enter valid choice\r\n");
		}
	}
	while (Ch != 'Q');
}

/******************************************************/
/*SendMail function: The function is used to send mail*/
/******************************************************/
void SendMail()
{
	char m_Server[256], m_From[256], m_To[256], m_CC[256], m_BCC[256], m_Subject[256], m_Body[256], m_Attachment[256];//define charecter buffer of 256 to server name, port, from address, cc, bcc, subject, to address, body of mail message and mail attachment if any
	long l_Port, l_Delete;//define server port and mail attachement delete flag
	printf("\r\nEnter Server Name = ");
	gets(m_Server);//read server name
	printf("\r\nEnter Server Port = ");
	scanf("%d", &l_Port);//read server port
	getchar();
	printf("\r\nEnter From Address = ");
	gets(m_From);//read from
	printf("\r\nEnter To Address = ");
	gets(m_To);//read to
	printf("\r\nEnter CC Address = ");
	gets(m_CC);//read cc
	printf("\r\nEnter BCC Address = ");
	gets(m_BCC);//read bcc
	printf("\r\nEnter Subject = ");
	gets(m_Subject);//read subject
	printf("\r\nEnter Body = ");
	gets(m_Body);//read body
	printf("\r\nEnter Attachment Complete Path = ");
	gets(m_Attachment);//read complete attachment path
	printf("\r\nEnter Attachment Delete Flag [1/0] = ");
	scanf("%d", &l_Delete);//read attachment delete flag
	getchar();
	try//for COM library call, try...catch is used to handle exceptions
	{
		ISMTPPtr obj;//take smart pointer object obj for COM library
		CoInitialize(NULL);//Initializes the COM library on the current thread
		obj = ISMTPPtr("AdvSMTP.SMTP");
		obj->PutServer(m_Server);//set server name property
		obj->PutPort(l_Port);//set server port property
		obj->PutCc(m_CC);//set cc property
		obj->PutBcc(m_BCC);//set bcc property
		obj->PutSubject(m_Subject);//set subject property
		obj->PutBody(m_Body);//set body property
		if (l_Delete)
			obj->MimeAttach(m_Attachment, L"Delete");
		else
			obj->MimeAttach(m_Attachment, L"NoDelete");
		HRESULT hr = obj->SendMail(m_From, m_To);//call SendMail method of COM library
		if (!SUCCEEDED(hr))//if COM call is not successfull then display error message and return
		{
			printf("\r\nError: %s\r\n", (char*)obj->GetErrorReason());//display error message
			return;
		}
		//call GetIsError() property of COM library and check its value, if its value is 1, then
		//GetErrorReason() property of COM will contain error message, for which this error occured
		if (obj->GetIsError() == 1)
			printf("\r\nError: %s\r\n", (char*)obj->GetErrorReason());//display error message
		else
			printf("\r\nSuccessfully Send\r\n");//display successfull message
	}
	catch (_com_error &error)//catch exception here
	{
		printf(TEXT("%S[%X]"), error.ErrorMessage(), error.Error());//display error message
	}
}

/***********************************************************************/
/*SendQMail function: The function is used to send mail through service*/
/***********************************************************************/
void SendQMail()
{
	char m_Server[256], m_From[256], m_To[256], m_CC[256], m_BCC[256], m_Subject[256], m_Body[256], m_Attachment[256];//define charecter buffer of 256 to server name, port, from address, cc, bcc, subject, to address, body of mail message and mail attachment if any
	long l_Port, l_Delete;//define server port and mail attachement delete flag
	printf("\r\nEnter Server Name = ");
	gets(m_Server);//read server name
	printf("\r\nEnter Server Port = ");
	scanf("%d", &l_Port);//read server port
	getchar();
	printf("\r\nEnter From Address = ");
	gets(m_From);//read from
	printf("\r\nEnter To Address = ");
	gets(m_To);//read to
	printf("\r\nEnter CC Address = ");
	gets(m_CC);//read cc
	printf("\r\nEnter BCC Address = ");
	gets(m_BCC);//read bcc
	printf("\r\nEnter Subject = ");
	gets(m_Subject);//read subject
	printf("\r\nEnter Body = ");
	gets(m_Body);//read body
	printf("\r\nEnter Attachment Complete Path = ");
	gets(m_Attachment);//read complete attachment path
	printf("\r\nEnter Attachment Delete Flag [1/0] = ");
	scanf("%d", &l_Delete);//read attachment delete flag
	getchar();
	try//for COM library call, try...catch is used to handle exceptions
	{
		ISMTPPtr obj;//take smart pointer object obj for COM library
		CoInitialize(NULL);//Initializes the COM library on the current thread
		obj = ISMTPPtr("AdvSMTP.SMTP");
		obj->PutServer(m_Server);//set server name property
		obj->PutPort(l_Port);//set server port property
		obj->PutCc(m_CC);//set cc property
		obj->PutBcc(m_BCC);//set bcc property
		obj->PutSubject(m_Subject);//set subject property
		obj->PutBody(m_Body);//set body property
		if (l_Delete)
			obj->MimeAttach(m_Attachment, L"Delete");
		else
			obj->MimeAttach(m_Attachment, L"NoDelete");
		HRESULT hr = obj->SendQMail(m_From, m_To);//call SendMail method of COM library
		if (!SUCCEEDED(hr))//if COM call is not successfull then display error message and return
		{
			printf("\r\nError: %s\r\n", (char*)obj->GetErrorReason());//display error message
			return;
		}
		//call GetIsError() property of COM library and check its value, if its value is 1, then
		//GetErrorReason() property of COM will contain error message, for which this error occured
		if (obj->GetIsError() == 1)
			printf("\r\nError: %s\r\n", (char*)obj->GetErrorReason());//display error message
		else
			printf("\r\nSuccessfully Send\r\n");//display successfull message
	}
	catch (_com_error &error)//catch exception here
	{
		printf(TEXT("%S[%X]"), error.ErrorMessage(), error.Error());//display error message
	}
}
