Download latest jwSMTP
Old releases
Example code
Example code 2
HowTo
MS Win details
License
jwSMTP interface
rfc 821 SMTP
johnwiggins.net
tar -xvzf jwsmtp-<version>.tar.gzThis will extract all the files into the jwsmtp-<version> directory, or use Winzip.
./configure make make installCode Usage
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>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.
boost::thread thrd(mail); // operator( )( ) called implicitly // thrd.join( ); // optional
// 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") {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.
// error }
std::vector<char> vec;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.
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
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.
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.
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,mail.authtype("mailer::PLAIN");