Skip to content

A simple SMTP client library built in C++ that support authentication and secure connections (Forced SSL and Opportunistic SSL/TLS encryption)

License

Notifications You must be signed in to change notification settings

killerovsky/CPP-SMTPClient-library

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Jed# C++ SMTP Client Library

A simple SMTP client library built in C++ that support authentication and secure connections (Forced SSL and Opportunistic SSL/TLS encryption).

For TLS 1.3 support you must build the library against OpenSSL 1.1.1

The library is cross-platform and has been tested on Linux and Windows.


### 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.

Download latest binaries

Windows

Release MD5 hash of smtpclient.dll

v1.1.4 (x64)

c0c50a722e02dba488d6440ede046976

v1.1.4 (x86)

dce3b4c0704c8aafd4b2c5e8fdd3a701

See the section Releases for previous versions.

The 3 client classes

OpportunisticSecureSMTPClient

The OpportunisticSecureSMTPClient should be your default choice for communicating with modern SMTP servers. The communication is usually done via port 587.

ForcedSecureSMTPClient

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.

SmtpClient

The SmtpClient should be used to communicate with internal relay servers. The communication is usually done via port 25.

How it works

Some examples


Send a plaintext email via an unsecured server

#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;
}

Send an html email to 2 recipients with an attachment via an unsecured server

#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;
}

Send a plaintext email via a secure server (opportunistic)

#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;
}

Send a plaintext email via a secure server (forced)

#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;
}

Unit tests

How to run the unit tests

Documentation

See the classes documentation here

About

A simple SMTP client library built in C++ that support authentication and secure connections (Forced SSL and Opportunistic SSL/TLS encryption)

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C++ 89.4%
  • CMake 9.1%
  • C 1.5%