Wt: Wt::Mail::Client Class Reference

Wt 4.10.4

Loading...
Searching...
No Matches
Public Member Functions | List of all members
Wt::Mail::Client Class Reference
SMTP client library (Wt::Mail)

An SMTP mail client. More...

`#include <Wt/Mail/Client.h>`

## Public Member Functions
| Client (const std::string &selfHost=std::string())
| Constructor.
| ~Client ()
| Destructor.
void | enableAuthentication (const std::string &username, const std::string &password, AuthenticationMethod method=AuthenticationMethod::Plain)
| Enables authentication.
AuthenticationMethod | authenticationMethod () const
| Returns the authentication method.
bool | authenticationEnabled () const
| Returns whether authentication is enabled.
void | setSslCertificateVerificationEnabled (bool enabled)
| Sets whether SSL certificate verification is enabled.
bool | isSslCertificateVerificationEnabled () const
| Gets whether SSL certificate verification is enabled.
void | setTransportEncryption (TransportEncryption method)
| Sets the transport encryption method.
TransportEncryption | transportEncryption () const
| Get the encryption method.
bool | connect ()
| Connects to the default SMTP server.
bool | connect (const std::string &smtpHost, int smtpPort=25)
| Connects to a given STMP server and port.
void | disconnect ()
| Disconnects the client from the SMTP server.
bool | send (const Message &message)
| Sends a message.

## Detailed Description

An SMTP mail client.

The SMTP client can send one or more mail messages to an SMTP host.

#include <Wt/Mail/Client>
#include <Wt/Mail/Message>
Mail::Message message;
message.setFrom(Mail::Mailbox("kudos@corp.org", "Kudos Dukos");
message.addRecipient(Mail::RecipientType::To, Mail::Mailbox("koen@emweb.be", "Koen Deforche");
message.setSubject("Hey there, koen!");
message.setBody("That mail client seems to be working.");
message.addHtmlBody ("<p>"
"<a href=\"http://www.webtoolkit.eu/wt\">That mail client</a>"
" seems to be working just great!</p>");
Mail::Client client;
client.connect("localhost");
client.send(message);
Wt::Mail::Client
An SMTP mail client.
Definition Client.h:79
Wt::Mail::Client::connect
bool connect()
Connects to the default SMTP server.
Wt::Mail::Client::send
bool send(const Message &message)
Sends a message.
Wt::Mail::Mailbox
An email sender or recipient mailbox.
Definition Mailbox.h:22
Wt::Mail::Message
A mail message.
Definition Message.h:46
Wt::Mail::Message::setFrom
void setFrom(const Mailbox &from)
Sets the sender mailbox.
Wt::Mail::Message::addHtmlBody
void addHtmlBody(const WString &text)
Adds an HTML body.
Wt::Mail::Message::addRecipient
void addRecipient(RecipientType type, const Mailbox &recipient)
Adds a recipient.
Wt::Mail::Message::setSubject
void setSubject(const WString &subject)
Sets a subject.
Wt::Mail::Message::setBody
void setBody(const WString &text)
Sets the plain text body.
Wt::Mail::RecipientType::To
@ To
To: recipient.

Only the bare essentials of the SMTP protocol are current implemented, although the Message itself supports proper unicode handling.

Note
  Currently the client sends an email synchronously, and thus a slow connection to the SMTP server may block the current thread. We plan to change this in the future. It is thus beneficial to connect to a local SMTP daemon to minimize this deficiency.

## Constructor & Destructor Documentation

## ◆ Client()

Wt::Mail::Client::Client | ( | const std::string & | selfHost = std::string() | ) |

Constructor.

The `selfHost` is how the mail client will identify itself to the mail server, in the EHLO command.

If not defined, the "smtp-self-host" configuration property is used, and if that property is not defined, it defaults to "localhost".

## ◆ ~Client()

Wt::Mail::Client::~Client | ( |  | ) |

Destructor.

If the client is still connected, this disconnects the client.

## Member Function Documentation

## ◆ authenticationEnabled()

bool Wt::Mail::Client::authenticationEnabled | ( |  | ) | const

Returns whether authentication is enabled.

See also
  enableAuthentication

## ◆ authenticationMethod()

AuthenticationMethod Wt::Mail::Client::authenticationMethod | ( |  | ) | const

Returns the authentication method.

See also
  enableAuthentication

## ◆ connect() [1/2]

bool Wt::Mail::Client::connect | ( |  | ) |

Connects to the default SMTP server.

This connects to the SMTP server defined by the "smtp-host" property, on port defined by the "smtp-port" property. If these properties are not set, "localhost" and 25 are used as defaults respectively.

Returns whether the connection could be established and the SMTP hand-shake was successful.

## ◆ connect() [2/2]

bool Wt::Mail::Client::connect | ( | const std::string & | smtpHost,
|  | int | smtpPort = 25 )

Connects to a given STMP server and port.

Returns whether the connection could be established and the SMTP hand-shake was successful.

## ◆ enableAuthentication()

void Wt::Mail::Client::enableAuthentication | ( | const std::string & | username,
|  | const std::string & | password,
|  | AuthenticationMethod | method = AuthenticationMethod::Plain )

Enables authentication.

Enable authentication either through this method, or by specifying the following configuration properties:

- smtp-auth-username
- smtp-auth-password
- smtp-auth-method

"smtp-auth-method" can be one of: "none", "plain", or "login".

## ◆ isSslCertificateVerificationEnabled()

bool Wt::Mail::Client::isSslCertificateVerificationEnabled | ( |  | ) | const

Gets whether SSL certificate verification is enabled.

See also
  setSslCertificateVerificationEnabled

## ◆ send()

bool Wt::Mail::Client::send | ( | const Message & | message | ) |

Sends a message.

The client must be connected before messages can be sent.

The function returns true on success, false on error. The reason for the error is logged in the log file.

## ◆ setSslCertificateVerificationEnabled()

void Wt::Mail::Client::setSslCertificateVerificationEnabled | ( | bool | enabled | ) |

Sets whether SSL certificate verification is enabled.

Defaults to true

## ◆ setTransportEncryption()

void Wt::Mail::Client::setTransportEncryption | ( | TransportEncryption | method | ) |

Sets the transport encryption method.

The default is TransportEncryption::None

Enable transport encryption either through this method, or by specifying the "smtp-transport-encryption" configuration property. Its value can be one of: "none", "starttls", or "tls".

## ◆ transportEncryption()

TransportEncryption Wt::Mail::Client::transportEncryption | ( |  | ) | const

Get the encryption method.

See also
  setTransportEncryption