🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.nativeapi
Article #13631

Re: How to send email with attachment

#13631
From: Rodolfo Frino
Date: Tue, 30 Sep 2003 23:52
240 lines
9348 bytes
This code will do the attachment for you. You can change the position of the
attachment
if you wanted to.

#include <vcl.h>
#include <mapi.h>
#include "AutomatingMailClient.h"
#pragma hdrstop

//--------------------------------------------------------------------------
-
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;

HINSTANCE      hMAPILib = 0;
LPMAPILOGON    pMAPILogon = NULL;
LPMAPILOGOFF   pMAPILogoff = NULL;
LPMAPISENDMAIL pMAPISendMail = NULL;


//--------------------------------------------------------------------------
-
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}
//--------------------------------------------------------------------------
-

int InitMAPIFunctions()
{
    hMAPILib = ::LoadLibrary("MAPI32.DLL");

    if (!hMAPILib)
        return 1;   // failed

    if (!(pMAPILogon = (LPMAPILOGON) GetProcAddress (hMAPILib,
"MAPILogon")))
        return 2;   // failed

    if (!(pMAPISendMail = (LPMAPISENDMAIL) GetProcAddress (hMAPILib,
          "MAPISendMail")))
        return 3;   // failed

    if (!(pMAPILogoff = (LPMAPILOGOFF) GetProcAddress (hMAPILib,
"MAPILogoff")))
        return 4;   // failed

    return 0;       // success
}

void ComposeSender(MapiRecipDesc& SenderInfo,
                   char* SenderName, char* SenderAddress)
{
    SenderInfo.ulReserved = 0;
    SenderInfo.ulRecipClass = MAPI_ORIG;
    SenderInfo.lpszName = SenderName;
    SenderInfo.lpszAddress = SenderAddress;
    SenderInfo.ulEIDSize = 0;  // dummy
    SenderInfo.lpEntryID = 0;  // dummy
}

void ComposeRecipient(MapiRecipDesc& RecipientInfo,
                      char* RecipientName, char* RecipientAddress)
{
    RecipientInfo.ulReserved = 0;
    RecipientInfo.ulRecipClass = MAPI_TO;
    RecipientInfo.lpszName = RecipientName;
    RecipientInfo.lpszAddress = RecipientAddress;
    RecipientInfo.ulEIDSize = 0;  // dummy
    RecipientInfo.lpEntryID = 0;  // dummy
}

void ComposeAttachment(MapiFileDesc&  FileInfo)
{
    FileInfo.ulReserved = 0;
    FileInfo.flFlags = 0;
    FileInfo.nPosition = -1;
    FileInfo.lpszPathName = "C:\\Kylie.jpg";
    FileInfo.lpszFileName = NULL;
    FileInfo.lpFileType = NULL;
}

void ComposeEmail(MapiRecipDesc& SenderInfo, MapiRecipDesc& RecipientInfo,
                  MapiMessage& Message, MapiFileDesc&  FileInfo,
                  char* SenderAlias,    char* SenderEmail,
                  char* RecipientAlias, char* RecipientEmail,
                  char* Subject, char* Text)
{
// Compose Email

           //---------------------------------------------------
            // Compose Sender
            ComposeSender(SenderInfo, SenderAlias, SenderEmail);
            //---------------------------------------------------
            // Compose Recipient
            ComposeRecipient(RecipientInfo, RecipientAlias, RecipientEmail);
            //---------------------------------------------------
            // Compose Attachment
            ComposeAttachment(FileInfo);
            //-------------------------------------------------------------
            // Compose Rest of E-mail
            memset(&Message, 0, sizeof(Message));
            Message.ulReserved = 0;
            Message.lpszSubject = Subject;
            Message.lpszNoteText = Text;
            Message.lpszMessageType = 0;
            Message.lpszDateReceived = 0;
            Message.lpszConversationID = 0;

            Message.flFlags = MAPI_LOGON_UI | MAPI_NEW_SESSION;
            Message.lpOriginator = &SenderInfo;// Get Pointer to the Sender
info

            Message.nRecipCount = 1;
            Message.lpRecips = &RecipientInfo; // Get Pointer to the
Recipients info
            Message.nFileCount = 1;            // No attachmewnts
            Message.lpFiles = &FileInfo;

}

void SendMailMessage()
{
        static LHANDLE hSession = 0;

        switch (InitMAPIFunctions())
        {
        case 0:
            MapiMessage   Message;            // MapiMessage structure
            MapiRecipDesc SenderInfo;         // MapiRecipDesc structure
            MapiRecipDesc RecipientInfo;      // MapiRecipDesc structure
            MapiFileDesc  FileInfo;

            // Compose e-mail
            ComposeEmail(SenderInfo, RecipientInfo, Message, FileInfo,
                         "Rodolfo",   "rodolfofrino@yahoo.com", // Sender
                         "Alias",     "someone@yahoo.com",      // Recipient
                         "Hello E-World, this is the subject",  // Subject
                         // e-mail Body
                         "Hello people, I am the body of this e-mail");

            // Logon session
            if (pMAPILogon(0, NULL, NULL, MAPI_LOGON_UI | MAPI_NEW_SESSION,
                           0, &hSession) != SUCCESS_SUCCESS)
                ShowMessage("Cannot send mail: Logon failed");

            //************************************************
            // Send email using the MAPISendMail API function
            //************************************************
            switch (pMAPISendMail(0, 0, &Message, MAPI_DIALOG, 0))
            {
            case SUCCESS_SUCCESS:
                 break;
            case MAPI_E_AMBIGUOUS_RECIPIENT:
                 ShowMessage("Cannot send mail: MAPI_E_AMBIGUOUS_RECIPIENT
error");
                 break;
            case MAPI_E_ATTACHMENT_NOT_FOUND:
                 ShowMessage("Cannot send mail: MAPI_E_ATTACHMENT_NOT_FOUND
error");
                 break;
            case MAPI_E_ATTACHMENT_OPEN_FAILURE:
                 ShowMessage("Cannot send mail:
MAPI_E_ATTACHMENT_OPEN_FAILURE error");
                 break;
            case MAPI_E_BAD_RECIPTYPE:
                 ShowMessage("Cannot send mail: MAPI_E_BAD_RECIPTYPE
error");
                 break;
            case MAPI_E_FAILURE:
                 // There are unspecified errors. (one or more)
                 // message (e-mail) was not sent.
                 ShowMessage("Cannot send mail: MAPI_E_FAILURE error");
                 break;
            case MAPI_E_INSUFFICIENT_MEMORY:
                 ShowMessage("Cannot send mail: MAPI_E_INSUFFICIENT_MEMORY
error");
                 break;
            case MAPI_E_LOGIN_FAILURE:
                 // no default logon was performed, and the user failed
                 // to log on successfully when the logon dialog window
                 // was displayed. message (e-mail) was not sent.
                 ShowMessage("Cannot send mail: MAPI_E_LOGIN_FAILURE
error");
                 break;
            case MAPI_E_TEXT_TOO_LARGE:
                 ShowMessage("Cannot send mail: MAPI_E_TEXT_TOO_LARGE
error");
                 break;
            case MAPI_E_TOO_MANY_FILES:
                 ShowMessage("Cannot send mail: MAPI_E_TOO_MANY_FILES
error");
                 break;
            case MAPI_E_TOO_MANY_RECIPIENTS:
                 ShowMessage("Cannot send mail: MAPI_E_TOO_MANY_RECIPIENTS
error");
                 break;
            case MAPI_E_UNKNOWN_RECIPIENT:
                 ShowMessage("Cannot send mail: MAPI_E_UNKNOWN_RECIPIENT
error");
                 break;
            case MAPI_E_USER_ABORT:
                 ShowMessage("Cannot send mail: MAPI_E_USER_ABORT error");
                 break;
            default:
                 ShowMessage("Cannot send mail: Unknown Error");
            }

            // Logoff session
            pMAPILogoff(hSession, 0, 0, 0);
            ::FreeLibrary(hMAPILib);
            break;
        case 1:
            ShowMessage("LoadLibrary(\"MAPI32.DLL\") falied");
            break;
        case 2:
            ::FreeLibrary(hMAPILib);
            ShowMessage("GetProcAddress (hMAPILib, \"MAPILogon\") failed");
            break;
        case 3:
            ::FreeLibrary(hMAPILib);
            ShowMessage("GetProcAddress (hMAPILib, \"MAPISendMail\")
failed");
            break;
        case 4:
            ::FreeLibrary(hMAPILib);
            ShowMessage("GetProcAddress (hMAPILib, \"MAPILogoff\") failed");
            break;
        }
}

void __fastcall TForm1::Button1Click(TObject *Sender)
{
    SendMailMessage();
}


Rodolfo
Technical Details
Message-ID: <3f7a172e@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!sn-xit-02!sn-xit-04!sn-xit-01!sn-xit-08!supernews.com!news-out.visi.com!petbe.visi.com!news.maxwell.syr.edu!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail
References: <3f6f2a83@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 10cc67,103a5b9fca46af86
X-Google-Attributes: gid10cc67,public
X-Google-ArrivalTime: 2003-09-30 17:09:56 PST
From: "Rodolfo Frino" <rodolfofrino@yahoo.com>
Newsgroups: borland.public.cppbuilder.nativeapi
References: <3f6f2a83@newsgroups.borland.com>
Subject: Re: How to send email with attachment
Date: Wed, 1 Oct 2003 09:52:25 +1000
Lines: 242
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
NNTP-Posting-Host: 203.206.27.34
Message-ID: <3f7a172e@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1064965934 203.206.27.34 (30 Sep 2003 16:52:14 -0700)
Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!sn-xit-02!sn-xit-04!sn-xit-01!sn-xit-08!supernews.com!news-out.visi.com!petbe.visi.com!news.maxwell.syr.edu!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail