开发者

Command Line C++ Program to Send an EMail

I'm using VS2008 & C++ and I'm trying to create a command line program that sends an e开发者_高级运维mail.

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


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","to.someone@gmail.com","Test subject","New Message");
    return 0;
}


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


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"jane@contoso.com";
      String^ from = L"ben@contoso.com";
      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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜