错误 C3861: '_T':未找到标识符,无法将主函数参数作为函数参数传递

11

我正在遵循这个指南,学习使用C++发送电子邮件。我按照说明操作,一切都很顺利。

以下是发送电子邮件的示例代码,出于安全考虑,我已更改了电子邮件地址和密码。

#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("randomemail1.lim@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;
}

当我尝试将这段代码复制粘贴到我的程序中并将其作为一个可用的函数,我会得到以下错误:

error C3861: '_T': identifier not found 

这是我的函数长这样:
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("randomemail2@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("randomemail1@gmail.com");
    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;


}

我认为这与我无法将 int argc_TCHAR*argc[] 作为参数传递有关,如何将其制作成可用的函数并解决该问题?
1个回答

39

为什么我的第一个示例可以在没有<tchar.h>头文件的情况下工作? - Computernerd
@Computernerd,它将被包含在其他显式包含的头文件之一中。建议始终显式包含所需的头文件,因为编译器供应商会有所不同。 - hmjd

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接