how to send Email in ASP.NET using SMTP sever

Here, I will provide an easy way to send mail from ASP.NET pages. Suppose, you have developed one registration form in your site for adding new user and after completion of whole procedure you want to inform your client or you want a notification of the new subscription then here are many ways which you can use to inform the client, one way is to send him/her message at his/her email address. But if you manually do this task then it becomes time consuming and even there are chances of a client missing out. So to overcome this problem you have to go through another way and the way is by setting the code which will automatically send the mail to the client whoever registers himself in your site as well as you will receive a notification. The most beneficial side of this way is that you will never forget to send email to any single client and another benefit is that all the work will be done automatically that is how you can minimize work load and there are few chances for error to come.

Code for sending email in ASP.NET using SMTP service is given below but before using this code check that SMTP services is on in your server. 

first line of code would be used to import statement which is used to import required namespace ‘for this code to work
Imports System.Web.Mail
‘If you will never import this namespace then code will never work and it will gives you an error.
‘Then declare variable of class MailMessge
Dim mailmessage As MailMessage
‘Create an instance of a MailMessage class
mailmessage = New MailMessage
‘Specify sender of the message using property of MailMessage class named ‘From’. 
mailmessage.From = "email address of massage sender" 
‘Specify where you want to send message using property of MailMessage class named ‘To’. 
mailmessage.To = “email address where you want to send message” 
‘Specify the subject of message using property called ‘Subject’. 
mailmessage.Subject = "message subject will come here"
‘If you want html content in your mail then define the body format as HTML. 
mailmessage.BodyFormat = MailFormat.Html 
‘Declare one string variable for combining the whole message. 
Dim body as string 
‘Create the message and format in which you want to send.
body = "message" + "<br>message<br>"
body += "message! <br><br>
body += "message!"

body += "message!" 
‘Specify the body property of MailMessage class which keeps your message. 
mailmessage.Body = body 
‘Specify the SMTP mail server.
Make sure that
SMTP relaying is enabled on your server.
SmtpMail.SmtpServer = "" 
‘Put the given line of code in try…catch to reduce the chances error.
‘User the Send property of SmtpMail class to send the mail.
       Catch ex As Exception
‘Write the message you want to generate if error occurs when you run this code.
End Try

So, you are done!

(Posted By Aden)

Web development tips, marketing strategies and AccuWeb Hosting news sent to your inbox.

Live Chat with our Executive