Table of Contents

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:

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:

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:

24.4.0.4 Excursus SMTP

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