使用字符串变量而不是字符串文本会导致错误
Using string variables instead of string literals causes error
我正在按照本指南用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 ...rn" ));
if( oSmtp->SendMail() == 0 )
{
_tprintf( _T("email was sent successfully!rn"));
}
else
{
_tprintf( _T("failed to send email with the following error: %srn"),
(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 ...rn" ));
if( oSmtp->SendMail() == 0 )
{
_tprintf( _T("email was sent successfully!rn"));
}
else
{
_tprintf( _T("failed to send email with the following error: %srn"),
(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
相同的操作,并根据是否定义了UNICODE
决定使用wstring
或string
。尝试通读代码以及_T
做什么,您应该理解我的意思。
是否定义了UNICODE
。
如果是,请使用std::wstring
而不是字符串。此外,删除包装字符串变量_T
并添加.c_str()
如果这是标准的Microsoft垃圾(更准确地说,一个失败的实验——他们想要实现的是值得称赞,但他们实际上最终得到的是无用的额外复杂性),你最好摆脱它:函数是 main
(不是_tmain
),使用char
(或wchar_t
,具体取决于你想要的,但它必须在声明中char
main
)而不是_TCHAR
,忘记_T
。
至于眼前的问题,_T
是一个宏,它将仅适用于字符串文字;这就是它的设计方式。如果您在其他任何东西上使用它,它可能会扩展到类似 Lemail
,这当然不会知道。
_T() 的无效用法;它适用于字符串文字。
所以那条线应该是;
oSmtp->FromAddr = email.c_str();
_T
的定义:
#if defined(_UNICODE)
#define _T(x) L ##x
#else
#define _T(x) x
#endif
正如你所看到的,它只是将L
附加到它里面传递的任何内容前面。因此,如果您传入一个字符串,例如_T("HELLO WORLD")
如果定义了_UNICODE
,它将转换为L"HELLO WORLD"
。
同样,如果您传入任何其他令牌,例如_T(email)
,它会将其转换为Lemail
这就是您收到错误的原因。
解决方法是将std::wstring
和std::string
用作:
#if defined(_UNICODE)
#define String std::wstring
#else
#define String std::string
#endif
并改用String email
。
- 如何在C++中用std::cout正确显示带十六进制的字符串文本
- 在编译时检查字符串文本的长度
- 读取字符串文本输入以创建 2D 矢量
- 管理字符串文本的最佳做法
- 定义宏以将前缀 0x 添加到十六进制字符串文本
- 无法在模板参数中定义字符串文本
- 不推荐使用 PTCHAR 的字符串文本
- C++ 字符串文本和常量
- C++20字符串文本模板参数工作示例
- 返回从字符串文本创建的静态string_view是否安全?
- 在处理任何字符大小的模板中使用字符串文本
- 是否保证相同内容字符串文本的存储相同?
- 将以 null 结尾的字节字符串转换为原始字符串文本
- 是否可以创建一个用户定义的文本,将字符串文本转换为 own 类型的数组?
- 使用C++中的模板检测不同的字符串文本
- 无法完全专用化字符串文本的模板
- C++ - 确定 const char* 是指向字符串文本对象还是动态对象
- 是否可以在原始字符串文本中插入转义序列?
- C++带有捕获组的正则表达式字符串文本
- 为什么多维数组中的空字符串文本衰减为空指针?