How to send email in ASP.NET?
Rating: 0 user(s) have rated this article
Posted by: admin, on 5/25/2009, in category "MS .NET"
Views: this article has been read 974 times
Location: Rawalpindi, Punjab, Pakistan
Abstract: This sample shows how to send e-mail in ASP.NET

 
string fromName = ConfigurationManager.AppSettings["defaultEmailFromName"];
string fromEmail = ConfigurationManager.AppSettings["defaultEmailFrom"];

System.Net.Mail.MailMessage email = new System.Net.Mail.MailMessage();
 
email.IsBodyHtml = true;
email.From = new System.Net.Mail.MailAddress(fromEmail, fromName);
email.To.Add(new System.Net.Mail.MailAddress("some@web.com", "Sender Name"));
email.Subject = "Email Subject";
using (StreamReader sr = File.OpenText("HTMLTemplates/Template.htm"))
{
  body = sr.ReadToEnd();
  sr.Close();
}

body = body.Replace("[Name]", "Customer Name");
email.Body = body;
try
{
   System.Net.Mail.SmtpClient emailProvider = new System.Net.Mail.SmtpClient();
   emailProvider.Send(email);
}
catch (Exception anException)
{
   // Exception handaling
}

<<<<<<<<<< web.config >>>>>>>>>>>
<system.net>
  <mailSettings>
    <smtp>
        <network host="localhost" port="25" defaultCredentials="true"/>
    </smtp>
  </mailSettings>
</system.net>

Remember to set IIS Default smpt relay settings restrictions to grant access for 127.0.0.1.


How would you rate this article?

User Feedback

Post your comment
Name:
E-mail:
Comment:
Insert Cancel
Select Theme

Theme:

Account Login
 
 
 
Shopping Cart

Your cart is currently empty.

Order History
Announcements

Website is currently under construction. Please check back soon!


Maintenance Schedule
Advertisements
Newsletter
Register to the site for free, and subscribe to the newsletter. Every month you will receive new articles and special content not available elsewhere on the site, right into your e-mail box!

Archived Newsletters
Poll of the week
Navigation Control
More Polls