1 | // |
---|
2 | // SMTPClientSession.h |
---|
3 | // |
---|
4 | // $Id: //poco/1.3/Net/include/Poco/Net/SMTPClientSession.h#5 $ |
---|
5 | // |
---|
6 | // Library: Net |
---|
7 | // Package: Mail |
---|
8 | // Module: SMTPClientSession |
---|
9 | // |
---|
10 | // Definition of the SMTPClientSession class. |
---|
11 | // |
---|
12 | // Copyright (c) 2005-2008, Applied Informatics Software Engineering GmbH. |
---|
13 | // and Contributors. |
---|
14 | // |
---|
15 | // Permission is hereby granted, free of charge, to any person or organization |
---|
16 | // obtaining a copy of the software and accompanying documentation covered by |
---|
17 | // this license (the "Software") to use, reproduce, display, distribute, |
---|
18 | // execute, and transmit the Software, and to prepare derivative works of the |
---|
19 | // Software, and to permit third-parties to whom the Software is furnished to |
---|
20 | // do so, all subject to the following: |
---|
21 | // |
---|
22 | // The copyright notices in the Software and this entire statement, including |
---|
23 | // the above license grant, this restriction and the following disclaimer, |
---|
24 | // must be included in all copies of the Software, in whole or in part, and |
---|
25 | // all derivative works of the Software, unless such copies or derivative |
---|
26 | // works are solely in the form of machine-executable object code generated by |
---|
27 | // a source language processor. |
---|
28 | // |
---|
29 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
30 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
31 | // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT |
---|
32 | // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE |
---|
33 | // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, |
---|
34 | // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
35 | // DEALINGS IN THE SOFTWARE. |
---|
36 | // |
---|
37 | |
---|
38 | |
---|
39 | #ifndef Net_SMTPClientSession_INCLUDED |
---|
40 | #define Net_SMTPClientSession_INCLUDED |
---|
41 | |
---|
42 | |
---|
43 | #include "Poco/Net/Net.h" |
---|
44 | #include "Poco/Net/DialogSocket.h" |
---|
45 | #include "Poco/Timespan.h" |
---|
46 | |
---|
47 | |
---|
48 | namespace Poco { |
---|
49 | namespace Net { |
---|
50 | |
---|
51 | |
---|
52 | class MailMessage; |
---|
53 | |
---|
54 | |
---|
55 | class Net_API SMTPClientSession |
---|
56 | /// This class implements an Simple Mail |
---|
57 | /// Transfer Procotol (SMTP, RFC 2821) |
---|
58 | /// client for sending e-mail messages. |
---|
59 | { |
---|
60 | public: |
---|
61 | enum |
---|
62 | { |
---|
63 | SMTP_PORT = 25 |
---|
64 | }; |
---|
65 | |
---|
66 | enum LoginMethod |
---|
67 | { |
---|
68 | AUTH_NONE, |
---|
69 | AUTH_CRAM_MD5, |
---|
70 | AUTH_LOGIN |
---|
71 | }; |
---|
72 | |
---|
73 | explicit SMTPClientSession(const StreamSocket& socket); |
---|
74 | /// Creates the SMTPClientSession using |
---|
75 | /// the given socket, which must be connected |
---|
76 | /// to a SMTP server. |
---|
77 | |
---|
78 | SMTPClientSession(const std::string& host, Poco::UInt16 port = SMTP_PORT); |
---|
79 | /// Creates the SMTPClientSession using a socket connected |
---|
80 | /// to the given host and port. |
---|
81 | |
---|
82 | virtual ~SMTPClientSession(); |
---|
83 | /// Destroys the SMTPClientSession. |
---|
84 | |
---|
85 | void setTimeout(const Poco::Timespan& timeout); |
---|
86 | /// Sets the timeout for socket read operations. |
---|
87 | |
---|
88 | Poco::Timespan getTimeout() const; |
---|
89 | /// Returns the timeout for socket read operations. |
---|
90 | |
---|
91 | void login(const std::string& hostname); |
---|
92 | /// Greets the SMTP server by sending a EHLO command |
---|
93 | /// with the given hostname as argument. |
---|
94 | /// |
---|
95 | /// If the server does not understand the EHLO command, |
---|
96 | /// a HELO command is sent instead. |
---|
97 | /// |
---|
98 | /// Throws a SMTPException in case of a SMTP-specific error, or a |
---|
99 | /// NetException in case of a general network communication failure. |
---|
100 | |
---|
101 | void login(); |
---|
102 | /// Calls login(hostname) with the current host name. |
---|
103 | |
---|
104 | void login(LoginMethod loginMethod, const std::string& username, const std::string& password); |
---|
105 | /// Logs in to the SMTP server using the given authentication method and the given |
---|
106 | /// credentials. |
---|
107 | |
---|
108 | void open(); |
---|
109 | /// Reads the initial response from the SMTP server. |
---|
110 | /// |
---|
111 | /// Usually called implicitly through login(), but can |
---|
112 | /// also be called explicitly to implement different forms |
---|
113 | /// of SMTP authentication. |
---|
114 | /// |
---|
115 | /// Does nothing if called more than once. |
---|
116 | |
---|
117 | void close(); |
---|
118 | /// Sends a QUIT command and closes the connection to the server. |
---|
119 | /// |
---|
120 | /// Throws a SMTPException in case of a SMTP-specific error, or a |
---|
121 | /// NetException in case of a general network communication failure. |
---|
122 | |
---|
123 | void sendMessage(const MailMessage& message); |
---|
124 | /// Sends the given mail message by sending a MAIL FROM command, |
---|
125 | /// a RCPT TO command for every recipient, and a DATA command with |
---|
126 | /// the message headers and content. |
---|
127 | /// |
---|
128 | /// Throws a SMTPException in case of a SMTP-specific error, or a |
---|
129 | /// NetException in case of a general network communication failure. |
---|
130 | |
---|
131 | int sendCommand(const std::string& command, std::string& response); |
---|
132 | /// Sends the given command verbatim to the server |
---|
133 | /// and waits for a response. |
---|
134 | /// |
---|
135 | /// Throws a SMTPException in case of a SMTP-specific error, or a |
---|
136 | /// NetException in case of a general network communication failure. |
---|
137 | |
---|
138 | int sendCommand(const std::string& command, const std::string& arg, std::string& response); |
---|
139 | /// Sends the given command verbatim to the server |
---|
140 | /// and waits for a response. |
---|
141 | /// |
---|
142 | /// Throws a SMTPException in case of a SMTP-specific error, or a |
---|
143 | /// NetException in case of a general network communication failure. |
---|
144 | |
---|
145 | protected: |
---|
146 | enum StatusClass |
---|
147 | { |
---|
148 | SMTP_POSITIVE_COMPLETION = 2, |
---|
149 | SMTP_POSITIVE_INTERMEDIATE = 3, |
---|
150 | SMTP_TRANSIENT_NEGATIVE = 4, |
---|
151 | SMTP_PERMANENT_NEGATIVE = 5 |
---|
152 | }; |
---|
153 | enum |
---|
154 | { |
---|
155 | DEFAULT_TIMEOUT = 30000000 // 30 seconds default timeout for socket operations |
---|
156 | }; |
---|
157 | |
---|
158 | static bool isPositiveCompletion(int status); |
---|
159 | static bool isPositiveIntermediate(int status); |
---|
160 | static bool isTransientNegative(int status); |
---|
161 | static bool isPermanentNegative(int status); |
---|
162 | |
---|
163 | void login(const std::string& hostname, std::string& response); |
---|
164 | void loginUsingCRAM_MD5(const std::string& username, const std::string& password); |
---|
165 | void loginUsingLogin(const std::string& username, const std::string& password); |
---|
166 | void loginUsingPlain(const std::string& username, const std::string& password); |
---|
167 | |
---|
168 | private: |
---|
169 | DialogSocket _socket; |
---|
170 | bool _isOpen; |
---|
171 | }; |
---|
172 | |
---|
173 | |
---|
174 | // |
---|
175 | // inlines |
---|
176 | // |
---|
177 | inline bool SMTPClientSession::isPositiveCompletion(int status) |
---|
178 | { |
---|
179 | return status/100 == SMTP_POSITIVE_COMPLETION; |
---|
180 | } |
---|
181 | |
---|
182 | |
---|
183 | inline bool SMTPClientSession::isPositiveIntermediate(int status) |
---|
184 | { |
---|
185 | return status/100 == SMTP_POSITIVE_INTERMEDIATE; |
---|
186 | } |
---|
187 | |
---|
188 | |
---|
189 | inline bool SMTPClientSession::isTransientNegative(int status) |
---|
190 | { |
---|
191 | return status/100 == SMTP_TRANSIENT_NEGATIVE; |
---|
192 | } |
---|
193 | |
---|
194 | |
---|
195 | inline bool SMTPClientSession::isPermanentNegative(int status) |
---|
196 | { |
---|
197 | return status/100 == SMTP_PERMANENT_NEGATIVE; |
---|
198 | } |
---|
199 | |
---|
200 | |
---|
201 | } } // namespace Poco::Net |
---|
202 | |
---|
203 | |
---|
204 | #endif // Net_SMTPClientSession_INCLUDED |
---|