The described SMTP client extends in a straightforward way the project for long-term measurement of temperatures in → Chapter 13.6 TrayIcon. In this real experiment, temperatures are measured and logged and are to be sent as an email via SMTP in this project. An elaborate GUI can be dispensed with because the programme for long-term measurement is sent to the system menu bar. There, only a tray icon reminds the user that the programme is working in the background.
The basic idea for the project is,
Figure 24.4.3.1: Excerpt from the display in the email client Thunderbird
Here is the source code that implements the above idea in simulated form with randomly generated measurement data. An adaptation to real projects should not be a problem:
' Gambas class file Public hSMTPClient As SMTPClient Public Sub Form_Open() FMain.Center FMain.Resizable = False TimerSend.Delay = 5000 ' 5 Sekunden für Tests TimerSend.Start RMkDir(Application.Path &/ "Logs") End ' Form_Open() Public Sub TimerSend_Timer() Dim sText, sBasisPath, sArchivPath, sLogPath As String Dim Cp As New Compress Randomize sText = gb.NewLine & "Protokoll zum Langzeit-Versuch LongTerm_TU" & gb.NewLine sText &= String$(46, "-") & gb.NewLine & gb.NewLine sText &= "Datum: " & Format(Now, "dd.mm.yyyy") & " Zeit: " & Format(Now, "hh:nn:ss") & " Uhr" sText &= gb.NewLine & "Mess-Station: Fuchsbau (52.7904° | 11.7533°)" sText &= gb.NewLine & gb.NewLine & String$(44, "-") & gb.NewLine sText &= "Temperatur = " & Str(Rand(16, 23)) & " °C" & gb.NewLine sText &= "Luftdruck = " & Str(Rand(977, 1023)) & " hPa" & gb.NewLine sText &= String$(44, "-") & gb.NewLine & gb.NewLine & gb.NewLine sText &= "Die Protokoll-Datei liegt im Anhang!" sBasisPath = Application.Path &/ "Logs" &/ Format(Now, "dd.mm.yyyy-hh:nn:ss") & "_log.txt" sLogPath = sBasisPath File.Save(sLogPath, sText) Wait sArchivPath = sBasisPath & ".gz" Cp.Type = "zlib" Cp.File(sLogPath, sArchivPath, 9) ' Kompression Wait SendProtocol(sText, sArchivPath) Kill sLogPath End ' TimerSend_Timer() Public Sub SendProtocol(EMailText As String, ArchivPath As String) Dim sMessage As String If hSMTPClient Then hSMTPClient = Null hSMTPClient = New SMTPClient ' hSMTPClient.Debug = True ' Nur für Testzwecke einschalten ' SMTP-Server hSMTPClient.Host = "smtp.server.net" ' <<--- ERSETZEN ' Port: hSMTPClient.Port = 465 ' <<--- PRÜFEN UND ERSETZEN ' Verbindungssicherheit hSMTPClient.Encrypt = Net.SSL ' <<--- PRÜFEN UND ERSETZEN ' EMail-Absender: Konto-Daten (Username und Passwort) hSMTPClient.User = "userXY" ' <<--- ERSETZEN hSMTPClient.Password = "passwordXY" ' <<--- ERSETZEN ' EMail-Absender: EMail-Adresse hSMTPClient.From = "Vorname Nachname <xyz@smtp.server.net>" ' <<--- ERSETZEN ' Liste der EMail-Empfänger hSMTPClient.To.Add("abc@freenet.de") ' <<--- ERSETZEN ' hSMTPClient.To.Add("to2@freenet.de") ' <<--- ERSETZEN ' Option: Liste zusätzlicher EMail-Empfänger (CC = CarbonCopy) ' hSMTPClient.Cc.Add("echo@tu-berlin.de") ' <<--- ERSETZEN ODER LÖSCHEN ' Option: Liste zusätzlicher, versteckter EMail-Empfänger (BCC = BlindCarbonCopy) ' hSMTPClient.Bcc.Add("echo@tu-berlin.de") ' <<--- ERSETZEN ODER LÖSCHEN ' Inhalt der Betreff-Zeile hSMTPClient.Subject = "Protokoll vom " & Format(Now, "dd.mm.yyyy hh:nn:ss") ' EMail-Text (Mime-Typ: text/plain;charset=utf-8) hSMTPClient.Body = EMailText ' Option: EMail-Anhänge ' Syntax: hSMTP.Add(Datei-Pfad, [ Mime-Typ, anzuzeigender Datei-Name ]) hSMTPClient.Add(File.Load(ArchivPath),"application/gzip",Format(Now,"dd.mm.yyyy-hh:nn:ss")&"_log.txt.gz") Try hSMTPClient.Send If Error Then sMessage = "Fehler beim Senden der EMail!" sMessage &= gb.NewLine sMessage &= Format(Now, "dd.mm.yyyy hh:nn:ss") & " Uhr" File.Save(Application.Path &/ "Logs" &/ "error.log", sMessage) Endif End ' SendProtocol() Public Sub MkFile(sFilePath As String) Dim hFile As File If Not Exist(sFilePath) Then hFile = Open sFilePath For Create Close #hFile Endif End ' MkFile(...) Public Sub RMkDir(sDirectory As String) If Not sDirectory Or If Exist(sDirectory) Then Return RMkDir(File.Dir(sDirectory)) Mkdir sDirectory End ' RMkDir(...) Public Sub Form_Close() TimerSend.Stop If hSMTPClient Then hSMTPClient = Null End ' Form_Close() Public Sub btnClose_Click() FMain.Close End ' btnClose_Click()
Notes: * You have to adapt the value for the time interval to your needs. For a daily report, the value for the TimerSend.Delay property is 24 * 60 * 60 * 1000 ms.
Here you can see the contents of the error.log file in the event of an (initiated) error:
Fehler beim Senden der EMail! 30.01.2016 14:56:59 Uhr
Attention:The project does not use the original component gb.net.smtp, but a version corrected by the author and now error-free in the project folder. The component gb.net.smtp should therefore not be activated in the project settings! The error-free version is also available from the Gambas sources as of Gambas 3.8.3.
Project