2014-02-12 126 views
1

我正在使用外部庫 EASendMail使用Gmail作爲SMTP服務器發送電子郵件。未處理的異常:_com_error在內存位置0x0040f4ac

線導致錯誤

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 = "[email protected]"; 

    ::CoInitialize(NULL); 

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

    // Set your gmail email address 
    oSmtp->FromAddr = _T(" [email protected]"); 

    // 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 "[email protected]", then the user should be "[email protected]" 
    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)); 
} 

回答

0

的最好的事情就是圍繞你的整個代碼

try{ 
.... 
}catch(_com_error& ex){ 
e=e;//a break point here 
} 

,並通過與調試代碼步驟。只要它進入捕獲部分,它就是之前調用的方法。通常與這個COM的東西(我不喜歡它,但有點familliar)它發生,因爲一個較早的方法得到錯誤的參數,所以它返回一個空指針或類似的東西。

+0

我知道哪一行導致錯誤,oSmtp-> LicenseCode = _T( 「TryIt」); – Computernerd

+0

對不起,我還沒有看到你的來源評論。 –

+1

據我所見(我看了一下來自easendmail的產品頁面),它和他們的例子完全一樣。這聽起來可能很愚蠢,但你是否嘗試過重新編譯它? –

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

這可能是問題所在。首先,您要爲空間分配oSmtp,而不是嘗試訪問它。請確認,oSmtp可能是NULL。