Skip to content

Commit ff0d090

Browse files
committed
added more send* members
Added: void sendMessage(std::istream& istr); void sendAddresses(const std::string& from, const Recipients& recipients); void sendData();
1 parent 221f526 commit ff0d090

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
@@ -365,6 +365,47 @@ void SMTPClientSession::sendCommands(const MailMessage& message, const Recipient
365365
}
366366

367367

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

410451

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

0 commit comments

Comments
 (0)