如何在 vc 6.0 中使用 smtp 附加 txt 文件

Posted

技术标签:

【中文标题】如何在 vc 6.0 中使用 smtp 附加 txt 文件【英文标题】:How to attach the txt file using smtp in vc 6.0 【发布时间】:2013-10-31 13:00:26 【问题描述】:

我们能够发送简单的电子邮件。但我们需要在 vc 6.0 中使用 smtp 发送带有文件附件的电子邮件。

bool SendMail()

if (!ValidateEnvelope(sendmail.host, sendmail.recip, 
    sendmail.sender, &sendmail.IP)) return false;

char tmp[255];
if (sendmail.sender=="" || sendmail.recip=="" ||
    sendmail.body=="" || sendmail.subject=="") return false;

sendmail.hSocket=socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sendmail.hSocket==SOCKET_ERROR) return false; 

if (!ConnectTo())

    printf("Unable to connect to the server.\n");
    return false;

snd("HELO", true);

sprintf(tmp, "%s%s%s", "MAIL FROM:<", sendmail.sender, ">");
snd(tmp, true);
sprintf(tmp, "%s%s%s", "RCPT TO:<", sendmail.recip, ">");
snd(tmp, true);

sprintf(tmp, "%s%s%s", "MAIL FROM:<", sendmail.sender, ">");
snd(tmp, true);
sprintf(tmp, "%s%s%s", "RCPT TO:<", sendmail.recip, ">");
snd(tmp, true);
snd("DATA", true);
sprintf(tmp, "%s%s", "From: ", sendmail.sender);
snd(tmp, false);
sprintf(tmp, "%s%s", "To: ", sendmail.recip);
snd(tmp, false);
sprintf(tmp, "%s%s", "Subject: ", sendmail.subject);
snd(tmp, false);
snd("", false);
snd(sendmail.body ,false);
snd(".", true);

snd("NOOP", true);
snd("QUIT", true);
closesocket(sendmail.hSocket);

return true;

如何在上面的代码中附加文件。 提前致谢。

【问题讨论】:

我需要 smtp 来附加文件 【参考方案1】:

你为什么不直接使用这样的简单 MAPI:

    HINSTANCE hlibMAPI;
    LPMAPISENDMAIL lpMAPISendMail;
    MapiMessage msg;
    MapiRecipDesc recipient, sender;

    hlibMAPI = LoadLibrary("MAPI32.DLL");
    if (!hlibMAPI)
    
        AfxMessageBox("Error while sending E-Mail: Can't load MAPI32.DLL");
        return;
    

    lpMAPISendMail= (LPMAPISENDMAIL)GetProcAddress(hlibMAPI, "MAPISendMail");
    if (!lpMAPISendMail)
    
        AfxMessageBox("Error while sending E-Mail: Can't locate function 'MAPISendMail' in 'MAPI32.DLL'");
        return;
    

    long l = 123456;
    unsigned long ul;

    sender.ulReserved = NULL;   
    sender.ulRecipClass = MAPI_ORIG; 
    sender.lpszName = "generic"; 
    sender.lpszAddress = "SMTP:generic@sender.org";  
    sender.ulEIDSize = 4; 
    sender.lpEntryID = &l;    

    recipient.ulReserved = NULL;   
    recipient.ulRecipClass = MAPI_TO; 
    recipient.lpszName = "dummy recipient"; 
    char eadr[200];
    strcpy(eadr, "SMTP:dummy@recipient.org");
    recipient.lpszAddress = eadr;  
    recipient.ulEIDSize = 4; 
    recipient.lpEntryID = &l;    

    // setup message body
    msg.ulReserved = NULL;      
    msg.lpszSubject = "sample subject";      
    msg.lpszNoteText = "sample text";      
    msg.lpszMessageType = NULL; 
    msg.lpszDateReceived = "2013/10/31 00:00"; 
    msg.lpszConversationID = NULL;      
    msg.flFlags = MAPI_RECEIPT_REQUESTED;      
    msg.lpOriginator = &sender; 
    msg.nRecipCount = 1;      
    msg.lpRecips = &recipient;      
    msg.nFileCount = 0;   /// <-- use this to attach your file
    msg.lpFiles = NULL;   //         

    ul = (*lpMAPISendMail)(NULL, (ULONG)AfxGetMainWnd(), &msg, MAPI_LOGON_UI, NULL);
    FreeLibrary(hlibMAPI);

    switch (ul)
    
    case MAPI_E_LOGIN_FAILURE:
        AfxMessageBox("Error while sending E-Mail: Coldn't login");
        return;

    case MAPI_E_INSUFFICIENT_MEMORY:
        AfxMessageBox("Error while sending E-Mail: There was insufficient memory to send the e-mail");
        return;

    case MAPI_E_USER_ABORT:
        AfxMessageBox("User canceled mail creation");
        return;

    case SUCCESS_SUCCESS:
        AfxMessageBox("E-mail was sent successfully");
        break;

    default:
        AfxMessageBox("Unknown error while sending E-Mail");
        return;
    

【讨论】:

感谢您的回复 thomeil...您可以分享完整的代码...因为它显示了很多错误,我不知道它是什么?例如'错误C2065:'LHANDLE':未声明的标识符错误C2275:'ULONG':非法使用这种类型作为表达式 确保你有#includeED 我认为它使用 Outlook.... 我不想使用 Outlook。在 SMTP 中附加文件的任何其他方式? 别傻了...为什么你认为它使用 Outlook?我还没有在这里安装 Outlook,它工作正常。如果您不想使用 MAPI32.DLL 之类的库,我建议您在网上查找 SMTP 代码并从头开始。 抱歉...现在我收到“lpfnMAPISendMail”:未声明的标识符错误...有什么帮助吗?【参考方案2】:

我建议您使用标准库。 I would prefer this one.

否则从项目中复制所需的代码。

【讨论】:

以上是关于如何在 vc 6.0 中使用 smtp 附加 txt 文件的主要内容,如果未能解决你的问题,请参考以下文章

在 VS2013 中创建与 VC++ 6.0 向后兼容的 Dll

VC++6.0 如何获取窗口客户区域在屏幕中的位置

如何在VC++6.0中新建一个通用类

VC++6.0中如何让窗口总在前面?

如何在vc++6.0中引用bcgcontrolbar控件?

在VC++6.0中点击OPEN,然后软件就自动关闭了,这是怎么回事,如何解决。