Frage

we able to send the simple email. but we need to send the email with an file attachment using smtp in vc 6.0.

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;
}

How to attach the file in above code. Thanks in advance.

War es hilfreich?

Lösung

Why don't you just use Simple MAPI like this:

    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;
    }

Andere Tipps

I would advise you to use a standard library. I would prefer this one.

Else copy the required code from the project.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top