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
CoInitializeCoInitialize(0);
- And write code similar to below snippet
CMimeMessage msg; msg.SetSender(strSender); msg.AddRecipient(strRecepient); /* Optional msg.SetSubject(strSubject); msg.AddText(strBody); msg.AttachFile(strAttachFile); */ CSMTPConnection conn; conn.Connect(server); BOOL bSuccess = conn.SendMessage(msg); conn.Disconnect();
- Call
CoUninitializeCoUninitialize();
w00t, that is quite close to what I was looking for today.
(
Pity it doesn’t work in VC6.
I do have a
icon Colino. Maybe you could use that next time
Waa??? You can do things without managment
Is Call CoInitialize/CoUninitialize necessary? It works without, and crashes (on exit) with them in!
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.
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.
in VC++ 6.0 This Dont Works ! in VC++ 6.0 Lib Doesnt exist atlsmtpconnection.h and its lib.
Has anyone encountered error # 997 while calling the sendMessage() function. What could be the reason for the error?
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
thanq
Me too got error#997 while calling sendMessage. has anybody got any idea how this will be resolved??
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!!!
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 “smtp.live.com”). please reply.
Anil.S
Thanks for your marvelous posting! I really enjoyed reading it, you
can be a great author. I will ensure that I bookmark
your blog and may come back down the road. I want to encourage yourself to continue your great work, have a nice
morning!