| Author: Vidhya 25 Jul 2008 | Member Level: Gold | Rating: Points: 1 |
To send an e-mail with a simple text message, you have to use the Send() method of SMTP class. The syntax of the Send() method is shown in Listing 1.1 and an example is shown in Listing 1.2:
Listing 1.1
[Visual C# .NET]
SmtpMail.Send("FROM","TO","SUBJECT","MESSAGE BODY");
[Visual Basic .NET]
SmtpMail.Send("FROM","TO","SUBJECT","MESSAGE BODY")
Listing 1.2
[Visual C# .NET]
SmtpMail.Send("info@mydomain.com","hello@hello.com","Thank You", "We look forward to working with you again in the future");
[Visual Basic .NET]
SmtpMail.Send("info@mydomain.com","hello@hello.com","Thank You", _ "We look forward to working with you again in the _ future")
You can place the above code either on the form's Load() event or in a button control.
|
| Author: Vidhya 25 Jul 2008 | Member Level: Gold | Rating: Points: 1 |
Instead of supplying all parameters in the Send() method, you can define properties and their corresponding values separately by creating an instance of the MailMessage class. With the help of this class, you can easily add attachments, set priorities, BCC, CC values, and much more. Table 1, given at the end of this article, shows a list of properties of the MailMessage class. Add the code given in Listing 1.3 by double-clicking the button captioned Submit:
Listing 1.3
[Visual C# .NET]
MailMessage objEmail = new MailMessage(); objEmail.To = txtTo.Text; objEmail.From = txtFrom.Text; objEmail.Cc = txtCc.Text; objEmail.Subject = "Test Email"; objEmail.Body = txtName.Text + ", " + txtComments.Text; objEmail.Priority = MailPriority.High; //SmtpMail.SmtpServer = "localhost"; try{ SmtpMail.Send(objEmail); Response.Write("Your Email has been sent sucessfully - Thank You"); } catch (Exception exc){ Response.Write("Send failure: " + exc.ToString()); }
[Visual Basic .NET]
Dim objEmail as New MailMessage() objEmail.To = txtTo.Text objEmail.From = txtFrom.Text objEmail.Cc = txtCc.Text objEmail.Subject = "Test Email" objEmail.Body = txtName.Text & ", " &txtComments.Text objEmail.Priority = MailPriority.High 'SmtpMail.SmtpServer = "localhost" try SmtpMail.Send(EMail) Response.Write(Your E-mail has been sent sucessfully - _ Thank You)
catch exc as Exception Response.Write("Send failure: " + exc.ToString()) End Try
Note: If you are using your local system (Server = localhost) instead of a real live server, you should properly enable relying on the Internet Information Server (IIS). When you execute the above code, the server not only displays a confirmation message but also sends an e-mail to the address mentioned in the To and Cc text boxes with the information you entered in the respective fields. It is not necessary for you to enter a Cc address, but it is shown here as part of the explanation. Further, the e-mail will be sent with the highest priority.
Table 1: MailMessage class properties
Property Description Attachments Used for sending e-mails with attachments From Sender's e-mail address To Recipient's e-mail address Cc Recipient's e-mail address (Carbon Copy) Bcc Recipient's e-mail address (Blind Carbon Copy) Body Text of the e-mail message BodyFormat Specifies the format of an e-mail message (Possible Values: Text, Html) Priority Specifies the priority of an e-mail message (Possible Values: High, Low, and Normal) Subject Denotes the subject of an e-mail message Headers Denotes a collection of acceptable headers (Example: Reply-To) BodyEncoding Specifies the method of encoding an e-mail message (Possible Values: Base64 and UUEncode)
Sending HTML E-Mail Messages If you would like to send the above e-mail in HTML Format, simply add the code given below to the above listing:
[Visual C# .NET]
objEmail.BodyFormat = MailFormat.Html;
[Visual Basic .NET]
objEmail.BodyFormat = MailFormat.Html
|
| Author: Vidhya 25 Jul 2008 | Member Level: Gold | Rating: Points: 1 |
hi,
for more details,
refer following link..
http://www.developer.com/net/asp/article.php/3096831
happy programming...........
|