-
Notifications
You must be signed in to change notification settings - Fork 21
/
send-mail.cpp
136 lines (108 loc) · 4.06 KB
/
send-mail.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include <iostream>
#include <cpp/opportunisticsecuresmtpclient.hpp>
#include <cpp/plaintextmessage.hpp>
#include <cpp/htmlmessage.hpp>
namespace email = jed_utils::cpp;
static const std::string kSenderAddress = "[email protected]";
static const std::string kPassword = "changeme"; // This will be your access token
template<typename T>
class ClientWrapper {
public:
ClientWrapper(T client,
email::Credential&& credentials) : mClient{std::move(client)} {
mClient.setCredentials(credentials);
}
auto client() -> T& { return mClient; }
private:
email::OpportunisticSecureSMTPClient mClient;
};
auto buildClient() -> email::OpportunisticSecureSMTPClient& {
static ClientWrapper clientWrapper {
email::OpportunisticSecureSMTPClient{"smtp.gmail.com", 587},
email::Credential(kSenderAddress, kPassword,
jed_utils::RecommendedAuthenticationMethod::kXOauth2)
};
return clientWrapper.client();
}
void sendTextEmail() {
using email::PlaintextMessage;
using email::MessageAddress;
auto& client = buildClient();
PlaintextMessage msg(MessageAddress(kSenderAddress, "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::cerr << client.getCommunicationLog() << '\n';
std::string errorMessage = client.getErrorMessage(err_no);
std::stringstream err{};
err << "An error occurred: " << errorMessage << " (error no: " << err_no << ")";
throw std::invalid_argument{err.str()};
}
std::cout << client.getCommunicationLog() << '\n';
std::cout << "Operation completed!" << std::endl;
}
void generateTestFile() {
std::ofstream out{"/tmp/test.txt"};
out << "Generated content" << std::endl;
out.flush();
out.close();
}
void sendAttachmentEmail() {
using email::MessageAddress;
using email::Attachment;
using email::PlaintextMessage;
generateTestFile();
const auto to = MessageAddress(kSenderAddress);
const auto& from = to;
const auto subject = "This is an email with an attachment";
const auto body = "See attachment";
const auto attachments = {
Attachment{"/tmp/test.txt", "test.txt", "test.txt"},
};
PlaintextMessage msg{
from, { to }, subject, body, {}, {}, attachments,
};
auto& client = buildClient();
int err_no = client.sendMail(msg);
if (err_no != 0) {
std::cerr << client.getCommunicationLog() << '\n';
std::string errorMessage = client.getErrorMessage(err_no);
std::stringstream err{};
err << "An error occurred: " << errorMessage << " (error no: " << err_no << ")";
throw std::invalid_argument{err.str()};
}
std::cout << client.getCommunicationLog() << '\n';
std::cout << "Operation completed!" << std::endl;
}
void sendHTMLEmail() {
using email::MessageAddress;
using email::Attachment;
using email::HTMLMessage;
const auto to = MessageAddress(kSenderAddress);
const auto& from = to;
const auto subject = "This is an email with an attachment";
const auto body = "<html><body><h1>This is a html formatted email</h1></body></html>";
const auto attachments = {
Attachment{"/tmp/test.txt", "test.txt", "test.txt"},
};
HTMLMessage msg(from, { to }, subject, body, {}, {}, attachments);
auto& client = buildClient();
int err_no = client.sendMail(msg);
if (err_no != 0) {
std::cerr << client.getCommunicationLog() << '\n';
std::string errorMessage = client.getErrorMessage(err_no);
std::stringstream err{};
err << "An error occurred: " << errorMessage << " (error no: " << err_no << ")";
throw std::invalid_argument{err.str()};
}
std::cout << client.getCommunicationLog() << '\n';
std::cout << "Operation completed!" << std::endl;
}
auto main() -> int {
sendTextEmail();
sendAttachmentEmail();
sendHTMLEmail();
return 0;
}