User Tools

Site Tools


Sidebar

Network and communication

k24:k24.4:k24.4.3:start

24.4.3 SMTP Project 3

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,

  • to send the stored measurement results in an email via a clock at freely definable time intervals,
  • that the subject of each email contains a time stamp in readable format,
  • to include the measurement results with all relevant data in the text of the email (body),
  • to additionally package the measurement results in an archive and to send the archive file as an attachment, and
  • to save these archive files in a specified folder (log).

B1
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.

  • For creating the required log folder and files, the two procedures: RMkDir(sDirectory As String) and MkFile(sFilePath As String) are used.
  • The temperature and air pressure values are generated via random numbers and are to be replaced by the experimentally determined values for practical use!
  • The original log file is deleted after sending, as only the archive file is saved.
  • The compression rate for the compression method used is set to the maximum value (→ 9).

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.

Download

Project

Download

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/k24.4.3/start.txt · Last modified: 16.08.2022 (external edit)

Page Tools