views:

69

answers:

3

I'm using VS2008 & C++ and I'm trying to create a command line program that sends an email.
I've looked on line and found some sample programs but none will compile for me.
Does anyone have an example program for me? Thanks

+1  A: 

Take a look at this: http://sourceforge.net/projects/blat/files/

Wolfgang Plaschg
thanks for the link, but I was kinda hoping for a simple command line program that I could easily understand so I could use the same logic in my program.
Rossini
+1  A: 

This code compiles & runs for me - after figuring out the right headers etc. Still needs command line handling, and the use of the MAPI libraries is deprecated, but what do you want for free? Original code from codeproject.com

#include "windows.h"
#include "tchar.h"

#include "mapi.h"
#include "assert.h"
#define ASSERT assert
#define VERIFY assert

BOOL SendMail(CHAR *lpszFrom, CHAR *lpszTo, CHAR *lpszSubject, CHAR *lpszMessage)
{
   BOOL bSent = FALSE;

   HINSTANCE hMAPI = ::LoadLibrary(_T("mapi32.dll"));
   if(0==hMAPI) return bSent;

   typedef ULONG (FAR PASCAL *PFN_MAPILogon)(ULONG,LPTSTR,LPTSTR,FLAGS,ULONG,LPLHANDLE);
   typedef ULONG (FAR PASCAL *PFN_MAPISendMail)(LHANDLE,ULONG,lpMapiMessage,FLAGS,ULONG);
   typedef ULONG (FAR PASCAL *PFN_MAPILogoff)(LHANDLE,ULONG,FLAGS,ULONG);

   PFN_MAPILogon MAPILogon = (PFN_MAPILogon)::GetProcAddress(hMAPI,"MAPILogon");
   PFN_MAPISendMail MAPISendMail = (PFN_MAPISendMail)::GetProcAddress(hMAPI,"MAPISendMail");
   PFN_MAPILogoff MAPILogoff = (PFN_MAPILogoff)::GetProcAddress(hMAPI,"MAPILogoff");

   const BOOL bFunctionsLoaded = (0!=MAPILogon)&&(0!=MAPISendMail)&&(0!=MAPILogoff);
   ASSERT(bFunctionsLoaded);

   if(bFunctionsLoaded)
   {

      LHANDLE session = 0;
      VERIFY(SUCCESS_SUCCESS==MAPILogon(0,0,0,MAPI_NEW_SESSION,0,&session));
      ASSERT(0!=session);

      MapiRecipDesc recipient;
      ::ZeroMemory(&recipient,sizeof(recipient));
      recipient.ulRecipClass = MAPI_TO;
      recipient.lpszName = lpszTo;

      MapiMessage message;
      ::ZeroMemory(&message,sizeof(message));
      message.lpszSubject = lpszSubject;
      message.lpszNoteText = lpszMessage;
      message.nRecipCount = 1;
      message.lpRecips = &recipient;

      bSent = SUCCESS_SUCCESS == MAPISendMail(session,0,&message,0,0);

      VERIFY(SUCCESS_SUCCESS==MAPILogoff(session,0,0,0));

   }

   ::FreeLibrary(hMAPI);

   return bSent;
}

int _tmain(int argc, _TCHAR* argv[])
{
   SendMail("from_you@go_daddy.com","[email protected]","Test subject","New Message");
    return 0;
}
Jeff
This worked! however it does caue outlook to prompt me to allow the program to send the e-mail. I got around this by installing Click Yes http://www.contextmagic.com/express-clickyes/ Since my program will run on an internal PC it's not a problem to install this software.
Rossini
Yes - that's because this is using the MAPI server to send the mail on the PC. There are alternative solutions that don't use MAPI, so you can look into that later -
Jeff
A: 

Since your server is Exchange, your most convenient method to write a program to send email will be using C# and System.Net.Mail as demonstrated here. Here is the C++/CLI code:

   static void CreateTestMessage2( String^ server )
   {
      String^ to = L"[email protected]";
      String^ from = L"[email protected]";
      MailMessage^ message = gcnew MailMessage( from,to );
      message->Subject = L"Using the new SMTP client.";
      message->Body = L"Using this new feature, you can send an e-mail message from an application very easily.";
      SmtpClient^ client = gcnew SmtpClient( server );

      // Credentials are necessary if the server requires the client 
      // to authenticate before it will send e-mail on the client's behalf.
      client->UseDefaultCredentials = true;
      client->Send( message );
      client->~SmtpClient();
   }

If you really want to use native C++ (ie. not access System.Net.Mail via C++/CLI) then you are stuck with one of the native APIs described here.

However you could use MapiSend or blat as described here.

Steve Townsend
I agree c# would be a lot easier, however I need to intergrate sending an e-mail with an existing C++ command line program.
Rossini