A very common and extremely useful functionality in Web Developement is sending emails from a Web Application. Some of the very common purposes of sending emails from a web application are, to send feedback to the webmaster of a site or informing a forum member of new post in a thread they subscribed - like Go4Expert, the list is never ending.
In the .NET Framework the task of sending an email is made very easy. In order to send an email message we need to use the SmtpMail class found in the System.Web.Mail namespace, which contains a static method Send.The simplest way to send an email message is to call the Send method passing an instance of the MailMessage class, the MailMessage class is also found in the System.Web.Mail namespace and it represents an email message.
The MailMessage class contains properties very similar to CDONTS (the frequently used component used in ASP to send mails).Such properties include: To, From, Cc, Bcc, BodyFormat, Subject, Priority, Body, etc. So, to send an email, we should create an instance of the MailMessage class and set its properties.
'Create an instance of the MailMessage class
Dim oMM as New MailMessage()
'Set the properties oMM.To = "firstname.lastname@example.org" oMM.From = "email@example.com" 'If you want to CC this email to someone oMM.Cc = "firstname.lastname@example.org" 'If you want to BCC this email to someone oMM.Bcc = "email@example.com" 'Send the email in text format oMM.BodyFormat = MailFormat.Text '(to send HTML format, you need to change MailFormat.Text to MailFormat.Html) 'Set the priority - options are High, Low, and Normal oMM.Priority = MailPriority.Normal 'Set the subject oMM.Subject = "Hello there!" 'Set the body - use VbCrLf to insert a carriage return oMM.Body = "Hi!" & vbCrLf & vbCrLf & "Sending an Email from ASP.NET"
Once we have created the MailMessage class instance with all the required properties, sending the email message is a piece of cake. We just need to call the static method Send in the SmtpMail class, passing the MailMessage class instance.
' Send the message, using the Send method of the SmtpMail class SmtpMail.Send(objMM)
SmtpMail.SmtpServer = emailServerNameorIP
SmtpMail.SmtpServer = ""