Subj : Re: email api To : borland.public.cpp.borlandcpp From : "Dennis Jones" Date : Wed Nov 12 2003 03:01 pm "Dan" wrote in message news:3fb134b2$1@newsgroups.borland.com... > > Hello I am an experienced C/C++ programmer with minimal win32 skills. > I want to find some example code that will teach me how to send an email text message to a known domain address. I have C++ builder and plain Borland C++. 5 And I have a fairly good library of books. Maybe i just need to know what to look for? > > Any tips or sugegstions would be greatly appreciated. > > For example: > > int SendEmail(char *address, char *subject, char *text) > { > > // Help me what do i do > > retrun 0; > } This is one of those things that is not only poorly documented, but that no one person seems to know how to do correctly for all situations (using the Shell API to browse folders while also correctly initializing with UNC paths is another one). When I finally did get email to basically work, it worked correctly for Outlook Express, but not for Outlook. Then I corrected it to work with Outlook, and it stopped working for Outlook Express! It seems Microsoft has a real problem coming up with consistent ways of doing things. Anyway, take a look at the code below and see what I finally ended up with. The code below has been modified from my original code to take the arguments you seem to want to pass in, although you may have to tweak it, but it should work fairly well (originally written in BCB5). You may or may not need to modify the flags in the MapiSendMail() call. Good luck, - Dennis #pragma option push -C- /*P_O_Push*/ // disable nested comments #include #pragma option pop /*P_O_Pop*/ #include bool SendEmail( const char * const RecipientName, const char * const EmailAddress, const char * const Subject, const char * const MessageText ) { TMapiRecipDesc recipient; recipient.ulReserved = 0; recipient.ulRecipClass = MAPI_TO; recipient.lpszName = (char *)RecipientName; recipient.lpszAddress = (char *)EmailAddress; recipient.ulEIDSize = 0; recipient.lpEntryID = NULL; TMapiRecipDesc *pMapiRecip = 0; TMapiMessage note; note.ulReserved = 0; note.lpszSubject = Subject; note.lpszNoteText = MessageText; note.lpszMessageType = NULL; note.lpszDateReceived = NULL; note.lpszConversationID = NULL; note.flFlags = 0; note.lpOriginator = NULL; note.nRecipCount = 1; note.lpRecips = &recipient; note.nFileCount = 0; note.lpFiles = NULL; unsigned int error; try { unsigned int Session; error = MapiLogOn( 0, NULL, NULL, MAPI_NEW_SESSION | MAPI_LOGON_UI, 0, &Session ); if ( error != SUCCESS_SUCCESS ) { return false; } try { std::auto_ptr pRegistry(new TRegistry); pRegistry->RootKey = HKEY_LOCAL_MACHINE; if ( !pRegistry->OpenKey( "\\Software\\Clients\\Mail", false ) ) return false; AnsiString DefaultMailClient; try { try { DefaultMailClient = pRegistry->ReadString( "" ); } catch(...) { return false; } } __finally { pRegistry->CloseKey(); } if ( DefaultMailClient == "Microsoft Outlook" ) { // MapiResolveName allows MapiSendMail to work with Outlook, but causes // MapiSendMail to fail when Outlook Express is used. error = MapiResolveName( Session, (unsigned int)Application->Handle, (char *)EmailAddress, /*MAPI_DIALOG*/0, 0, pMapiRecip ); if ( error != SUCCESS_SUCCESS ) { pMapiRecip = 0; return false; } note.lpRecips = pMapiRecip; } error = MapiSendMail(Session, (unsigned int)Application->Handle, note, /*MAPI_LOGON_UI | MAPI_DIALOG | MAPI_NEW_SESSION*/0, 0); if ( error != SUCCESS_SUCCESS ) { return false; } } __finally { MapiLogOff( Session, (unsigned int)Application->Handle, 0, 0 ); } } __finally { if ( pMapiRecip ) MapiFreeBuffer( pMapiRecip ); // a MAPI error occured if (error != SUCCESS_SUCCESS) { AnsiString msg; msg.sprintf( "An error occured while trying to send email.\n\nMAPI Error Code: %d", error ); Application->MessageBox( msg.c_str(), "MAPI Error", MB_OK | MB_ICONERROR ); } } return true; } //-------------------------------------------------------------------------- - .