Send Email using Gmail SMTP

In one of my Application called Exxecutive (secret !) I wanted to send email to my clients using my Gmail Account. I wanted to make sure Gmail keeps a copy of my sent emails. If I use SMTP provided by Microsoft IIS, Gmail doesn’t come in the picture. After doing some research I wrote a function that will use Gmail’s SMTP server and send email and also keep a copy in the send mail folder. This function uses .NET’s Mail Namespace.

It is important to note here that Gmail uses port 587 (a new preferred port for mail submission) and SSL

Here is the VB.net code:

    Function SendEmail(ByVal inTo As String, ByVal inSubject As String, ByVal inBody As String)
        Try
            Dim MySmtpClient As New System.Net.Mail.SmtpClient()
            Dim MyCredentials As New System.Net.NetworkCredential
 
            MyCredentials.UserName = "name@gmail.com"
            MyCredentials.Password = "***your password here****"
 
            MySmtpClient.Host = "smtp.gmail.com"
            MySmtpClient.Port = 587 ' 25
            MySmtpClient.Credentials = MyCredentials
            MySmtpClient.EnableSsl = True
            Dim ToMailAddress As New MailAddress(inTo)
            Dim FromMailAddress As New MailAddress(MyEmailAddress, MyEmailDisplayName)
            Dim MyMailMessage As New System.Net.Mail.MailMessage(FromMailAddress, ToMailAddress)
 
            MyMailMessage.Subject = inSubject
            MyMailMessage.Body = inBody
            MyMailMessage.IsBodyHtml = True
            MySmtpClient.Send(MyMailMessage)
            Return True
        Catch ex As Exception
            MsgBox(ex.ToString)
            Return False
        End Try
    End Function
 

A few years ago while working on a web-scraping tool in .NET I found an amazing library SgmlReader which made my life so easy to convert HTML documents to XHTML. With this I was able to run XPATH queries to extract whatever information I want from any dam website written in worst possible malformed HTML. Had it not been SgmlReader I would have had to write tedious parsing  code to extract the tokens from the HTML string.

With this simple code you cleanup the mess that most web-masters do !!

Here is the function for VB.NET. Please download SgmlReader from the link above.

    Public Function Html2Xml(ByVal txtHTMLString As String) As String
        Dim XHTML As New Sgml.SgmlReader
        Dim sw As StringWriter = New StringWriter()
        Dim w As XmlTextWriter = New XmlTextWriter(sw)
 
        XHTML.DocType = "HTML"
        XHTML.InputStream = New StringReader(txtHTMLString)
 
        While (Not XHTML.EOF)
            w.WriteNode(XHTML, True)
        End While
        w.Close()
 
        Return sw.ToString()
 
    End Function

 

Recently I encountered a similar need in PHP and I was desperately searching for SgmlReader equivalent and my search zeroed on php_tidy extension. Once you enable this extension you get all the functionality.

	$opts = array("clean" => true, "output-xml" => true);
	$xhtml = tidy_parse_file("http://www.example.com", $opts);
	echo $xhtml;

 

For more information about php_tidy goto http://us.php.net/tidy