Skip to content

Commit c1c01fe

Browse files
author
Mathäus Mendel
committed
Merge branch 'develop' of https://github.com/pocoproject/poco into develop
2 parents 0b4d541 + 5d9fa09 commit c1c01fe

2 files changed

Lines changed: 74 additions & 0 deletions

File tree

Net/include/Poco/Net/SMTPClientSession.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,11 @@ class Net_API SMTPClientSession
148148
/// Throws a SMTPException in case of a SMTP-specific error, or a
149149
/// NetException in case of a general network communication failure.
150150

151+
void sendMessage(std::istream& istr);
152+
/// Sends the mail message from the supplied stream. Content of the stream
153+
/// is copied without any checking. Only the completion status is checked and,
154+
/// if not valid, SMTPExcpetion is thrown.
155+
151156
int sendCommand(const std::string& command, std::string& response);
152157
/// Sends the given command verbatim to the server
153158
/// and waits for a response.
@@ -162,6 +167,19 @@ class Net_API SMTPClientSession
162167
/// Throws a SMTPException in case of a SMTP-specific error, or a
163168
/// NetException in case of a general network communication failure.
164169

170+
void sendAddresses(const std::string& from, const Recipients& recipients);
171+
/// Sends the message preamble by sending a MAIL FROM command,
172+
/// and a RCPT TO command for every recipient.
173+
///
174+
/// Throws a SMTPException in case of a SMTP-specific error, or a
175+
/// NetException in case of a general network communication failure.
176+
177+
void sendData();
178+
/// Sends the message preamble by sending a DATA command.
179+
///
180+
/// Throws a SMTPException in case of a SMTP-specific error, or a
181+
/// NetException in case of a general network communication failure.
182+
165183
protected:
166184
enum StatusClass
167185
{

Net/src/SMTPClientSession.cpp

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,47 @@ void SMTPClientSession::sendCommands(const MailMessage& message, const Recipient
360360
}
361361

362362

363+
void SMTPClientSession::sendAddresses(const std::string& from, const Recipients& recipients)
364+
{
365+
std::string response;
366+
int status = 0;
367+
368+
std::string::size_type emailPos = from.find('<');
369+
if (emailPos == std::string::npos)
370+
{
371+
std::string sender("<");
372+
sender.append(from);
373+
sender.append(">");
374+
status = sendCommand("MAIL FROM:", sender, response);
375+
}
376+
else
377+
{
378+
status = sendCommand("MAIL FROM:", from.substr(emailPos, from.size() - emailPos), response);
379+
}
380+
381+
if (!isPositiveCompletion(status)) throw SMTPException("Cannot send message", response, status);
382+
383+
std::ostringstream recipient;
384+
385+
for (Recipients::const_iterator it = recipients.begin(); it != recipients.end(); ++it)
386+
{
387+
388+
recipient << '<' << *it << '>';
389+
int status = sendCommand("RCPT TO:", recipient.str(), response);
390+
if (!isPositiveCompletion(status)) throw SMTPException(std::string("Recipient rejected: ") + recipient.str(), response, status);
391+
recipient.str("");
392+
}
393+
}
394+
395+
396+
void SMTPClientSession::sendData()
397+
{
398+
std::string response;
399+
int status = sendCommand("DATA", response);
400+
if (!isPositiveIntermediate(status)) throw SMTPException("Cannot send message data", response, status);
401+
}
402+
403+
363404
void SMTPClientSession::sendMessage(const MailMessage& message)
364405
{
365406
sendCommands(message);
@@ -402,4 +443,19 @@ int SMTPClientSession::sendCommand(const std::string& command, const std::string
402443
}
403444

404445

446+
void SMTPClientSession::sendMessage(std::istream& istr)
447+
{
448+
std::string response;
449+
int status = 0;
450+
451+
SocketOutputStream socketStream(_socket);
452+
MailOutputStream mailStream(socketStream);
453+
StreamCopier::copyStream(istr, mailStream);
454+
mailStream.close();
455+
socketStream.flush();
456+
status = _socket.receiveStatusMessage(response);
457+
if (!isPositiveCompletion(status)) throw SMTPException("The server rejected the message", response, status);
458+
}
459+
460+
405461
} } // namespace Poco::Net

0 commit comments

Comments
 (0)