To work with SMTP clients in the Fore language, use the INetSmtpClient interface. To send emails using SMPT client in the code, execute the following:
Create sender and recipient email addresses.
Create an email, fill in its parameters (specify sender, recipient, header, email text and other required parameters).
Initialize SMTP client, set up its parameters (specify SMTP server and port, via which email will be sent, if required, specify other parameters).
Send the email.
In general this algorithm is implemented as follows:
Sub SendMessage;
Var
From_, To_: INetMailAddress;
Message: INetMailMessage;
Client: INetSmtpClient;
Credential: INetNetworkCredential;
Begin
From_ := New NetMailAddress.Create("ivanov@server.ru");
To_ := New NetMailAddress.Create("petrov@server.ru");
Message := New NetMailMessage.CreateWithFromAndTo(From_, To_);
Message.Subject := "Test message";
Message.Body := "Message text";
Client := New NetSmtpClient.CreateWithHost("example.server.ru");
Client.Send(Message);
End Sub SendMessage;
If it is required to authorize in the SMTP server, add the following to the code:
//...
Client := New NetSmtpClient.CreateWithHost("example.server.ru");
Credential := New NetNetworkCredential.Create("ivanov", "password", "work");
Client.ThisHostCredentials("") := Credential;
//...
By default, working with SMTP servers is executed via the port 25. This is a basic SMTP port, and for security reasons it can be closed at the SMTP server. To work via another port, specify its address in the Port property or use the CreateWithHostAndPort method for initializing SMTP client. On working via the port 465 it is also required to enable SSL encryption, to do this, set the EnableSsl property to True.
//...
Client := New NetSmtpClient.CreateWithHost("example.server.ru");
Credential := New NetNetworkCredential.Create("ivanov", "password", "work");
Client.Port := 465;
Client.EnableSsl := True;
Client.ThisHostCredentials("") := Credential;
//...
If the SMTP server uses user authentication protocol, specify its name when setting user credentials:
//...
Client.ThisHostCredentials("Kerberos") := Credential;
//...
Below is the list of SMPT server settings, which are required to specify in the code to send emails:
SMTP server | Port | SSL | Description |
smtp.gmail.com | 587(TLS), 465(SSL) | + | SMTP server of Google company. |
smtp.mail.com | 465 | + | SMTP server of Mail.ru company. |
smtp.yandex.ru | 465 | + | SMTP server of Yandex company. |
smtp.mail.yahoo.com plus.smtp.mail.yahoo.com |
465 | + | SMTP server of Yahoo company. |
See also: