How to send an email in vb.net

To send an email in vb.net, you will need to create a function, and pass parameters to it from the code behind, in order for the email class to work you will need to add a namespace by importing the "System.Web.Mail", this class contains all the methods that will enable you to send the email. So, first let's go ahead and create a vb class, and let's call it "mail", next step we are going to import the namespace mail by using the following code prior to the class declaration: "Imports System.Web.Mail". Afterthat, your class declaration: "Public Class Email" and last but not least, your email function. below is the code for this function:

Private Shared ReadOnly SmtpServer As String = ConfigurationSettings.AppSettings("SmtpServer")

'This fucntion will send the email according to the strings passsed
Public Shared Sub SendEmail(ByVal Sendto As String, ByVal From As String, ByVal Subject As String, ByVal Body As String)

'Try
Dim Message As MailMessage = New MailMessage< /FONT >
'Set properties of the email to be sent

With Message .To = Sendto
 .From = From
.Subject = Subject
 .Body = Body
 .BodyFormat = MailFormat.Html
 End With

'Select SmtpSever set in webconfig and send email
SmtpMail.SmtpServer = SmtpServer
 SmtpMail.Send(Message)
Catch ex As System.Web.HttpException
'Throw ex
'end try


now, when calling the email function you will need to create an instance of the class mail in the code behind; to do that write the following code:

Dim SendMessage As New Email()

and the last step is to call your function thru the instance of the mail object you created. code goes like this:

SendMessage.SendEmail(email, subjectt, message, subject)

notice that you will need to pass your parameter to the funtion. that's it, using this code users will be able to send you email from your webpage.






Home | FeedBack | Contact Us | Sitemap | Link Exchange
Copyright © 2007 Bztalkonline. All rights reserved.