Sending a message
For the purpose of message sending, one must connect to an SMTP server, create a message and send it over that connection. The chosen SMTP server further relays the message to the final destination. The well known port 25 is assigned for the unencrypted communication. Sometimes it does not even require authentication, but more often it requires username and password.
So, let's assume that there is an email SMTP server at
smtp.server.com
and port 25
, which has the
account mailio@server.com
created with the password
mailiopass
. The non-encrypted connection is created by using the class
smtp
. Authentication beside the username and password requires also the authentication type
as third parameter. The message is created as in the previous chapters. At the end, it is submitted over that connection:
try
{
smtp conn("smtp.server.com", 25);
conn.authenticate("mailio@server.com", "mailiopass", smtp::auth_method_t::LOGIN);
message msg;
msg.from(mail_address("mailio library", "mailio@server.com"));
msg.add_recipient(mail_address("mailio library", "mailio@server.com"));
msg.subject("Salut, tout le monde!");
msg.content("Hello, World!");
conn.submit(msg);
}
catch (smtp_error& exc)
{
cout << exc.what() << endl;
}
catch (dialog_error& exc)
{
cout << exc.what() << endl;
}
The possible exceptions are on the SMTP level handled with smtp_error
and on the TCP level
with dialog_error
.
In case one does not need the authentication, the third parameter of
smtp::connect()
should be
smtp::auth_method_t::NONE
. Many servers support the plain connection over the port 587, so it
can be used too.