Friday, 2 November 2012

MVC and Sending email

I have often needed email class that works with my website.

Here I am adding small memory nudge :)
How do you setup your webconfig?
how does website use the SmtpClient ?

Here is your configuration of web.config:

        <smtp deliveryMethod="Network" from="">
            <network host="" 
                     password="" port="25"/>
Note: this tag is straight in configuration tag of your web.config

And here is the sample C# code that will use the above configuration settings
public void SendBy(string to, string subject, string body)
    var mailMessage = new System.Net.Mail.MailMessage();
    mailMessage.Subject = subject;
    mailMessage.Body = body;

    var smtpClient = new SmtpClient();
    smtpClient.EnableSsl = true;