# Poco::Net

# class SMTPClientSession

Library: Net
Package: Mail
Header: Poco/Net/SMTPClientSession.h

## Description

This class implements an Simple Mail Transfer Protocol (SMTP, RFC 2821) client for sending e-mail messages.

## Inheritance

Known Derived Classes: SecureSMTPClientSession

## Member Summary

Member Functions: close, getTimeout, isPermanentNegative, isPositiveCompletion, isPositiveIntermediate, isTransientNegative, login, loginUsingCRAM, loginUsingCRAMMD5, loginUsingCRAMSHA1, loginUsingLogin, loginUsingPlain, loginUsingXOAUTH2, open, sendAddresses, sendCommand, sendData, sendMessage, setTimeout, socket

## Types

### Recipients

typedef std::vector < std::string > Recipients;

## Enumerations

### Anonymous

SMTP_PORT = 25

### Anonymous

DEFAULT_TIMEOUT = 30000000

### LoginMethod

AUTH_NONE

AUTH_CRAM_MD5

AUTH_CRAM_SHA1

AUTH_LOGIN

AUTH_PLAIN

AUTH_XOAUTH2

### StatusClass

SMTP_POSITIVE_COMPLETION = 2

SMTP_POSITIVE_INTERMEDIATE = 3

SMTP_TRANSIENT_NEGATIVE = 4

SMTP_PERMANENT_NEGATIVE = 5

## Constructors

### SMTPClientSession

explicit SMTPClientSession(
const StreamSocket & socket
);

Creates the SMTPClientSession using the given socket, which must be connected to a SMTP server.

### SMTPClientSession

SMTPClientSession(
const std::string & host,
Poco::UInt16 port = SMTP_PORT
);

Creates the SMTPClientSession using a socket connected to the given host and port.

## Destructor

### ~SMTPClientSession

virtual ~SMTPClientSession();

Destroys the SMTPClientSession.

## Member Functions

### close

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.

### getTimeout

Poco::Timespan getTimeout() const;

Returns the timeout for socket read operations.

### login

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.

### login

void login();

Calls login(hostname) with the current host name.

### login

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.

### login

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.

### open

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.

### sendAddresses

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.

### sendCommand

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.

### sendCommand

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.

### sendData

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.

### sendMessage

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.

### sendMessage

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.

### sendMessage

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.

### setTimeout

void setTimeout(
const Poco::Timespan & timeout
);

Sets the timeout for socket read operations.

### isPermanentNegative

static bool isPermanentNegative(
int status
);

### isPositiveCompletion

static bool isPositiveCompletion(
int status
);

### isPositiveIntermediate

static bool isPositiveIntermediate(
int status
);

### isTransientNegative

static bool isTransientNegative(
int status
);

### login

void login(
const std::string & hostname,
std::string & response
);

### loginUsingCRAM

void loginUsingCRAM(
const std::string & username,
const std::string & method,
Poco::DigestEngine & hmac
);

### loginUsingCRAMMD5

void loginUsingCRAMMD5(
const std::string & username,
const std::string & password
);

### loginUsingCRAMSHA1

void loginUsingCRAMSHA1(
const std::string & username,
const std::string & password
);

### loginUsingLogin

void loginUsingLogin(
const std::string & username,
const std::string & password
);

### loginUsingPlain

void loginUsingPlain(
const std::string & username,
const std::string & password
);

### loginUsingXOAUTH2

void loginUsingXOAUTH2(
const std::string & username,
const std::string & password
);

### socket

DialogSocket & socket();