Article View: borland.public.cppbuilder.nativeapi
Article #13627Re: Send a mail with attachment
From: Rodolfo Frino
Date: Tue, 30 Sep 2003 17:55
Date: Tue, 30 Sep 2003 17:55
228 lines
9005 bytes
9005 bytes
This is one way of doing it. It also contains a full example on how
to automate the default e-mail client via the famous MAPI API's
Sorry if i made any mistakes but it's late here and I'm really tired :-)
#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 InitMAPI()
{
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; // sucess
}
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 ComposeEmail(MapiRecipDesc& SenderInfo, MapiRecipDesc& RecipientInfo,
MapiMessage& Message,
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 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 = 0; // No attachmewnts
Message.lpFiles = NULL;
}
void __fastcall TForm1::Button1Click(TObject *Sender)
{
static LHANDLE hSession = 0;
switch (InitMAPI())
{
case 0:
MapiRecipDesc SenderInfo;
MapiRecipDesc RecipientInfo;
MapiMessage Message; // MapiMessage structure
ComposeEmail(SenderInfo, RecipientInfo, Message,
"Rodolfo", "rodolfofrino@yahoo.com", // Sender
"Alias", "someone@yahoo.com", // Recipient
"Hello E-World, this is the subject", // Subject
"Hello people, I am the body of this e-mail");
// e-mail Body
// 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
//***************************************
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:
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:
// There was no default logon, and the user failed
// to log on successfully when the logon dialog
// box was displayed. No message was 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;
}
}
Rodolfo
"David Ayre" <davidcayre@aol.com> wrote in message
news:3f799567@newsgroups.borland.com...
>
> Thanks Rodolfo,
>
> My code compiles now except for a linker error. Do I need to
> include a .lib file? I'm not sure where the code for MAPI is
> contained. Is it in a dll, or do I need to install a component?
Technical Details
Message-ID:
<3f79c3ae@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!sn-xit-02!sn-xit-06!sn-xit-09!supernews.com!news.maxwell.syr.edu!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mailReferences:
<3f70794d@newsgroups.borland.com> <3f784a32@newsgroups.borland.com> <3f79708b@newsgroups.borland.com> <3f79860c@newsgroups.borland.com> <3f799567@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 10cc67,23d9e1242f297072 X-Google-Attributes: gid10cc67,public X-Google-ArrivalTime: 2003-09-30 11:28:54 PST From: "Rodolfo Frino" <rodolfofrino@yahoo.com> Newsgroups: borland.public.cppbuilder.nativeapi References: <3f70794d@newsgroups.borland.com> <3f784a32@newsgroups.borland.com> <3f79708b@newsgroups.borland.com> <3f79860c@newsgroups.borland.com> <3f799567@newsgroups.borland.com> Subject: Re: Send a mail with attachment Date: Wed, 1 Oct 2003 03:55:57 +1000 Lines: 230 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.23.61 Message-ID: <3f79c3ae@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1064944558 203.206.23.61 (30 Sep 2003 10:55:58 -0700) Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!sn-xit-02!sn-xit-06!sn-xit-09!supernews.com!news.maxwell.syr.edu!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail