forked from pocoproject/poco
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSMTPClientSession.h
More file actions
254 lines (205 loc) · 8.3 KB
/
SMTPClientSession.h
File metadata and controls
254 lines (205 loc) · 8.3 KB
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
//
// SMTPClientSession.h
//
// $Id: //poco/1.4/Net/include/Poco/Net/SMTPClientSession.h#1 $
//
// Library: Net
// Package: Mail
// Module: SMTPClientSession
//
// Definition of the SMTPClientSession class.
//
// Copyright (c) 2005-2008, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// Permission is hereby granted, free of charge, to any person or organization
// obtaining a copy of the software and accompanying documentation covered by
// this license (the "Software") to use, reproduce, display, distribute,
// execute, and transmit the Software, and to prepare derivative works of the
// Software, and to permit third-parties to whom the Software is furnished to
// do so, all subject to the following:
//
// The copyright notices in the Software and this entire statement, including
// the above license grant, this restriction and the following disclaimer,
// must be included in all copies of the Software, in whole or in part, and
// all derivative works of the Software, unless such copies or derivative
// works are solely in the form of machine-executable object code generated by
// a source language processor.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//
#ifndef Net_SMTPClientSession_INCLUDED
#define Net_SMTPClientSession_INCLUDED
#include "Poco/Net/Net.h"
#include "Poco/Net/DialogSocket.h"
#include "Poco/DigestEngine.h"
#include "Poco/Timespan.h"
namespace Poco {
namespace Net {
class MailMessage;
class Net_API SMTPClientSession
/// This class implements an Simple Mail
/// Transfer Procotol (SMTP, RFC 2821)
/// client for sending e-mail messages.
{
public:
typedef std::vector<std::string> Recipients;
enum
{
SMTP_PORT = 25
};
enum LoginMethod
{
AUTH_NONE,
AUTH_CRAM_MD5,
AUTH_CRAM_SHA1,
AUTH_LOGIN,
AUTH_PLAIN
};
explicit SMTPClientSession(const StreamSocket& socket);
/// Creates the SMTPClientSession using
/// the given socket, which must be connected
/// to a SMTP server.
SMTPClientSession(const std::string& host, Poco::UInt16 port = SMTP_PORT);
/// Creates the SMTPClientSession using a socket connected
/// to the given host and port.
virtual ~SMTPClientSession();
/// Destroys the SMTPClientSession.
void setTimeout(const Poco::Timespan& timeout);
/// Sets the timeout for socket read operations.
Poco::Timespan getTimeout() const;
/// Returns the timeout for socket read operations.
void login(const std::string& hostname);
/// Greets the SMTP server by sending a EHLO command
/// with the given hostname as argument.
///
/// If the server does not understand the EHLO command,
/// a HELO command is sent instead.
///
/// Throws a SMTPException in case of a SMTP-specific error, or a
/// NetException in case of a general network communication failure.
void login();
/// Calls login(hostname) with the current host name.
void login(const std::string& hostname, LoginMethod loginMethod, const std::string& username, const std::string& password);
/// Logs in to the SMTP server using the given authentication method and the given
/// credentials.
void login(LoginMethod loginMethod, const std::string& username, const std::string& password);
/// Logs in to the SMTP server using the given authentication method and the given
/// credentials.
void open();
/// Reads the initial response from the SMTP server.
///
/// Usually called implicitly through login(), but can
/// also be called explicitly to implement different forms
/// of SMTP authentication.
///
/// Does nothing if called more than once.
void close();
/// Sends a QUIT command and closes the connection to the server.
///
/// Throws a SMTPException in case of a SMTP-specific error, or a
/// NetException in case of a general network communication failure.
void sendMessage(const MailMessage& message);
/// Sends the given mail message by sending a MAIL FROM command,
/// a RCPT TO command for every recipient, and a DATA command with
/// the message headers and content. Using this function results in
/// RCPT TO commands list generated from the recipient list supplied
/// with the message itself.
///
/// Throws a SMTPException in case of a SMTP-specific error, or a
/// NetException in case of a general network communication failure.
void sendMessage(const MailMessage& message, const Recipients& recipients);
/// Sends the given mail message by sending a MAIL FROM command,
/// a RCPT TO command for every recipient, and a DATA command with
/// the message headers and content. Using this function results in
/// message header being generated from the supplied recipients list.
///
/// Throws a SMTPException in case of a SMTP-specific error, or a
/// NetException in case of a general network communication failure.
void sendMessage(std::istream& istr);
/// Sends the mail message from the supplied stream. Content of the stream
/// is copied without any checking. Only the completion status is checked and,
/// if not valid, SMTPExcpetion is thrown.
int sendCommand(const std::string& command, std::string& response);
/// Sends the given command verbatim to the server
/// and waits for a response.
///
/// Throws a SMTPException in case of a SMTP-specific error, or a
/// NetException in case of a general network communication failure.
int sendCommand(const std::string& command, const std::string& arg, std::string& response);
/// Sends the given command verbatim to the server
/// and waits for a response.
///
/// Throws a SMTPException in case of a SMTP-specific error, or a
/// NetException in case of a general network communication failure.
void sendAddresses(const std::string& from, const Recipients& recipients);
/// Sends the message preamble by sending a MAIL FROM command,
/// and a RCPT TO command for every recipient.
///
/// Throws a SMTPException in case of a SMTP-specific error, or a
/// NetException in case of a general network communication failure.
void sendData();
/// Sends the message preamble by sending a DATA command.
///
/// Throws a SMTPException in case of a SMTP-specific error, or a
/// NetException in case of a general network communication failure.
protected:
enum StatusClass
{
SMTP_POSITIVE_COMPLETION = 2,
SMTP_POSITIVE_INTERMEDIATE = 3,
SMTP_TRANSIENT_NEGATIVE = 4,
SMTP_PERMANENT_NEGATIVE = 5
};
enum
{
DEFAULT_TIMEOUT = 30000000 // 30 seconds default timeout for socket operations
};
static bool isPositiveCompletion(int status);
static bool isPositiveIntermediate(int status);
static bool isTransientNegative(int status);
static bool isPermanentNegative(int status);
void login(const std::string& hostname, std::string& response);
void loginUsingCRAMMD5(const std::string& username, const std::string& password);
void loginUsingCRAMSHA1(const std::string& username, const std::string& password);
void loginUsingCRAM(const std::string& username, const std::string& method, Poco::DigestEngine& hmac);
void loginUsingLogin(const std::string& username, const std::string& password);
void loginUsingPlain(const std::string& username, const std::string& password);
DialogSocket& socket();
private:
void sendCommands(const MailMessage& message, const Recipients* pRecipients = 0);
void transportMessage(const MailMessage& message);
DialogSocket _socket;
bool _isOpen;
};
//
// inlines
//
inline bool SMTPClientSession::isPositiveCompletion(int status)
{
return status/100 == SMTP_POSITIVE_COMPLETION;
}
inline bool SMTPClientSession::isPositiveIntermediate(int status)
{
return status/100 == SMTP_POSITIVE_INTERMEDIATE;
}
inline bool SMTPClientSession::isTransientNegative(int status)
{
return status/100 == SMTP_TRANSIENT_NEGATIVE;
}
inline bool SMTPClientSession::isPermanentNegative(int status)
{
return status/100 == SMTP_PERMANENT_NEGATIVE;
}
inline DialogSocket& SMTPClientSession::socket()
{
return _socket;
}
} } // namespace Poco::Net
#endif // Net_SMTPClientSession_INCLUDED