Email, SMS (Serial Port)



       Email – Electronic mail is soft messages (plain text created using mailware client with graphics, documents, and any kinds of files … as attachment) send from one to one or more recipient through internet or intranet protocols. SMTP (Simple mail transfer protocol) commonly use to send the mail messges, POP3 (Post office protocol or IMAP (internet Message access Protocol) use to read the messages.

Microsoft .Net latest version comes with System.Net.Mail namespace (.Net 1.1 came with System.Web.Mail. But it was deprecated and System.Net.Mail namespace introduced in .Net version 2.0). It has MailMessage class with various properties such as from, to, cc (carbon copy), bcc (blind carbon copy). Subject, subject encoding, isBodyHTML, Body and body encoding
So mail message created using ‘Message’ class need to send to SMTP server. ‘SMTPClient’ class helps to send the message to designated SMTP server with port assigned.

Sample Code:

Imports System.Net
Imports System.Net.Mail
Imports System.Net.Security
Public shared Sub Main()
End SubPrivate Sub testMail()
Dim fromMail As New MailAddress(“”)
Dim toMail(2) As MailAddress
toMail(0) = New MailAddress(“”)
toMail(1) = New MailAddress(“”)
toMail(2) = New MailAddress(“”)
If Not sendMail(“testServer”, “25”, “test Message”, _
“Hi, < br / >
This is Test Message from testServer.
< br />
< strong > By
< br />
Web Admin. </ strong >”, _
fromMail, toMail) Then
Console.WriteLine(“Send mail failed.”)
End If
End SubPrivate Function sendMail(ByVal strServer As String, ByVal iport As Integer, _
ByVal strSubject As String, ByVal strMessageBody As String, _
ByVal fromMail As MailAddress, ByVal toMail() As MailAddress, _
Optional ByVal cc As MailAddress = Nothing, _
Optional ByVal bcc As MailAddress = Nothing) As Boolean
Dim message As New MailMessage()
With message
For i = 0 To (toMail.GetLength(0) – 1)
.From = fromMail
If Not cc Is Nothing Then .CC.Add(cc)
If Not bcc Is Nothing Then .CC.Add(bcc)
.Subject() = strSubject
.IsBodyHtml = True
.Body = strMessageBody
End With
Dim smtpClient As New SmtpClient(strServer, iport)
smtpClient.Credentials = CredentialCache.DefaultNetworkCredentials
Return True
Catch ex As Exception
Return False
End Try
End Function

Serial Port (or) Com Port

Still comport devices (scanners, printers, imaging devices, modem and so on) are available in the market. But new devices adapted to universal serial bus communication. These USB communications identified as virtual communication port with the help of device driver provided by the vendor with operating system. Blue tooth communication also identified as virtual com port depends the driver and feature (profile) provided with it (Some thermal printers are communicating with handheld using blue tooth wireless communication). Some scenario as a developer we have to integrate those devices and peripherals when business adopt or go with new technologies. As a .Net developer we can achieve com port communication using System.IO.Ports namespace.

Comport Settings:
Before use the comport we have to assign Baud rate, parity, Databit and stopbit value to it.

baud rate – symbols per second or pulses per second. It is the unit of symbol rate, also known as baud rate
parity = none (N), odd (O), even (E), mark (M), or space (S
databit = 5 to 8 ; 8 is mostly used with new applications
stopbit = End character to detect by the hardware

Sample Code :
public shared sub Main()

If Not sendData(“Test Message”, 9600) Then
Console.WriteLine(“Write data to port failed.”)
Console.WriteLine(“Write data to the port completed.”)
End If

End Sub

Private Function sendData(ByVal data As String, _
Optional ByVal baudrate As Long = 2400, _
Optional ByVal Stopbit As Integer = 1, _
Optional ByVal databit As Integer = 8, _
Optional ByVal parity As Integer = 3) As Boolean

Dim comport As New Ports.SerialPort()
With comport
Dim portName As String
Console.WriteLine(“Available ports are “)
For Each portName In Ports.SerialPort.GetPortNames()
Next portName
Console.Write(“Type the port name to connect”)
.PortName = Console.ReadLine()
.BaudRate = baudrate
.DataBits = databit
.StopBits = Stopbit
.Parity = .Parity

If Not .IsOpen Then .Open()


If .IsOpen Then .Close()
End With
Return True
Catch ex As Exception
Console.WriteLine(String.Format(“Error Occured : {0} “, ex.ToString()))
Return False
End Try

End Function

SMS (Short Message Service)

If you want to send SMS using GPRS/GPS modem using serial port, we have to send the ‘AT’ (attention) commands with relevant parameters, then need to send the actual message (or write actual message). Some of the AT Command and its meaning. This is generic command, please check the modem manual and/or vendor support / technical site for more information.

AT command


+CMGF Set text (+CMGF=1)  or PDU (+CMGF=0) mode
+CMGS Send message
+CMSS Send message from storage
+CMGW Write message to memory
+CMGD Delete message
+CMGC Send command
+CMMS More messages to send

Sample Code: (Command may varies depends modem specification)

private string sendSMS(string RecipientHpNo, string messages)
this.serialPort.WriteLine(“AT” + (char)(13));
this.serialPort.WriteLine(“AT+CMGF=1” + (char)(13)); //Check your modem support text mode or not. If not supporting, this AT command return error
this.serialPort.WriteLine(“AT+CMGW=\”” + RecipientHpNo + “\””);
this.serialPort.WriteLine(“>” + messages + (char)(26));
this.serialPort.WriteLine(“AT+CMGS” + (char)(13));


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s