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 CurlMailAddress.Create("ivanov@server.ru");
To_ := New CurlMailAddress.Create("petrov@server.ru");
Message := New CurlMailMessage.CreateWithFromAndTo(From_, To_);
Message.Subject := "Test message";
Message.Body := "Message text";
Client := New CurlSmtpClient.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 CurlSmtpClient.CreateWithHost("example.server.ru");
Credential := New CurlNetworkCredential.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 INetSmtpClient.Port property or use the CurlSmtpClient.CreateWithHostAndPort method for initializing SMTP client. When working via the 465 port, one should also enable SSL encryption by setting the INetSmtpClient.EnableSsl property to True.
//...
Client := New CurlSmtpClient.CreateWithHost("example.server.ru");
Credential := New CurlNetworkCredential.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: