Automatic Email

Started by magesh.p, Jul 26, 2008, 03:53 PM

Previous topic - Next topic

magesh.p

Automatic Email

I have noticed a few people here in the .Net forums trying to get their systems to automatically email someone.  I have written a class that does this very simply.  All you need to do is set the properties and call the function whenever you want your program to email someone.

To use this class add a new class file called Emailer.vb.  Open the file and overwrite it with the green code found here.

If you are using this function in a VB.NET standalone project then the dll will not be referenced yet.  To reference it do the following.

Go to Project -> Add Reference -> Find System.Web.dll -> Select and OK.

If you check in your references folder in the solution window you should now see an entry for System.Web

Imports System.Web
Public Class Emailer
    Private mTo As String
    Private mFrom As String
    Private mSubject As String
    Private mBodyFormat As Mail.MailFormat
    Private mSmtpServer As String

#Region " Class Properties "

    Public Property MailTo() As String
        Get
            Return mTo
        End Get
        Set(ByVal Value As String)
            mTo = Value
        End Set
    End Property

    Public Property From() As String
        Get
            Return mFrom
        End Get
        Set(ByVal Value As String)
            mFrom = Value
        End Set
    End Property

    Public Property Subject() As String
        Get
            Return mSubject
        End Get
        Set(ByVal Value As String)
            mSubject = Value
        End Set
    End Property

    Public Property BodyFormat() As Mail.MailFormat
        Get
            Return mBodyFormat
        End Get
        Set(ByVal Value As Mail.MailFormat)
            mBodyFormat = Value
        End Set
    End Property

    Public Property SmtpServer() As String
        Get
            Return mSmtpServer
        End Get
        Set(ByVal Value As String)
            mSmtpServer = Value
        End Set
    End Property

#End Region

#Region " Class Functions "

    Public Function EmailCust(ByVal Email As String, ByVal msgBody As String) As Boolean
        Dim myMessage As New Mail.MailMessage()
        Dim myMailServer As Mail.SmtpMail
        Dim output As Boolean = True

        myMessage.To = MailTo
        myMessage.From = From
        myMessage.Subject = Subject
        myMessage.BodyFormat = BodyFormat

        myMessage.Body = msgBody
        myMailServer.SmtpServer = SmtpServer
        Try
            myMailServer.Send(myMessage)
        Catch
            output = False
        Finally
            myMessage = Nothing
        End Try

        Return output
    End Function

#End Region

End Class
- An Proud Acumen -