我正在按照本指南使用 c++ 发送电子邮件。您可以下载头文件:easendmailobj.tlh
并easendmail object
在此处
#include "stdafx.h"
#include <iostream>
#include "easendmailobj.tlh"
using namespace EASendMailObjLib;
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
//string email = "randomemail1@gmail.com";
::CoInitialize( NULL );
IMailPtr oSmtp = NULL;
oSmtp.CreateInstance( "EASendMailObj.Mail");
oSmtp->LicenseCode = _T("TryIt");
// Set your gmail email address
oSmtp->FromAddr = _T("randomemail1@gmail.com");
// Add recipient email address
oSmtp->AddRecipientEx( _T("randomemail2s@hotmail.com"), 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("somerandomemail@gmail.com");
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;
}
为什么当我尝试使用字符串变量而不是“”时出现错误???
#include "stdafx.h"
#include <iostream>
#include "easendmailobj.tlh"
using namespace EASendMailObjLib;
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
string email = "randomemail1@gmail.com": //string variables
::CoInitialize( NULL );
IMailPtr oSmtp = NULL;
oSmtp.CreateInstance( "EASendMailObj.Mail");
oSmtp->LicenseCode = _T("TryIt");
// Set your gmail email address
oSmtp->FromAddr = _T(email); // string variable instead of " " , error is here
// Add recipient email address
oSmtp->AddRecipientEx( _T("randomemail2s@hotmail.com"), 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("somerandomemail@gmail.com");
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;
}
我收到以下错误
Error C2065: 'Lemail' : undeclared identifier