A simple SMTP client library built in C++ that support authentication and secure connections (Forced SSL and Opportunistic SSL/TLS encryption).
### How to build the SMTP client or integrate it in your application
Follow this link for a quick guide on how to build the SMTP client and integrate it in your application.
Release | MD5 hash of smtpclient.dll |
---|---|
c0c50a722e02dba488d6440ede046976 |
|
dce3b4c0704c8aafd4b2c5e8fdd3a701 |
See the section Releases for previous versions.
The OpportunisticSecureSMTPClient should be your default choice for communicating with modern SMTP servers. The communication is usually done via port 587.
The ForcedSecureSMTPClient is useful to communicate with legacy systems which requires that the communication be encrypted from the initial connection. The communication is usually done via port 465.
The SmtpClient should be used to communicate with internal relay servers. The communication is usually done via port 25.
- Send a plaintext email via an unsecured server
- Send an html email to 2 recipients with an attachment via an unsecured server
- Send a plaintext email via a secure server (opportunistic) -> SSL/TLS Port 587
- Send a plaintext email via a secure server (forced) -> SSL/TLS Port 465
#include "smtpclient.h"
#include <iostream>
#include <memory>
#include <stdexcept>
using namespace jed_utils;
int main()
{
SmtpClient client("<your smtp server address>", 25);
try
{
PlaintextMessage msg(MessageAddress("[email protected]", "Test Address Display"),
MessageAddress("[email protected]"),
"This is a test (Subject)",
"Hello\nHow are you?");
int err_no = client.sendMail(msg);
if (err_no != 0) {
std::cout << client.getCommunicationLog() << '\n';
std::unique_ptr<char> errorMessage(client.getErrorMessage(err_no));
std::cerr << "An error occurred: " << errorMessage.get()
<< " (error no: " << err_no << ")" << '\n';
return 1;
}
std::cout << client.getCommunicationLog() << '\n';
std::cout << "Operation completed!" << std::endl;
}
catch (std::invalid_argument &err)
{
std::cerr << err.what() << std::endl;
}
return 0;
}
#include "smtpclient.h"
#include <iostream>
#include <memory>
#include <stdexcept>
using namespace jed_utils;
int main()
{
SmtpClient client("<your smtp server address>", 25);
try
{
const int ATTACHMENT_COUNT = 1;
const int TOADDR_COUNT = 2;
Attachment att1[ATTACHMENT_COUNT] = { Attachment("C:\\Temp\\test.png", "test image.png") };
MessageAddress to_addr[TOADDR_COUNT] = { MessageAddress("[email protected]"),
MessageAddress("[email protected]")
};
HTMLMessage msg(MessageAddress("[email protected]", "Test Address Display"),
to_addr, TOADDR_COUNT,
"This is a test (Subject)",
"<html><body><h1>Hello,</h1><br/><br/>How are you?</body></html>", nullptr, 0, nullptr, 0, att1, ATTACHMENT_COUNT);
int err_no = client.sendMail(msg);
if (err_no != 0) {
std::cout << client.getCommunicationLog() << '\n';
std::unique_ptr<char> errorMessage(client.getErrorMessage(err_no));
std::cerr << "An error occurred: " << errorMessage.get()
<< " (error no: " << err_no << ")" << '\n';
return 1;
}
std::cout << client.getCommunicationLog() << '\n';
std::cout << "Operation completed!" << std::endl;
}
catch (std::invalid_argument &err)
{
std::cerr << err.what() << std::endl;
}
return 0;
}
#include "opportunisticsecuresmtpclient.h"
#include <iostream>
#include <memory>
#include <stdexcept>
using namespace jed_utils;
int main()
{
OpportunisticSecureSMTPClient client("<your smtp server address>", 587);
client.setCredentials(Credential("[email protected]", "mypassword"));
try
{
PlaintextMessage msg(MessageAddress("[email protected]", "Test Address Display"),
MessageAddress("[email protected]", "Another Adresse display"),
"This is a test (Subject)",
"Hello\nHow are you?");
int err_no = client.sendMail(msg);
if (err_no != 0) {
std::cout << client.getCommunicationLog() << '\n';
std::unique_ptr<char> errorMessage(client.getErrorMessage(err_no));
std::cerr << "An error occurred: " << errorMessage.get()
<< " (error no: " << err_no << ")" << '\n';
return 1;
}
std::cout << client.getCommunicationLog() << '\n';
std::cout << "Operation completed!" << std::endl;
}
catch (std::invalid_argument &err)
{
std::cerr << err.what() << std::endl;
}
return 0;
}
#include "forcedsecuresmtpclient.h"
#include <iostream>
#include <memory>
#include <stdexcept>
using namespace jed_utils;
int main()
{
ForcedSecureSMTPClient client("<your smtp server address>", 465);
client.setCredentials(Credential("[email protected]", "mypassword"));
try
{
PlaintextMessage msg(MessageAddress("[email protected]", "Test Address Display"),
MessageAddress("[email protected]", "Another Adresse display"),
"This is a test (Subject)",
"Hello\nHow are you?");
int err_no = client.sendMail(msg);
if (err_no != 0) {
std::cout << client.getCommunicationLog() << '\n';
std::unique_ptr<char> errorMessage(client.getErrorMessage(err_no));
std::cerr << "An error occurred: " << errorMessage.get()
<< " (error no: " << err_no << ")" << '\n';
return 1;
}
std::cout << client.getCommunicationLog() << '\n';
std::cout << "Operation completed!" << std::endl;
}
catch (std::invalid_argument &err)
{
std::cerr << err.what() << std::endl;
}
return 0;
}
See the classes documentation here