jwsmtp

Get jwSMTP C++ code for sending email at SourceForge.net. Fast, secure and Free Open Source software downloads latest release V1.32.15 md5sum: 8f634f3be6118dc5fb2f43fb5e25af36

jwSMTP is a C++ library/code (GPL license) to facilitate sending email programmatically. New in version 1.32.3 send mail in html format. All you need worry about is who the mail is from, who to send it to and the message itself, no network coding necessary. jwSMTP can send attachments, send to multiple recipients including BCC CC recipients. LOGIN & PLAIN SMTP authentication. Do an MX lookup or send direct via an smtp server.

Download the code above. uncompress the source code e.g:

tar -xvzf jwsmtp-<version>.tar.gz

This will extract all the files into the jwsmtp-<version> directory, or use Winzip.
If you have Visual C++ double click the mail.dsw file in the main directory, this will open the project.
If you are using some flavor of unix type at the command line:

./configure
make
make install

Code Usage
Two function calls is all it takes to send the mail. As an example here is how it can be used.
To use the mailer class:-

mailer mail(“myspiffyfriend@spiffy.com”, // who the mail is too
“someone@somewhere.net”, // who the mail is from
“There is always room for FooBar”, // subject for the email
“Foo\nBar”, // content of the message
“ns.somewhere.net”); // the nameserver to contact
// to query for an MX record

mail.send( );

To use with boost::thread http://www.Boost.org:

#include <boost/thread/thread.hpp>
boost::thread thrd(mail); // operator( )( ) called implicitly
// thrd.join( ); // optional

If your not using boost::thread then just call operator( )( ) directly or call the send( ) function, which calls operator( )( ) implicitly. That’s it. If there is an error however. The class members will not throw an exception or report failure. To find if the mail was delivered correctly use the response method of class mailer, e.g.

// A valid response will begin with the string “250”
// see rfc 821 for response codes, section 4.2.2.
if(mail.response( ).substr(0,3) != “250”) {
// error
}

Alternatively you can use the second constructor for mailer passing a vector as the message body. The example below also shows how to send directly to an SMTP server rather than querying dns for MX records.

std::vector<char> vec;
std::string mess(“Foo\nBar”);
for(std::string::size_type i = 0; i < mess.length( ); ++i)
vec.push_back(mess[i]);

mailer mail(“myspiffyfriend@spiffy.com”, // who the mail is too
“someone@somewhere.net”, // who the mail is from
“There is always room for FooBar”, // subject for the email
vec, // content of the message
“mail.somewhere.net”, // the smtp server to mail to
mailer::SMTP_PORT, // default smtp port (25)
false); // do not query MX records,
// mail directly to mail.somewhere.net

To send to mutiple recipients (to multiple email addresses), construct an object of the mailer class as above. Then call the addrecipient method (before calling mailer::operator ( ) ( ) ) to add email addresses to send the mail to. e.g.

mail.addrecipient(“someoneElse@somewhere.net”);
// blind carbon copy this next address
mail.addrecipient(“whooton@somewhere.net”, mailer::Bcc);

The mail will now be sent to to these two addresses aswell as the address passed in the constructor.
To remove a recipient from the recipients list call the removerecipient method with the exact address passed into the constructor or the addrecipient methods. e.g.

mail.removerecipient(“someoneElse@somewhere.net”);

To remove all recipients from the recipients list call the clearrecipients method. e.g.

mail.clearrecipients( );

After construction the body of the mail & the subject of the mail can be changed using the setmessage & setsubject functions respectively. e.g.

mail.setmessage(“The new message that will be received”);
mail.setsubject(“Always room for improvement”);

To add an attachment use the attach function e.g.

mail.attach(“attachment.jpg”); // relative to the current directory
mail.attach(“D:\\adir\\attachment.jpg”); // windows full path
mail.attach(“/home/me/myprogram”); // unix full path

When the mail is sent these files will automatically be sent as part of the message also. Similarly to remove an attachment call the removeattachment function. e.g.

mail.removeattachment(“/home/me/myprogram”);

To reset the mailer object (clearing all recipients, message & errors if any) call the reset function. e.g.

mail.reset( );

To use the mail object again, add recipients and a new message and optionally a new subject also, then call operator( ) to have the new message mailed to the new recipients. The nameserver/smtpserver can be changed using the setserver function, simararily the sender can be changed using the setsender function. reset does not change these values, so sending to a different server or using a different nameserver will need setserver being called and likewise to change who the mail is from will need a call to setsender.

If the server to which you are connecting requires authentication set
the username/password pair before calling send. e.g.

mail.username(“loginname”);
mail.password(“secret”);

To stop using authentication call the username function with the empty string.

mail.username(“”);

Currently only LOGIN and PLAIN authentication are supported, LOGIN by default,
to set to PLAIN call the authtype function

mail.authtype(mailer::PLAIN);

Any BUG reports, things for further inclusion or general questions email: