Sending Email
The namespace that needs to be imported to send an email is the System.Web.Mail namespace. We use the SmtpMail and MailMessage classes of this namespace for this purpose. The MailMessage class provides properties and methods for constructing an email message. To start, open a Web Forms page, drag a Button control on to the form, switch to code view and paste the following code.
Imports System.Web.Mail 'namespace to be imported Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As_ System.EventArgs) Handles Button1.Click Dim mailMessage As New MailMessage() 'creating an instance of the MailMessage class mailMessage.From = "xyz@mydomain.com" 'senders email address mailMessage.To = "abc@sendersemail.com" 'recipient's email address mailMessage.Cc = "carboncopy@sendersemail.com" 'email address of the Cc recipient mailMessage.Bcc = "blindcarboncopy@sendersemail.com" 'email address of the Bcc recipient mailMessage.Subject = "Hello" 'subject of the email message mailMessage.BodyFormat = MailFormat.Text 'message text format. Can be text or html mailMessage.Body = "This tutorial is sending email with an ASP.NET app." 'message body mailMessage.Priority = MailPriority.Normal 'email priority. Can be low, normal or high SmtpMail.SmtpServer = "mail.yourserver.com" 'mail server used to send this email. modify this line based on your mail server SmtpMail.Send(mailMessage) 'using the static method "Send" of the SmtpMail class to send the mail Response.Write("Mail sent") 'message stating the mail is sent End Sub |
To start, drag seven labels, six textboxes and two buttons on to the Web forms page designer. The user interface for this sample can be found at the bottom of this page. The modified code looks as follows:
Imports System.Web.Mail
Private Sub SendMail_Click(ByVal sender As System.Object, ByVal e As_
System.EventArgs) Handles SendMail.Click
Dim mailMessage As New MailMessage()
mailMessage.From = TextBox1.Text
mailMessage.To = "admin@startvbdotnet.com"
'you also can set this to TextBox2.Text
mailMessage.Cc = TextBox3.Text
mailMessage.Bcc = TextBox4.Text
mailMessage.Subject = TextBox5.Text
mailMessage.BodyFormat = MailFormat.Text
mailMessage.Body = TextBox6.Text
'textbox6 TextMode property is set to MultiLine
mailMessage.Priority = MailPriority.Normal
SmtpMail.SmtpServer = "mail.yourserver.com"
'mail server used to send this email. modify this line based on your mail server
SmtpMail.Send(mailMessage)
Label6.Text = "Your mail was sent"
'message stating the mail is sent
End Sub
Private Sub Reset_Click(ByVal sender As System.Object, ByVal e As_
System.EventArgs) Handles Reset.Click
TextBox1.Text = " "
TextBox2.Text = " "
TextBox3.Text = " "
TextBox4.Text = " "
TextBox5.Text = " "
TextBox6.Text = " "
'resetting all the value to default i.e null
End Sub
No comments:
Post a Comment