Sending mail in an unmanaged VC++ application

An oft posted question in NGs is how to send mail from an unmanaged VC++ application. If you are using VC++ 2003, then you can use the ATL7 mailing classes. I list below some bare-minimum steps to send a mail out, but you might want to look up further documentation on MSDN.

  • Include the required header file
    #include <atlsmtpconnection.h>
  • Call CoInitialize
  • And write code similar to below snippet
    CMimeMessage msg;
    /*  Optional
    CSMTPConnection conn;
    BOOL bSuccess = conn.SendMessage(msg);
  • Call CoUninitialize

13 thoughts on “Sending mail in an unmanaged VC++ application

  1. Hey Eddie, CoInitialize/CoUninitialize are both required to make COM calls. In your app, perhaps CoInitialize is being called early on by some other method/class, which is why it works fine without the calls; and the reason for the crash with the calls could be that after the CoUninitialize you added, any COM calls that your app would attempt would fail.

  2. But all I did was copy your code into a new console project (weird!).

    Perhaps I’ll re-phrase the question: does atlsmtpconnection use COM? You’ll probably guess that I’m a newbie here.

  3. hi ya its fine.

    i have some doubt using mfc in vc++
    how to rename the folder name using mfc dialog based application by clicking one button
    then how to create new folder by clicking one button using mfc dialog based app

  4. hello.

    to impleamen sending mail
    in my visual c++
    i need the following header file

    i will be more than happy to get it…

    thank yoy very much!!!

    1. HI Nish,

      Can we directly use the above classes and steps without using CHttpRequest & CHttpResponse classes ? I tried with VS2012 and ATL server library from codePlex (ver 9.0.70425 Alpha) and it won’t work . Sendmessage() always return zero (server “”). please reply.


  5. Hi,
    I am getting error like………
    Error 997 (ERROR_IO_PENDING) from CSMTPConnection::SendSimple (“Overlapped I/O operation in progress”

    Dileep Kumar

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s