User Tools

Site Tools


Sidebar

Network and communication

k24:k24.4:start

24.4.0 Component gb.net.smtp

The component enables you to send e-mails using the SMTP protocol. Supported are, among other things, email attachments and optional connection security via SSL or TLS, provided the 'openssl' programme is installed on your system. This component gb.net.smtp is present in all Gambas3 versions since version 3.6 and was written entirely in Gambas .

24.4.0.1 Properties

The SmtpClient class has the following properties:

PropertyData typeDescription
CountIntegerReturns the number of parts of the current EMail.
MessageIDStringSets the message ID in the field 'Message-Id' in the header or returns the ID.
InReplyToStringSets the email address in the field 'In-Replay-To' in the header or returns the email address.
DebugBooleanIndicates whether debugging mode has been enabled for the SMTP client or switches the mode on. When debugging mode is enabled, all client activity is output to the Gambas IDE console.
AlternativeBooleanDetermines or sets whether mail attachments are the same data in different formats - encoded in HTML, UTF-8 or in ASCII. By default, this property is FALSE. You must define a change before the Add method.
FromStringDetermines or sets the return address of the email (sender).
ToString[]Determines or sets the list of recipients of the EMail (EMail addresses). The type of the property is a string array, so you can specify multiple recipients for the same EMail.
CcString[]Determines or sets the list of additional (Cc) recipients (carbon copy) of the EMail (EMail addresses). The type of the property is a string array, so you can specify several additional recipients for the same EMail.
BccString[]Determines or sets the list of additional (Bcc) recipients (blind carbon copy) of the email (email addresses). The type of the property is a string array, so you can specify multiple additional hidden recipients for the same email.
HostStringDetermines or sets the SMTP server to which the EMail is delivered (MTA).
PortIntegerDetermines or sets the SMTP port used by the SMTP server. The default port is 25.
UserStringDetermines or sets the user name for identification (login).
PasswordStringDetermines or sets the password required for authentication (Login).
SubjectStringDetermines or sets the subject of the email.
BodyStringDetermines or sets the EMail text. The mail text is always sent as the first part. The mime type 'text/plain; charset=utf-8' is used.
EncryptIntegerDetermines or sets the method of SMTP connection security.

Table 24.4.0.1.1 : Properties of the SmtpClient class

The constants of the SmtpClient class associated with the Encrypt property are:

  • Net.None (0) Without connection security.
  • Net.SSL (1) SSL connection security
  • Net.TLS (2) TLS connection security

24.4.0.2 Methods

The SmtpClient class has only three methods:

MethodDescription
Add ( Data As String [ , MimeType As String, Name As String ] )Adds an email attachment. Data is the data that usually comes directly from the file you want to attach. MimeType is the MIME type of the attached data. By default it is 'text/plain'. Name is the name of the attachment. It is automatically generated if not specified. Currently not all MIME types are supported.
AddHeader ( Name As String, Value As String ) Adds a user-defined header field to the header. Name is the field name and Value is the field value.
Send()The email is sent to the SMTP server (MTA). If the connection is encrypted, then the programme 'openssl' is used in the background. The openssl process is terminated when the email has been sent successfully or when an error occurred during sending.

Table 24.4.0.2.1 : Methods of the class SmtpClient

The static method FormatDate ( Date As Date ) As String formats a date according to the syntax of RFC 822: SmtpClient.FormatDate(Now) → Mon, 01 Feb 2016 18:12:36 GMT .

24.4.0.3 Authentication via CRAM-MD5

If an SMTP server allows authentication via the CRAM-MD5 (Challenge Response Authentication Mechanism, Message Digest 5) method in addition to the AUTH PLAIN and AUTH LOGIN methods, then the SMTP username is sent in plain text in the SSL tunnel to identify the user, but the SMTP password is transmitted in encrypted form:

  • The SMTP client connects to the SMTP server. The SSL connection is confirmed in → line 3 with the status code 220 and a status message.
  • The client greets with EHLO and the local host name → line 4.
  • The server responds with an overview of possible commands for further transactions in → lines 5 to 11.
  • In the example, the client decides to identify and authenticate via CRAM-MD5 and sends the command AUTH CRAM-MD5 to the SMTP server → line 12.
  • The SMTP server responds in → line 13 after the status code 334 with a base64-encoded character string - the so-called challenge - consisting of numbers, timestamp and fully qualified host name of the server.
  • The SMTP client responds with a string (response) consisting of user name, space and a 'digest'. This digest is the base64-encoded MD5 hash of a value - calculated from the challenge and the password → line 14.
  • The SMTP server checks the digest received by performing the same calculation as the SMTP client with user name and user password stored on the server and compares its result with the one received from the client. If both values match, the identified user is also authenticated. This is confirmed in → line 15 after the status code 235.

Console:

[1] hans@linux:~$ openssl s_client -quiet -connect mx.freenet.de:465
[2] ...
[3] 220 mx8.freenet.de ESMTP Exim 4.85 Tue, 02 Feb 2016 07:53:59 +0100
[4] EHLO linux
[5] 250-mx8.freenet.de Hello p4fca44f0.dip0.t-ipconnect.de [79.202.68.240]
[6] 250-SIZE 209715200
[7] 250-8BITMIME
[8] 250-ETRN
[9] 250-PIPELINING
[10] 250-AUTH LOGIN PLAIN CRAM-MD5
[11] 250 HELP
[12] AUTH CRAM-MD5
[13] 334 PDE4OTYuNjk3MTcwOTUyQHBvc3RvZmZpY2UucmVzdG9uLm1jaS5uZXQ+
[14] dGltIGI5MTNhNjAyYzdlZGE3YTQ5NWI0ZTZlNzMzNGQzODkw
[15] 235 Authentication succeeded
[16] QUIT
[17] 221 mx8.freenet.de closing connection
[18] hans@linux:~$

You can use the following function to calculate the response to be sent in line 14. The SMTP user name, the corresponding SMTP user password and the 'task' set by the server are passed as parameters:

[1] Public Function CRAMMD5(sUsername As String, sPassword As String, sChallenge64 As String) As String
[2]
[3]   Dim sChallenge, sCommand, sDigestHex, sResponse As String
[4]
[5]   sChallenge = UnBase64(sChallenge64)
[6]
[7]   sCommand = "openssl md5 -hmac " & Shell$(sPassword) & " << EOF\n" & sChallenge
[8]   Shell sCommand To sDigestHex
[9]
[10]   sDigestHex = Split(Trim(sDigestHex), "=")[1]
[11]   sResponse = Base64(sUsername & sDigestHex)
[12]
[13]   Return sResponse
[14]
[15] End ' Function CRAMMD5(...)

Notes:

  • The function value in → line 13 is the response that the server expects.
  • Since Gambas version 3.8.4, the CRAM-MD5 method can also be used.
  • The SMTP client in Chapter 24.4.1 uses the CRAM-MD5 procedure alternatively and freely selectable.

24.4.0.4 Excursus SMTP

The following excursus summarises essential, selected aspects on the subject of SMTP:

  • SMTP stands for Simple Mail Transfer Protocol.
  • The e-mail transport system contains as an element, among others, the service MUA (Mail-User-Agent), which performs the handover of an e-mail to the service MTA (Mail-Transfer-Agent), whereby an MTA is used for forwarding and delivery to a message store. Remote access to the message store is primarily governed by the Post Office Protocol (POP) - currently in version 3.
  • SMTP regulates the delivery of an email to a server as an MTA.
  • As for all internet protocols, the binding rules for SMTP exist in different descriptions - the RFC.
  • SMTP is described in RFC 821 and since 2001 in RFC 2821, where RFC 2821 contains extensions of RFC 821, which has been in force since 1982.
  • SMTP is a client-server protocol and requires an active TCP/IP connection between client and server, which the client establishes.
  • SMTP is a text-based protocol.
  • SMTP initiates transactions between client and server by the client sending commands.
  • After each command, the client waits a finite time for the server response (timeout).
  • A server response begins with a status code (number of three digits) followed by a (non-normalised) status message.
  • SMTP relies on the transport protocol (default). The default port is 25. For an encrypted connection between client and server, SSL switches to port 465 and TLS switches to 587.
The website uses a temporary session cookie. This technically necessary cookie is deleted when the browser is closed. You can find information on cookies in our privacy policy.
k24/k24.4/start.txt · Last modified: 16.08.2022 (external edit)

Page Tools