14-11-2004, 11:27
|
|
|
|
חבר מתאריך: 20.06.03
הודעות: 5,616
|
|
להלן המאמר בנושא:
Sending a Simple E-Mail Message
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.
Sending E-Mail Messages Using WebForm Controls
The main problem with the code given in Listing 1.2 is that you have to change the parameter values each time for sending different mail messages. To solve this problem, you should build a User Interface using the required WebForm controls, as shown in Figure 1.
Figure 1: The User Interface
As you can see, the above User Interface is made up of two text boxes, one multiline text box, and a button. I have also applied ASP.NET validation controls to the above GUI to avoid errors.
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.
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
|