To add an automatic email function to a program

Source: Internet
Author: User
Tags mail account
to add an automatic email function to a program(2009-09-19 00:21:31) reprint
Tags: smtp email it Category: Development

Recently in the development of the Department of software products to add the crash, a lot of research on the Windows platform under the debug technology. The final Solution for minidump file + email reporting mode, for the relevant summary of debug I will write another, this article first summed up the automatic email function to achieve the method. In addition, I also wrote a demo program, although the UI is relatively simple, but the basic function of sending email has been, and used 5 kinds of library implementation, one is to compare the performance of the library, the second is through the comparison library implementation, we can learn the code design and implementation skills.

In fact, today's software applications and network integration more and more closely, we have a lot of benefits to the software plus email delivery function. For example, you can send software error report information, in favor of software debug, you can add the user opinion and the use of experience in the help of information, through the mail rollup upgrade, you can uninstall the software users to choose the reason to uninstall, collect user usage habits.

The protocol used to send a message is SMTP (Simple Mail Transfer Protocol, Easy Message Transfer Protocol), a TCP/IP based upper layer application protocol that defines the routing rules for messages from source to destination. For the specific content of the SMTP protocol, you can refer to the relevant information. We need to send an email, two conditions are required:

1. Libraries that support SMTP

The code that completes the SMTP function, because SMTP is very simple, is often a wrapper class, or a set of APIs. Of course, you can also use the socket to implement SMTP, but if not for practicing, or directly with the existing open source code, after all, simple and stable.

2, a send a mailbox account and a receiving mailbox account

e-mail account should not be difficult, everyone casually to apply for two free mailbox on the line. What needs to be explained is that when sending a message, you must log in to the mail server to authenticate according to the account number and password, so the sending mailbox must support SMTP access, for example, 126 mailboxes will no longer support SMTP access to new applicants since 07, so 126 of the email accounts have to be applied before 07. Otherwise, it will return a 550 error code at the time of authentication, prompting the user to be locked. There are also some mailboxes such as Gmail need SSL support encryption method, for some libraries may not support.

Let's take a look at the demo program interface to see what basic parameters are needed to send the message.

The parameters include: send-side mailbox accounts and passwords, receive-side mailbox accounts, mail topics, content, attachments and other information, and also specify the mail to send the server address, which we think is "" form, such as in the figure set the sending server address is The general library also supports specifying the sender user name, authentication mode, multiple receive addresses, CC addresses, HTML format body, multiple attachments, and more.

Here are a few of the more good SMTP libraries, are developed in C + +.

1, Jwsmtp (

JWSMTP is an open source library, supporting Cross-platform, SMTP commonly used features are good, and very convenient to apply. JWSMTP provides an encapsulation class with simple and Easy-to-use interfaces. The most important thing is that it has a very sophisticated documentation and examples, so it is the library that I finally chose to apply. In the application process, the library runs very stable, did not find any problems, we recommend the use of JWSMTP.

Here is the application code:

Jwsmtp::mailer Mail ((LPCTSTR) M_strtouser, (LPCTSTR) M_strformuser, (LPCTSTR) M_strsubject, (LPCTSTR) M_strContent, ( LPCTSTR) M_strserver, Jwsmtp::mailer::smtp_port, false);

Mail.username ((LPCTSTR) m_strformuser);

Mail.password ((LPCTSTR) m_strfrompassword);

Mail.authtype (Jwsmtp::mailer::P lain);

Mail.attach ((LPCTSTR) m_strattachment);

Mail.send ();

std::string strcode = Mail.response ();

2. Windows MAPI

Microsoft has provided Windows system developers with the development interface for the SMTP protocol--MAPI (Messaging application progrmming Interface). MAPI is still more complex, and if we just send a message, using Simple MAPI in it is sufficient and easy to use. Simple MAPI provides a set of API functions and related structure definitions, as long as you include the mapi.h header file in your code. However, to apply MAPI, you must have Windows Mail system support, such as installing applications such as Outlook, and configure profile in them.

The development step is: Load Mapi32.dll library-> through the GetProcAddress function to get API function address-> set MAPIMessage and other structural parameters-> call MAPISendMail function to send mail.

Because of the many limitations in MAPI development, we are not very convenient to apply and interested in studying the code in the demo.

3, free libsmtp (

Free LIBSMTP is also an open source C + + library, from the packaging structure to see it provides two classes, more complex than JWSMTP, object-oriented effect is not too ideal. Its biggest flaw is that it has not yet supported add-on functionality, and I suspect it may be that I did not find the newer version, but Sourcesforge did not. It features a corresponding exception class that supports exceptions to make your code more secure.

Here is the application code:



Mail::smtp Smtp ((LPCTSTR) M_strserver, StrName, (LPCTSTR) m_strfrompassword);

Mail::mail Email ((LPCTSTR) M_strformuser, (LPCTSTR) m_strtouser);

Email.setsubject ((LPCTSTR) m_strsubject);

Email.setmessage ((LPCTSTR) m_strcontent);

Smtp.sendmail (email);


catch (mail::exception& e)


printf (e.why (). C_STR ());



4, Smailer (

This is the domestic ITER write encapsulation class, it is good to use. But like free libsmtp, encapsulation does not do too well, is inconvenient to use, and supports exceptions.

The following is the application code:

Mutils::winsockhelper Wshelper;

Smailer::mailinfo info;

Info.setsendername ((LPCTSTR) m_strformuser);

Info.setsenderaddress ((LPCTSTR) m_strformuser);

Info.addreceiver ((LPCTSTR) M_strtouser, (LPCTSTR) m_strtouser);

Info.setsubject ((LPCTSTR) m_strsubject);

Info.setpriority (smailer::P riority::normal);

Info.addmimecontent (&smailer::textplaincontent (LPCTSTR) m_strcontent));

Info.addmimecontent (&smailer::appoctstrmcontent (LPCTSTR) m_strattachment));



Smailer::mailsender Sender (LPCTSTR) M_strserver, (LPCTSTR) M_strformuser, (LPCTSTR) m_strfrompassword);

Sender.setmail (&smailer::mailwrapper (&info));

Sender.sendmail ();


catch (smailer::mailexception& e)


printf (E.what ());



catch (...)




5, Smtp

The last one was inadvertently found on the Internet, did not find the official name and homepage, let's call it. Look at the source code is written so long ago, so I use the time did not send success, there is no time to debug specific reasons. There are many kinds of packages inside, and it is troublesome to use them. Put in the reason, one is that it is written for MFC applications, the second is to study the source code, because the data structure does not use the STL, many are designed by themselves.

OK, first introduced here, demo source in the following, we can look at the reference.


(Click the Free button in the form in the open Web page, and then fill in the Verification code, click Download.)

Or go to My network disk to download: Http://


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.