-
Notifications
You must be signed in to change notification settings - Fork 21
Some examples with pure c interface
Jeremy Dumais edited this page Dec 31, 2022
·
1 revision
- 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
- Send a plaintext email via an unsecured server
- Send an html email to 2 recipients with an attachment via an unsecured server
#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 Address 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 Address 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 "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;
}