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:

<system.net>
    <mailSettings>
        <smtp deliveryMethod="Network" from="name@domain.com">
            <network host="smtp.mail.com" 
                     userName="name@domain.com" 
                     password="blog.dotnetclr.com" port="25"/>
        </smtp>
    </mailSettings>
</system.net>
 
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.To.Add(to);
    mailMessage.Subject = subject;
    mailMessage.Body = body;

    var smtpClient = new SmtpClient();
    smtpClient.EnableSsl = true;
    smtpClient.Send(mailMessage);
}

No comments:

Post a Comment