Purpose
Send an email with one or more attachments via an SMTP server
|
Solution
The .NET library offers several methods to send an email. In this example we will use the System.Net.Mail namespace to create a method that will be called via GuiXT.
|
Here is the GuiXT script to send the email:
Set V[attachment1] "C:\temp\file1.pdf"
copytext fromString="attachment1" toText="guixtattachments"
Set text[guixtmailbody] "<b> Test <br> email <br>withHTML body </b> <br> <a href='www.synactive.com'> synactive.com </a>"
callVB r = utilities.email.send_email _
if V[r]
message "email was sent" -statusline
endif
Remarks:
Several GuiXT variables are used, especially for the body text as this text may exceed the maximal number of characters of a normal callVB parameterThe library needs to know whether to send plain text or HTML text in the body You may add a method to decrypt the password and give the encrypted password as a parameter for the callVB function |
Here is the VB.NET coding: Imports System.Net.Mail Imports guinet Public Class email Dim myguixt As New guinet.guixt Public Function send_email( mailto As String, mailfrom As String, mailcc As String, mailbcc As String, mailtitle As String, mailbody As String, mailbodyishtml As String, attachments As String, smtpserver As String, smtpuser As String, smtppassword As String, smtpport As String, authentification As String) As String Try Dim MySmtpClient As New SmtpClient() With MySmtpClient .UseDefaultCredentials = False If authentification = "X" Then .EnableSsl = True Else .EnableSsl = False End If .Credentials = New Net.NetworkCredential _ (smtpuser, smtppassword) .Port = smtpport .Host = smtpserver .Timeout = 10000 .DeliveryMethod = SmtpDeliveryMethod.Network End With Dim mail As New MailMessage() With mail .From = New MailAddress(mailfrom) For Each mt As String In mailto.Split(",") If Not mt.Trim = "" Then .To.Add(mt) End If Next For Each cc As String In mailcc.Split(",") If Not cc.Trim = "" Then .CC.Add(cc) End If Next For Each bcc As String In mailbcc.Split(",") If Not bcc.Trim = "" Then .Bcc.Add(bcc) End If Next 'Attachment(s) For Each att As String In myguixt.GetText("guixtattachments").Split(vbNewLine) If Not att.Trim = "" Then Dim path As String = att.Trim Dim attachment As System.Net.Mail.Attachment = New System.Net.Mail.Attachment(path) .Attachments.Add(attachment) End If Next .Subject = mailtitle .Body = myguixt.GetText(mailbody) If mailbodyishtml = "X" Then .IsBodyHtml = True Else .IsBodyHtml = False End If End With MySmtpClient.Send(mail) Catch ex As Exception Return ex.ToString End Try Return "" End Function |
Components
InputAssistant + Controls
|