未处理的异常:内存位置为0x0040f4ac的_com_error

时间:2014-02-12 14:35:55

标签: c++ debugging easendmail

我使用外部库 EASendMail使用gmail作为SMTP服务器发送电子邮件。

导致错误的行

  

oSmtp-> LicenseCode = _T(“TryIt”);

安装外部库的link

#include "stdafx.h"
#include <iostream>
#include "easendmailobj.tlh"
#include <string>

using namespace EASendMailObjLib;
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{

    string Lrecipient_email = "foobar@hotmail.com";

    ::CoInitialize( NULL );

    IMailPtr oSmtp = NULL;
    oSmtp.CreateInstance( "EASendMailObj.Mail");
    oSmtp->LicenseCode = _T("TryIt");  //error is here

    // Set your gmail email address
    oSmtp->FromAddr = _T(" mygmailacc@gmail.com");

    // Add recipient email address
    oSmtp->AddRecipientEx( _T(recipient_email.c_str()), 0);

    // Set email subject
    oSmtp->Subject = _T("Payment of Desposit Required");

    // Set email body
    oSmtp->BodyText = _T("Dear Customer , Please pay your deposit now !!!");

    // 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("username");
    oSmtp->Password = _T("password");

    _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();

    return 0;
}

我不知道为什么我会收到以下错误:

Unhandled exception at 0x7558c41f in SendEmail.exe: Microsoft C++ exception: _com_error at memory location 0x0040f4ac..

MS Studio调试程序将此文件显示为文件中的错误来源: easendmailobj.tli

错误1

 Interface* operator->() const 
    { 
        if (m_pInterface == NULL) 
        {
            _com_issue_error(E_POINTER);
        }

        return m_pInterface; 
    }

错误2

inline void IMail::PutLicenseCode ( _bstr_t pVal ) {
    HRESULT _hr = put_LicenseCode(pVal);
    if (FAILED(_hr)) _com_issue_errorex(_hr, this, __uuidof(this));
}

3 个答案:

答案 0 :(得分:2)

oSmtp-&gt; LicenseCode = _T(“TryIt”); 试用版到期时会发生此错误。

“TryIt”是评估许可证代码,仅可用于演示目的。许可证到期后1个月,它会引发COM异常。

您可以进一步查看这些链接

https://www.emailarchitect.net/easendmail/sdk/html/LicenseCode.htm https://www.emailarchitect.net/easendmail/sdk/html/license.htm

答案 1 :(得分:0)

最好的办法是用

包围整个代码
try{
....
}catch(_com_error& ex){
e=e;//a break point here
}

并使用调试器逐步执行代码。一旦它进入catch部分,它就是之前调用的引起它的方法。通常有这个COM的东西(我不喜欢它,但有点熟悉它)它发生,因为一个早期的方法得到错误的参数,所以它返回一个空指针或类似的东西。

答案 2 :(得分:0)

IMailPtr oSmtp = NULL;
oSmtp.CreateInstance( "EASendMailObj.Mail");

这可能是问题所在。首先,您要为oSmtp分配一个NULL,而不是尝试访问它。请验证,oSmtp可能为NULL。