Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Automatic Email

New Language Features

Automatic Email

by  Zarcom  Posted    (Edited  )
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


[color green]
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
[/color]

Hope you like it!
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top