I am following this guide on using C++ to send emails. I followed the instructions and it works perfectly fine
This is the sample code on how to send emails using the program , I have changed the email address and password for security purposes
#include "stdafx.h"
#include <iostream>
#include "easendmailobj.tlh"
using namespace EASendMailObjLib;
using namespace std;
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("[email protected]");
// Add recipient email address
oSmtp->AddRecipientEx( _T("[email protected]"), 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 "[email protected]", then the user should be "[email protected]"
oSmtp->UserName = _T("[email protected]");
oSmtp->Password = _T("somepassword");
_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();
int x;
cin>>x;
return 0;
}
When i attempt to copy paste this code into my program and make it as a function which i can use , I get the following error
error C3861: '_T': identifier not found
This is what my functions looks like
void DriverClass::send_email(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("randomemail1.com");
// Add recipient email address
oSmtp->AddRecipientEx( _T("[email protected]"), 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 "[email protected]", then the user should be "[email protected]"
oSmtp->UserName = _T("[email protected]");
oSmtp->Password = _T("randompassword");
_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();
int x;
cin>>x;
}
I think it has something to do with the fact that i cant pass in int argc
and _TCHAR*argc[]
as arguements , How do make it into a function which i can use and how do i solve it ???
Add:
#include <tchar.h>
as that is where the _T
(and _TCHAR
) macro is defined. See Generic-Text Mappings in Tchar.h.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With