Send messages to email

<< Click to Display Table of Contents >>

Navigation:  How to... > Scripting examples > Gateway scripting examples >

Send messages to email

Learn how to use a C# or Visual Basic .NET script with Diafaan SMS Server to send an email message. This simple script uses the System.Net.Mail namespace to send messages to the email address in the toAddress field of the message.



The only adaptation to the skeleton script is in the headers and the OnMessageToSend event:


C# script:


using System.Net.Mail;


private void OnMessageToSend(string recordId, string toAddress, string fromAddress, 

                             string message, string messageType)


   string smtpHostName = "";

   string defaultFromAddress = "";

   string defaultFromName = "Sender Name";

   string[] messageLines;

   SmtpClient smtpClient;

   MailAddress from;

   MailAddress to;

   MailMessage emailMessage;


   try {

      smtpClient = new SmtpClient(smtpHostName);

      if (fromAddress == "") {

        from = new MailAddress(defaultFromAddress, defaultFromName, 



      else {

        from = new MailAddress(fromAddress, fromAddress, 



      to = new MailAddress(toAddress);

      emailMessage = new MailMessage(from, to);

      messageLines = message.Split(new char[] {'\n','\r'}, 


      if (messageLines.Length > 0)   emailMessage.Subject = messageLines[0];

      emailMessage.SubjectEncoding = System.Text.Encoding.UTF8;

      emailMessage.Body = message;

      emailMessage.BodyEncoding =  System.Text.Encoding.UTF8;


      PostSendResult(recordId, "", StatusCode.Sent, 

                     "Success: Message accepted by the SMTP server", 

                     "", "", false);


   catch (Exception e) {

     PostSendResult(recordId, "", StatusCode.SendError, "Error: " + e.Message, 

                "", "", false);




Visual Basic .NET script:


Imports Microsoft.VisualBasic

Imports System.Net.Mail


Private Sub OnMessageToSend(recordId As String, toAddress As String, _

                            fromAddress As String, _

                            message As String, messageType As String)

   Dim smtpHostName As String = ""

   Dim defaultFromAddress As String = ""

   Dim defaultFromName As String = "Sender Name"

   Dim messageLines As String()

   Dim smtpClient As SmtpClient

   Dim from As MailAddress

   Dim [toAs MailAddress

   Dim emailMessage As MailMessage



      smtpClient = New SmtpClient(smtpHostName)

      If fromAddress = "" Then

         from = New MailAddress(defaultFromAddress, defaultFromName, _



         from = New MailAddress(fromAddress, fromAddress, System.Text.Encoding.UTF8)

      End If

      [to] = New MailAddress(toAddress)

      emailMessage = New MailMessage(from, [to])

      messageLines = message.Split(New Char() {ControlChars.Lf, ControlChars.Cr}, _


      If messageLines.Length > 0 Then

         emailMessage.Subject = messageLines(0)

      End If

      emailMessage.SubjectEncoding = System.Text.Encoding.UTF8

      emailMessage.Body = message

      emailMessage.BodyEncoding = System.Text.Encoding.UTF8


      PostSendResult(recordId, "", StatusCode.Sent, _

                "Success: Message accepted by the SMTP server", _

                "", "", False)

   Catch e As Exception

      PostSendResult(recordId, "", StatusCode.SendError, "Error: " & e.Message, _

                "", "", False)

   End Try

End Sub


If you make any change to the script make sure that the PostSendResult is always called, even if an exception is caused. Otherwise the script will only continue with the next message after a timeout of several minutes.