gpt4 book ai didi

c++ - 使用字符串变量而不是字符串文字会导致错误

转载 作者:搜寻专家 更新时间:2023-10-31 01:06:01 25 4
gpt4 key购买 nike

我正在关注这个 guide在 c++ 中发送电子邮件。您可以在 here 下载头文件:easendmailobj.tlheasendmail 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

最佳答案

一个字符串文字和一个 std::string是两个完全不同的东西。通常当函数 f期望一个 const char*你可以传递给它一个字符串文字或者你可以使用方法 c_str()像这样的字符串 f(email.c_str()) .

但是这里的问题是_T不是函数。它是一个宏,它只是附加一个 L。作为字符串文字的前缀。您无法将其用于 std::string .您将需要使用一些函数进行转换。

或者定义一个与_T 功能相同的宏并决定使用 wstringstring取决于是否 UNICODE被定义为。尝试通读代码,看看 _T 的作用是什么?做,你应该明白我的意思。

关于c++ - 使用字符串变量而不是字符串文字会导致错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21624429/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com