🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

LoadLibary() and MAPI...?

#12363
From: Oren (Halvani.de)
Date: Sat, 13 Dec 2003 00:39
161 lines
6703 bytes
hi dear builders,

I use the below code to send a e-mail via MAPI, my problem is IT ONLY works
when Mapi is installed on the current machine..so, what I would like to know
is, how can I load MAPI dynamicly via: LoadLibary( ) ??

Have someone a idea...?


Oren

/*************************************************************/

#include <mapi.h>


__fastcall TfrmEmail_Config::TfrmEmail_Config(TComponent* Owner) :
TForm(Owner){}

bool MapiSendMail(TStrings* pslRecipients, TStrings* pslCcRecipients,
  AnsiString slSubject, AnsiString slText, TStrings* pslAttachments)
{
  if(!pslRecipients || !pslRecipients->Count)   return false;

  bool blRetVal = true;  // R�ckgabewert

  HINSTANCE hMapi=LoadLibrary("MAPI32.DLL");
  if(hMapi)
  {
    // Zeiger auf die Funktionen MAPILogon(), MAPILogoff(),
    // MAPISendMail() besorgen:
    LPMAPILOGON pfMapiLogon= (LPMAPILOGON)
      GetProcAddress(hMapi,"MAPILogon");
    LPMAPILOGOFF pfMapiLogoff=(LPMAPILOGOFF)
      GetProcAddress(hMapi,"MAPILogoff");
    LPMAPISENDMAIL pfMapiSendMail=(LPMAPISENDMAIL)
      GetProcAddress(hMapi,"MAPISendMail");
    // Funktionszeiger auf NULL pr�fen:
    blRetVal = pfMapiLogon && pfMapiLogoff && pfMapiSendMail;

    // MAPI-Session starten:
    LHANDLE hSession = NULL;
    if(blRetVal) blRetVal = ((*pfMapiLogon)(0, NULL, NULL, MAPI_LOGON_UI, 0,
      &hSession) == SUCCESS_SUCCESS);

    // Falls soweit alles ok:
    if(blRetVal)
    {
      // Anzahl der Empf�nger bestimmen:
      int ilRecCount = pslRecipients->Count;
      // Anz. der Kopien-Empf�nger bestimmen:
      if(pslCcRecipients && pslCcRecipients->Count)
        ilRecCount += pslCcRecipients->Count;

      // "Empf�ngerbeschreibungsstrukturen" erzeugen und f�llen:
      MapiRecipDesc* pstRecDesc = new MapiRecipDesc[ilRecCount];

      AnsiString slAddress;
      // Die Hauptempf�ngerliste bearbeiten:
      for(int ilLine = 0; ilLine < pslRecipients->Count; ilLine++)
      {
        memset(&pstRecDesc[ilLine], 0, sizeof(MapiRecipDesc));
        slAddress = pslRecipients->Strings[ilLine];
        pstRecDesc[ilLine].ulRecipClass = MAPI_TO;
        pstRecDesc[ilLine].lpszName = AnsiString("SMTP:"+slAddress).c_str();
        pstRecDesc[ilLine].lpszName = new char[slAddress.Length() + 1];
        strcpy(pstRecDesc[ilLine].lpszName,slAddress.c_str());
        pstRecDesc[ilLine].lpszAddress = new char[slAddress.Length() + 6];
        strcpy(pstRecDesc[ilLine].lpszAddress,("SMTP:"+slAddress).c_str());
      }

      int ilIndex = 0;
      // Die Kopien-Empf�ngerliste bearbeiten:
      for(int ilLine = 0; pslCcRecipients && ilLine <
        pslCcRecipients->Count; ilLine++)
      {
        ilIndex = pslRecipients->Count+ilLine;
        memset(&pstRecDesc[ilIndex], 0, sizeof(MapiRecipDesc));
        slAddress = pslCcRecipients->Strings[ilLine];
        pstRecDesc[ilIndex].ulRecipClass = MAPI_CC;
        pstRecDesc[ilIndex].lpszName = new char[slAddress.Length() + 1];
        strcpy(pstRecDesc[ilIndex].lpszName,slAddress.c_str());
        pstRecDesc[ilIndex].lpszAddress = new char[slAddress.Length() + 6];
        strcpy(pstRecDesc[ilIndex].lpszAddress,("SMTP:"+slAddress).c_str());
      }

      MapiFileDesc* pstFilesDesc = NULL; // Liste der Anh�nge bearbeiten:
      if(pslAttachments && pslAttachments->Count)
      {
        pstFilesDesc = new MapiFileDesc[pslAttachments->Count];
        AnsiString slFullPath, slFileName;
        for(int ilLine = 0; ilLine < pslAttachments->Count; ilLine++)
        {
          memset(&pstFilesDesc[ilLine], 0, sizeof(MapiFileDesc));
          slFullPath = pslAttachments->Strings[ilLine];
          slFileName =
slFullPath.SubString(slFullPath.LastDelimiter("\\:")+1,
            slFullPath.Length());
          pstFilesDesc[ilLine].nPosition = (ULONG) -1;
          pstFilesDesc[ilLine].lpszPathName = new char[slFullPath.Length() +
1];
          pstFilesDesc[ilLine].lpszFileName = new char[slFileName.Length() +
1];
          strcpy(pstFilesDesc[ilLine].lpszPathName, slFullPath.c_str());
          strcpy(pstFilesDesc[ilLine].lpszFileName, slFileName.c_str());
        }
      }

      MapiMessage MapiMsg = {
0,slSubject.c_str(),slText.c_str(),NULL,NULL,NULL,0,NULL,0,0,0,0 };
      MapiMsg.nRecipCount = (ULONG)ilRecCount;
      MapiMsg.lpRecips = (lpMapiRecipDesc)(pstRecDesc);

      if(pstFilesDesc)
      {
        MapiMsg.nFileCount = (ULONG) pslAttachments->Count;
        MapiMsg.lpFiles = (lpMapiFileDesc)(pstFilesDesc);
      }
      blRetVal = (*pfMapiSendMail)(hSession,0,&MapiMsg, MAPI_LOGON_UI ,0) ==
SUCCESS_SUCCESS;

      // Speicher aufr�umen:
      // Empf�nger-Listen:
      if(pstRecDesc)
      {
        for(int ilEntry=0; ilEntry < ilRecCount; ilEntry++)
        {
          delete [] pstRecDesc[ilEntry].lpszAddress;
          delete [] pstRecDesc[ilEntry].lpszName;
        }
        delete [] pstRecDesc;
      }

      if(pstFilesDesc) // Anh�nge:
      {
        for (int ilEntry=0; ilEntry < pslAttachments->Count; ilEntry++)
        {
          delete [] pstFilesDesc[ilEntry].lpszPathName;
          delete [] pstFilesDesc[ilEntry].lpszFileName;
        }
        delete [] pstFilesDesc;
      }
    }
    (*pfMapiLogoff)(hSession,0,0,0); // Logout...
    FreeLibrary(hMapi);
    return blRetVal;
  }
  else return false;
}

void __fastcall TfrmEmail_Config::Button1Click(TObject *Sender)
{
  TStringList* pslAddresses = new TStringList();
  pslAddresses->Add("abc@def.gh");

  if(!MapiSendMail(pslAddresses, NULL, "Betreff",
frmStringDetails->Memo1->Text, NULL))
  ShowMessage("Fehler beim Versenden der E-Mail aufgetreten !");

  delete pslAddresses;
}
Technical Details
Message-ID: <3fda5fc7@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news.maxwell.syr.edu!xmission!logbridge.uoregon.edu!nntp-relay.ihug.net!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII
X-Google-Thread: 10cc67,6df1da8c482fa4e8,start
X-Google-Attributes: gid10cc67,public
X-Google-ArrivalTime: 2003-12-12 16:53:18 PST
Reply-To: "Oren \(Halvani.de\)" <oren78@freenet.de>
From: "Oren \(Halvani.de\)" <oren78@freenet.de>
Newsgroups: borland.public.cppbuilder.nativeapi
Subject: LoadLibary() and MAPI...?
Date: Sat, 13 Dec 2003 01:39:50 +0100
Lines: 163
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2600.0000
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2600.0000
NNTP-Posting-Host: 213.6.46.54
Message-ID: <3fda5fc7@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1071275975 213.6.46.54 (12 Dec 2003 16:39:35 -0700)
Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news.maxwell.syr.edu!xmission!logbridge.uoregon.edu!nntp-relay.ihug.net!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail