Friday, May 29, 2009

Sending Email Using ASP.NET

Hi,
I posted this sample send email using ASP.NET
In the below the from,to, bcc, cc are the variables that you have to get values from the corresponding text box and assign it, to send mail.


string body = "Name:" + Name + "
" + "Phone Number:" + PhoneNumber +
/";

MailMessage mMailMessage = new MailMessage();
mMailMessage.From = new MailAddress(from);
mMailMessage.To.Add(new MailAddress(to));
mMailMessage.Body.Insert(1, ClientPhoneNumber);

if ((bcc != null) && (bcc != string.Empty))
{
mMailMessage.Bcc.Add(new MailAddress(bcc));
}
if ((cc != null) && (cc != string.Empty))
{
mMailMessage.CC.Add(new MailAddress(cc));
}
mMailMessage.Subject = subject;
mMailMessage.Body = body;
mMailMessage.IsBodyHtml = true;
mMailMessage.Priority = MailPriority.High;
SmtpClient mSmtpClient = new SmtpClient();
mSmtpClient.EnableSsl = false;
try
{
mSmtpClient.Send(mMailMessage);
lblException1.ForeColor = System.Drawing.Color.Green;
lblException1.Text = "";
lblException1.Text = "Your inquiry has been sent. Thank you!";
}
catch (Exception ex)
{
lblException1.Text = "";
lblException1.Text = Convert.ToString(ex.InnerException);
lblException1.ForeColor = System.Drawing.Color.Red;
lblException1.Text = ex.Message;
}


Enjoy !

No comments:

Post a Comment