KARPACH

WEB DEVELOPER BLOG

How to send email 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 handeling
}

web.config

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

Don’t forget to set IIS Default SMPT relay settings restrictions to grant access for 127.0.0.1.

Posted on March 17, 2008 by

Comments

Posted on 10/20/2010 02:09:01 AM by Rajesh chettri

Could you send me some more….

Posted on 6/30/2011 07:35:45 AM by aa

I love you for all your great posts