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

Using string variables instead of string literals causes error

本文关键字:字符串 文本 错误 变量      更新时间:2023-10-16

我正在按照本指南用c ++发送电子邮件。您可以在此处下载头文件: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 ...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决定使用wstringstring。尝试通读代码以及_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::wstringstd::string用作:

#if defined(_UNICODE)
#define String std::wstring
#else
#define String std::string
#endif

并改用String email