ivan
  • ivan
  • 100% (Exalted)
  • Administration Topic Starter
14 years ago
VC++ Example

Gmail SMTP server address is "smtp.gmail.com". It requires SSL or TLS connection, and you should use your Gmail email address as the user name for ESMTP authentication. For example: your email is "gmailid@gmail.com", and then the user name should be "gmailid@gmail.com".

// The following example codes demonstrate sending email message using Gmail SMTP server
// To get full sample projects, please download and install EASendMail on your machine.
// To run it correctly, please change SMTP server, user, password, sender, recipient value to yours


#include "stdafx.h" 

#include "easendmailobj.tlh" 
using namespace EASendMailObjLib; 

int _tmain(int argc, _TCHAR* argv[]) 
{ 
    ::CoInitialize( NULL ); 

    IMailPtr oSmtp = NULL; 
    oSmtp.CreateInstance( "EASendMailObj.Mail"); 
    oSmtp->LicenseCode = _T("TryIt"); 

    // Set your gmail email address
    oSmtp->FromAddr = _T("gmailid@gmail.com"); 

    // Add recipient email address
    oSmtp->AddRecipientEx( _T("support@emailarchitect.net"), 0 ); 

    // Set email subject
    oSmtp->Subject = _T("simple email from Visual C++ with gmail account"); 

    // Set email body
    oSmtp->BodyText = _T("this is a test email sent from Visual C++ project with Gmail"); 

    // Gmail SMTP server address
    oSmtp->ServerAddr = _T("smtp.gmail.com"); 

    // If you want to use direct SSL 465 port,
    // Please add this line, otherwise TLS will be used.
    // oSmtp->ServerPort = 465;

    // detect SSL/TLS automatically
    oSmtp->SSL_init(); 

    // Gmail user authentication should use your
    // Gmail email address as the user name.
    // For example: your email is "gmailid@gmail.com", then the user should be "gmailid@gmail.com"
    oSmtp->UserName = _T("gmailid@gmail.com"); 
    oSmtp->Password = _T("yourpassword"); 

    _tprintf(_T("Start to send email via gmail account ...\r\n" )); 

    if( oSmtp->SendMail() == 0 ) 
    { 
        _tprintf( _T("email was sent successfully!\r\n")); 
    } 
    else 
    { 
        _tprintf( _T("failed to send email with the following error: %s\r\n"), 
            (const TCHAR*)oSmtp->GetLastErrDescription()); 
    } 

    if( oSmtp != NULL ) 
        oSmtp.Release(); 

    return 0; 
} 
Click here to read original topic - full version ... 

If you have any comments or questions about above example codes, please add your comments here.
kmtompkins
10 years ago
hi i am using the code that u made for Visual C++ - Send Email using Gmail Account i have changed a few thing because i was getting errors and i was able to get the code to run but at the line:
if (oSmtp->SendMail() == 0)

it crashes, the program just stops responding


// The following example codes demonstrate sending email message using Gmail SMTP server
// To get full sample projects, please download and install EASendMail on your machine.
// To run it correctly, please change SMTP server, user, password, sender, recipient value to yours

#define _AFXDLL
#include "stdafx.h" 

#include "easendmailobj.tlh" 
using namespace EASendMailObjLib;

int _tmain(int argc, char* argv[])
{
	::CoInitialize(NULL);

	IMailPtr oSmtp = NULL;
	oSmtp.CreateInstance("EASendMailObj.Mail");
	oSmtp->LicenseCode = ("TryIt");

	// Set your gmail email address
	oSmtp->FromAddr = ("gmailid@gmail.com");

	// Add recipient email address
	oSmtp->AddRecipientEx(("gmailid@gmail.com"), 0);

	// Set email subject
	oSmtp->Subject = ("simple email from Visual C++ with gmail account");

	// Set email body
	oSmtp->BodyText = ("this is a test email sent from Visual C++ project with Gmail");

	// Gmail SMTP server address
	oSmtp->ServerAddr = ("smtp.gmail.com");

	// If you want to use direct SSL 465 port,
	// Please add this line, otherwise TLS will be used.
	// oSmtp->ServerPort = 465;

	// detect SSL/TLS automatically
	oSmtp->SSL_init();

	// Gmail user authentication should use your
	// Gmail email address as the user name.
	// For example: your email is "gmailid@gmail.com", then the user should be "gmailid@gmail.com"
	oSmtp->UserName = ("kmtompkinscode@gmail.com");
	oSmtp->Password = ("password here");

	printf("Start to send email via gmail account ...\r\n");

	if (oSmtp->SendMail() == 0)//program crashes here
	{
		printf("email was sent successfully!\r\n");
	}
	else
	{
		printf("failed to send email with the following error: %s\r\n"),
			(const TCHAR*)oSmtp->GetLastErrDescription();
	}

	if (oSmtp != NULL)
		oSmtp.Release();

	return 0;
}

ivan
  • ivan
  • 100% (Exalted)
  • Administration Topic Starter
10 years ago
Please try to use "Try-catch" to catch the error information.
It is likely that your trial period is expired, you can re-download the installer from our site and re-install it to get extend trial period.


int _tmain(int argc, char* argv[])
{
    ::CoInitialize(NULL);

  try
  {
    IMailPtr oSmtp = NULL;
    oSmtp.CreateInstance("EASendMailObj.Mail");
    oSmtp->LicenseCode = ("TryIt");

    // Set your gmail email address
    oSmtp->FromAddr = ("gmailid@gmail.com");

    // Add recipient email address
    oSmtp->AddRecipientEx(("gmailid@gmail.com"), 0);

    // Set email subject
    oSmtp->Subject = ("simple email from Visual C++ with gmail account");

    // Set email body
    oSmtp->BodyText = ("this is a test email sent from Visual C++ project with Gmail");

    // Gmail SMTP server address
    oSmtp->ServerAddr = ("smtp.gmail.com");

    // If you want to use direct SSL 465 port,
    // Please add this line, otherwise TLS will be used.
    // oSmtp->ServerPort = 465;

    // detect SSL/TLS automatically
    oSmtp->SSL_init();

    // Gmail user authentication should use your
    // Gmail email address as the user name.
    // For example: your email is "gmailid@gmail.com", then the user should be "gmailid@gmail.com"
    oSmtp->UserName = ("gmailid@gmail.com");
    oSmtp->Password = ("****");

    printf("Start to send email via gmail account ...\r\n");

    if (oSmtp->SendMail() == 0)//program crashes here
    {
        printf("email was sent successfully!\r\n");
    }
    else
    {
        printf("failed to send email with the following error: %s\r\n"),
            (const TCHAR*)oSmtp->GetLastErrDescription();
    }

    if (oSmtp != NULL)
        oSmtp.Release();
  
   }catch( _com_error &ep )
   {
        _tprintf( _T("Error: %s"), (const TCHAR*)ep.Description());
   }

   return 0;
}
kmtompkins
10 years ago
ok u have a extra } with out it the program runs but it still crashes in the same spot


// The following example codes demonstrate sending email message using Gmail SMTP server
// To get full sample projects, please download and install EASendMail on your machine.
// To run it correctly, please change SMTP server, user, password, sender, recipient value to yours

#define _AFXDLL
#include "stdafx.h" 

#include "easendmailobj.tlh" 
using namespace EASendMailObjLib;

int _tmain(int argc, char* argv[])
{
	::CoInitialize(NULL);

	try
	{
		IMailPtr oSmtp = NULL;
		oSmtp.CreateInstance("EASendMailObj.Mail");
		oSmtp->LicenseCode = ("TryIt");

		// Set your gmail email address
		oSmtp->FromAddr = ("gmailid@gmail.com");

		// Add recipient email address
		oSmtp->AddRecipientEx(("gmailid@gmail.com"), 0);

		// Set email subject
		oSmtp->Subject = ("simple email from Visual C++ with gmail account");

		// Set email body
		oSmtp->BodyText = ("this is a test email sent from Visual C++ project with Gmail");

		// Gmail SMTP server address
		oSmtp->ServerAddr = ("smtp.gmail.com");

		// If you want to use direct SSL 465 port,
		// Please add this line, otherwise TLS will be used.
		// oSmtp->ServerPort = 465;

		// detect SSL/TLS automatically
		oSmtp->SSL_init();

		// Gmail user authentication should use your
		// Gmail email address as the user name.
		// For example: your email is "gmailid@gmail.com", then the user should be "gmailid@gmail.com"
		oSmtp->UserName = ("gmailid@gmail.com");
		oSmtp->Password = ("****");

		printf("Start to send email via gmail account ...\r\n");

		if (oSmtp->SendMail() == 0)//program crashes here
		{
			printf("email was sent successfully!\r\n");
		}
		else
		{
			printf("failed to send email with the following error: %s\r\n"),
				(const TCHAR*)oSmtp->GetLastErrDescription();
		}

		if (oSmtp != NULL)
			oSmtp.Release();
	}

	catch (_com_error &ep)
	{
		_tprintf(_T("Error: %s"), (const TCHAR*)ep.Description());
	}

	return 0;
}
ivan
  • ivan
  • 100% (Exalted)
  • Administration Topic Starter
10 years ago
please have a try with the following codes:

this statement has a problem:

printf("failed to send email with the following error: %s\r\n"),
(const TCHAR*)oSmtp->GetLastErrDescription();

Did you re-install it?
It is likely that your trial period is expired, you can re-download the installer from our site and re-install it to get extend trial period.


int _tmain(int argc, char* argv[])
{
	::CoInitialize(NULL);

	try
	{
		IMailPtr oSmtp = NULL;
		oSmtp.CreateInstance("EASendMailObj.Mail");
		oSmtp->LicenseCode = ("TryIt");

		// Set your gmail email address
		oSmtp->FromAddr = ("gmailid@gmail.com");

		// Add recipient email address
		oSmtp->AddRecipientEx(("gmailid@gmail.com"), 0);

		// Set email subject
		oSmtp->Subject = ("simple email from Visual C++ with gmail account");

		// Set email body
		oSmtp->BodyText = ("this is a test email sent from Visual C++ project with Gmail");

		// Gmail SMTP server address
		oSmtp->ServerAddr = ("smtp.gmail.com");

		// If you want to use direct SSL 465 port,
		// Please add this line, otherwise TLS will be used.
		// oSmtp->ServerPort = 465;

		// detect SSL/TLS automatically
		oSmtp->SSL_init();

		// Gmail user authentication should use your
		// Gmail email address as the user name.
		// For example: your email is "gmailid@gmail.com", then the user should be "gmailid@gmail.com"
		oSmtp->UserName = ("gmailid@gmail.com");
		oSmtp->Password = ("****");

		printf("Start to send email via gmail account ...\r\n");

		if (oSmtp->SendMail() == 0)//program crashes here
		{
			_tprintf(_T("email was sent successfully!\r\n"));
		}
		else
		{
			_tprintf(_T("failed to send email with the following error: %s\r\n"),
				(const TCHAR*)oSmtp->GetLastErrDescription());
		}

		if (oSmtp != NULL)
			oSmtp.Release();
	}

	catch( _com_error &ep )
	{
		_tprintf( _T("Error: %s"), (const TCHAR*)ep.Description());
	}

	return 0;
}

EXPLORE TUTORIALS

© All Rights Reserved, AIFEI Software Limited & AdminSystem Software Limited.