Generating an email in VB.NET

Posted on December 9th, 2008 by Arthur Kay

So I’ve been fooling around lately with .NET, and I have decided that I’m going to redesign my website (or at least create a sub-domain) in .NET as an exercise in developing my skills. I’ll be using Visual Web Developer… mainly because I don’t want to pay for Visual Studio :-)

Since one of the more important parts of the site is the “Contact Me” form, I decided that starting on that part would really be a good example for learning VB.NET.

My current website uses Classic ASP (with VB Script) to generate an email… so how hard could using VB.NET to send an SMTP email really be?

…apparently really hard, particularly since I’m new.

After searching Google for a bit, I stumbled across this blog by Mike Pope. This turned out to be my best friend, although his code didn’t exactly work out-of-the-box. With a few modifications, I had this working in about an hour — not exactly bad for my first VB.NET page!

Here’s what I came up with. I pass the page a handful of variables via a POST operation.

Partial Class contactpage
    Inherits System.Web.UI.Page

    Protected Sub contactpage_load() Handles Me.Load

        Dim email As New System.Net.Mail.MailAddress(Request("Email"))

        Dim Fname As New String(Request("Firstname"))
        Dim Lname As New String(Request("Lastname"))
        Dim phone As New String(Request("Phone"))
        Dim comments As New String(Request("Comments"))

        Response.Expires = -1
        Response.ContentType = "text/plain"

        ' ======================
        ' Send Email to visitor
        '=======================
        Dim visitorSubj As String = "Thank you for contacting aKa Web Design"

        visitorBody += "Thank you for contacting aKa Web Design!"

        Dim noReplyAddress As New System.Net.Mail.MailAddress("AN EMAIL ADDRESS HERE")

        SendMail(noReplyAddress, _
                 email, _
                 visitorSubj, _
                 visitorBody)

        ' **********************
        ' send message to admin
        ' **********************
        Dim sendMessage As String = "Some message here"

        Dim akaWebAddress As New System.Net.Mail.MailAddress("AN EMAIL ADDRESS HERE")

        SendMail(email, _
                 akaWebAddress, _
                 "Contact aKa Web Design", _
                 sendMessage)

    End Sub

    Protected Sub SendMail(ByVal strFrom As System.Net.Mail.MailAddress, _
                               ByVal strTo As System.Net.Mail.MailAddress, _
                               ByVal strSubj As String, _
                               ByVal strMsg As String)

        Dim mailMessage As New System.Net.Mail.MailMessage(strFrom, strTo)
        mailMessage.Subject = strSubj
        mailMessage.Body = strMsg
        mailMessage.IsBodyHtml = True

        Dim mailSender As New System.Net.Mail.SmtpClient()

        mailSender.Host = "YOUR OUTGOING SMTP SERVER HERE"

        mailSender.Port = 25 'YOUR PORT MIGHT BE DIFFERENT

        mailSender.Credentials = New Net.NetworkCredential("YOUR USERNAME", "YOUR PASSWORD")

        Try
            mailSender.Send(mailMessage)
            Response.Write("Message sent!")
        Catch ex As Exception
            Response.Write(ex.Message)
        End Try

    End Sub

End Class

I’m happy I finally got it working! My biggest problem was trying to figure out my outgoing SMTP port (I used a shared-hosting account).

I hope this helps someone! Stay tuned for the redesign, coming soon!

Share and Enjoy:
  • RSS
  • Facebook
  • StumbleUpon
  • Digg
  • Sphinn
  • del.icio.us
  • Technorati
  • Reddit
  • LinkedIn
  • Twitter
  • Yahoo! Buzz

Leave a Reply