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

时间:2013-10-31 13:00:26

标签: visual-c++ mfc smtp vc6

我们可以发送简单的电子邮件。但我们需要在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;
}

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

2 个答案:

答案 0 :(得分: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;
    }

答案 1 :(得分:0)

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

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