Mail sturen vanuit Gambas

MailFromGambas Kies component “gb.net.smtp” in Project/Properties/Components.
Je krijgt dan beschikking over een smtp opject met volgende eigenschappen en methodes:

gb.net.smtp
 SmtpClient
   Add
   Alternative
   Bcc
   Cc
   From
   Host
   Port
   Send
   Subject
   To

De meeste eigenschappen kan je gewoon een tekstwaarde toewijzen, bv .Host = "..."
De lijst van de bestemmelingen wordt gegeven door de string .To.
Om een bestemmeling toe te voegen moet je To.Add("..") gebruiken, bv in een loop per adres dat je wil toevoegen.
Voor de inhoud van de mail .Add("..")
Attachments zijn halvelings voorzien maar lijken in 2.x versies (<2.21) niet altijd te werken (alleen te werken voor pure tekst). Attachments hebben een type: IANN
Zie ook de documentatie van de gb.net.smtp module.

Een voorbeeld van code (die bij het schermvoorbeeld hoort):

' Gambas class file
'
PRIVATE mailer AS SmtpClient
'
PUBLIC SUB _new()
'
  mailer = NEW SmtpClient
'
END
'
PUBLIC SUB Form_Open()
'
  loadSettings()
'
END
'
PUBLIC SUB btQuit_Click()
'
  ME.Close
'
END
'
PUBLIC SUB btSave_Click()
'
  doSave()
'
END
'
PUBLIC SUB doSave()
'  
  Settings["mail/server"] = txbxMailServer.Text
  Settings["mail/serverport"] = txbxMailServerPort.Text
  IF (txbxMailFrom.Text)
    Settings["mail/from"] = txbxMailFrom.Text
  ENDIF 
  Settings["mail/remark"] = txbxMailRemark.Text
  Settings.Save()
'  
END
'
PUBLIC SUB loadSettings()
'  
  txbxMailServer.Text = Settings["mail/server", "localhost"]
  txbxMailServerPort.Text = Settings["mail/serverport", "25"]
  txbxMailFrom.Text = Settings["mail/from", System.User.Name & "@" & System.Host & System.Domain] 
  txbxMailRemark.Text = Settings["mail/remark", "Default setting"]
END
'
PUBLIC SUB canSend()
'  
  btSend.Enabled = (txbxMailFrom.Text) AND (txbxMailServer.Text) AND (txbxMailTo.Text) AND (txbxMailServerPort.Text) AND (txbxMailSubject.Text)
'  
END
'
PUBLIC SUB txbxMailFrom_Change()
'
  canSend()
'
END
'
PUBLIC SUB txbxMailSubject_Change()
'
  canSend()
' 
END
'
PUBLIC SUB txbxMailTo_Change()
'
  canSend()
'
END
'
PUBLIC SUB btSend_Click()
'
  doMakeMail()
  doSendMail()
'
END
'
PUBLIC SUB doMakeMail()
'  
  WITH mailer
    .Host = txbxMailServer.Text
    .Port = txbxMailServerPort.Text
    .From = txbxMailFrom.Text
    .To.Add(txbxMailTo.Text)
    .Subject = txbxMailSubject.Text
    .Add(txaeMailBody.Text)
    .Alternative = FALSE
  END WITH 
'  
END
'
PUBLIC SUB doAttach()
'  
  mailer.Add(FileChooser1.Value, "pdf")
'  
END
'
PUBLIC SUB doSendMail()
'  
    mailer.Send()
    btSend.Enabled = FALSE
'    
END
'
PUBLIC SUB btClear_Click()
'
  txbxMailServer.Text = ""
  txbxMailServerPort.Text = ""
  txbxMailRemark.Text = ""
'  
  txbxMailFrom.Text = ""
  txbxMailTo.Text = ""
  txbxMailSubject.Text = ""
  txaeMailBody.Clear()
'  
END
This entry was posted in Gamblog, Hoe - in Gambas, Uncategorized. Bookmark the permalink.