🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.internet.web
Article #971

Re: How display MAPIMAIL

#971
From: Remy Lebeau (TeamB)
Date: Thu, 02 Mar 2006 21:49
248 lines
8742 bytes
"RS" <rs.reloaded@wanadoo.fr> wrote in message
news:44073e03$1@newsgroups.borland.com...

> But I Have problem

Please be more specific.

> can you put the full source code for my situation ?

The code I gave you is a direct copy-paste from the test project I wrote for
this discussion, which works fine when I run it as-is.

> I don't know who I put your code !!

You simply replace the code you showed earlier with the code I gave you.
What exactly is so hard about that?

> I Have error :

You probably put the code in the wrong place.  GetFileName() is a standalone
function.  It cannot be placed inside another function, which is what you
are likely doing.

> My first code is on a simple button click.

Here is the FULL code I used for testing:

    --- Unit1.h---


//--------------------------------------------------------------------------
-

    #ifndef MapiSendMailFormH
    #define MapiSendMailFormH

//--------------------------------------------------------------------------
-
    #include <Classes.hpp>
    #include <Controls.hpp>
    #include <StdCtrls.hpp>
    #include <Forms.hpp>

//--------------------------------------------------------------------------
-
    class TForm1 : public TForm
    {
    __published: // IDE-managed Components
        TButton *Button1;
        void __fastcall Button1Click(TObject *Sender);
    private: // User declarations
    public:  // User declarations
    __fastcall TForm1(TComponent* Owner);
    };

//--------------------------------------------------------------------------
-
    extern PACKAGE TForm1 *Form1;

//--------------------------------------------------------------------------
-
    #endif

//--------------------------------------------------------------------------
-


    --- Unit1.cpp ---

    #include <vcl.h>
    #pragma hdrstop

    #include "Unit1.h"
    #include <mapi.h>

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

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

//--------------------------------------------------------------------------
-
    LPTSTR GetFileName(LPTSTR szPathName)
    {
        LPTSTR szFileName = szPathName;
        LPTSTR ptr = szPathName;

        if( NULL != ptr )
        {
            while( 0 != *ptr )
            {
                if( TEXT('\\') == *ptr )
                {
                    szFileName = CharNext(ptr);
                    ptr = szFileName;
                }
                else
                    ptr = CharNext(ptr);
            }

            if( 0 != *szFileName )
                return szFileName;
        }

        return szPathName;
    }

    void SendMapiMessage(HWND hWnd, LPTSTR SenderAddr, LPTSTR RecipAddr,
LPTSTR Subject, LPTSTR Body, LPTSTR *Files, ULONG FileCount)
    {
        TCHAR szMsg[64];

        HINSTANCE hDll = LoadLibrary(TEXT("MAPI32.DLL"));
        if( NULL != hDll )
        {
            LPMAPILOGON pfnLogon = (LPMAPILOGON) GetProcAddress(hDll,
TEXT("MAPILogon"));
            LPMAPILOGOFF pfnLogoff = (LPMAPILOGOFF) GetProcAddress(hDll,
TEXT("MAPILogoff"));
            LPMAPISENDMAIL pfnSendMail = (LPMAPISENDMAIL)
GetProcAddress(hDll, TEXT("MAPISendMail"));

            if( (NULL != pfnLogon) && (NULL != pfnLogoff) && (NULL !=
pfnSendMail) )
            {
                MapiFileDesc *mapifiledesc = NULL;
                if( 0 != FileCount )
                {
                    mapifiledesc = (MapiFileDesc*)
malloc(sizeof(MapiFileDesc) * FileCount);
                    if( NULL != mapifiledesc )
                    {
                        for(ULONG u = 0; u < FileCount; ++u)
                        {
                            mapifiledesc[u].nPosition    = -1;
                            mapifiledesc[u].lpszPathName = Files[u];
                            mapifiledesc[u].lpszFileName =
GetFileName(Files[u]);
                        }
                    }
                }

                if( (0 == FileCount) || (NULL != mapifiledesc) )
                {
                    LHANDLE lHnd;
                    ULONG err = pfnLogon((ULONG) hWnd, NULL, NULL,
MAPI_LOGON_UI, 0, &lHnd);

                    if( SUCCESS_SUCCESS == err )
                    {
                        MapiRecipDesc rdOriginator = {0};
                        rdOriginator.ulRecipClass  = MAPI_ORIG;
                        rdOriginator.lpszAddress   = SenderAddr;

                        MapiRecipDesc rdRecipient = {0};
                        rdRecipient.ulRecipClass  = MAPI_TO;
                        rdRecipient.lpszAddress   = RecipAddr;

                        MapiMessage mapimsg  = {0};
                        mapimsg.lpszSubject  = Subject;
                        mapimsg.lpszNoteText = Body;
                        mapimsg.lpOriginator = &rdOriginator;
                        mapimsg.nRecipCount  = 1;
                        mapimsg.lpRecips     = &rdRecipient;
                        mapimsg.nFileCount   = FileCount;
                        mapimsg.lpFiles      = mapifiledesc;

                        err = pfnSendMail(lHnd, (ULONG) hWnd, &mapimsg,
MAPI_DIALOG, 0);

                        if( SUCCESS_SUCCESS != err )
                        {
                            wsprintf(szMsg, TEXT("could not send mapi32
message, ErrorCode: %u"), err);
                            MessageBox(NULL, szMsg, TEXT("Error"), MB_OK);
                        }

                        err = pfnLogoff(lHnd, (ULONG) hWnd, 0, 0);
                        if( SUCCESS_SUCCESS != err )
                        {
                            wsprintf(szMsg, TEXT("could not log out of
mapi32, ErrorCode: %u"), err);
                            MessageBox(NULL, szMsg, TEXT("Error"), MB_OK);
                        }
                    }
                    else
                    {
                        wsprintf(szMsg, TEXT("could not log in to mapi32,
ErrorCode: %u"), err);
                        MessageBox(NULL, szMsg, TEXT("Error"), MB_OK);
                    }

                    if( mapifiledesc )
                        free(mapifiledesc);
                }
                else
                    MessageBox(NULL, TEXT("could not allocate memory for
attachments"), TEXT("Error"), MB_OK);
            }
            else
            {
                MessageBox(NULL, TEXT("could not load mapi32 functions"),
TEXT("Error"), MB_OK);
            }

            FreeLibrary(hDll);
        }
        else
        {
            wsprintf(szMsg, TEXT("could not load mapi32.dll, ErrorCode:
%u"), GetLastError());
            MessageBox(NULL, szMsg, TEXT("Error"), MB_OK);
        }
    }

//--------------------------------------------------------------------------
-
    void __fastcall TForm1::Button1Click(TObject *Sender)
    {
        LPTSTR Files[2] = {
            TEXT("C:\\autoexec.bat"),
            TEXT("C:\\config.sys")
        };

        SendMapiMessage(
            Application->Handle,
            TEXT("me@myaddress.com"),
            TEXT("you@youraddress.com"),
            TEXT("Here is the Subject"),
            TEXT("Here is the Body Text"),
            Files,
            2);
    }

//--------------------------------------------------------------------------
-


Gambit
Technical Details
Message-ID: <4407681b$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!news3.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail
References: <4402040f@newsgroups.borland.com> <4404bbf5$1@newsgroups.borland.com> <4404fe85$1@newsgroups.borland.com> <440602dc$1@newsgroups.borland.com> <44064ed2$1@newsgroups.borland.com> <44073e03$1@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: feeec,b415d4e666b19fd4
X-Google-Attributes: gidfeeec,public
X-Google-Language: ENGLISH,ASCII-7-bit
From: "Remy Lebeau \(TeamB\)" <no.spam@no.spam.com>
Newsgroups: borland.public.cppbuilder.internet.web
References: <4402040f@newsgroups.borland.com> <4404bbf5$1@newsgroups.borland.com> <4404fe85$1@newsgroups.borland.com> <440602dc$1@newsgroups.borland.com> <44064ed2$1@newsgroups.borland.com> <44073e03$1@newsgroups.borland.com>
Subject: Re: How display MAPIMAIL
Date: Thu, 2 Mar 2006 13:49:09 -0800
Lines: 250
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1409
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1409
NNTP-Posting-Host: 68.69.17.41
Message-ID: <4407681b$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1141336091 68.69.17.41 (2 Mar 2006 13:48:11 -0700)
Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!news3.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail