Introduction
We are using System.Web.Mail.SmtpMail
to send email in dotnet 1.1 which is obsolete in 2.0. The System.Net.Mail.SmtpClient
Class will provide us the same feature as that of its predecessor.
This article explains how to use System.Net.Mail
namespace to send emails.
Using the code
The HTML Design contains provision to enter sender’s name, email id and his comments. On click of the send email button the details will be sent to the specified email (Admin).
The Send mail functionality is similar to Dotnet 1.1 except for few changes
System.Net.Mail.SmtpClient
is used instead of System.Web.Mail.SmtpMail
(obsolete in Dotnet 2.0).
System.Net.MailMessage
Class is used instead of System.Web.Mail.MailMessage
(obsolete in Dotnet 2.0)
- The
System.Net.MailMessage
class collects From address as MailAddress object.
- The
System.Net.MailMessage
class collects To, CC, Bcc addresses as MailAddressCollection.
- MailMessage Body Format is replaced by IsBodyHtml
The Code is Self explanatory by itself.
protected void btnSendmail_Click(object sender, EventArgs e)
{
SmtpClient smtpClient = new SmtpClient();
MailMessage message = new MailMessage();
try
{
MailAddress fromAddress = new MailAddress(txtEmail.Text, txtName.Text);
smtpClient.Host = "localhost";
smtpClient.Port = 25;
message.From = fromAddress;
message.To.Add("admin1@yoursite.com");
message.Subject = "Feedback";
message.CC.Add("admin1@yoursite.com");
message.CC.Add("admin2@yoursite.com");
message.Bcc.Add(new MailAddress("admin3@yoursite.com"));
message.Bcc.Add(new MailAddress("admin4@yoursite.com"));
message.IsBodyHtml = false;
message.Body = txtMessage.Text;
smtpClient.Send(message);
lblStatus.Text = "Email successfully sent.";
}
catch (Exception ex)
{
lblStatus.Text = "Send Email Failed." + ex.Message;
}
}
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.