How to send email in ASP.NET C# MVC 3 (with sample working code)

Below is a sample working code for sending in a mail using C#, in the below example I am using google’s smtp server. The code is pretty self explanatory, replace email and password with your email and password values,

In case of any queries feel free to write in using the comment box. CHEERS !

public void SendEmail(string address, string subject, string message)  
{  
    string email = "yrshaikh.mail@gmail.com";  
    string password = "put-your-GMAIL-password-here";

    var loginInfo = new NetworkCredential(email, password);  
    var msg = new MailMessage();  
    var smtpClient = new SmtpClient("smtp.gmail.com", 587);

    msg.From = new MailAddress(email);  
    msg.To.Add(new MailAddress(address));  
    msg.Subject = subject;  
    msg.Body = message;  
    msg.IsBodyHtml = true;

    smtpClient.EnableSsl = true;  
    smtpClient.UseDefaultCredentials = false;  
    smtpClient.Credentials = loginInfo;  
    smtpClient.Send(msg);  
}  

3 comments on “How to send email in ASP.NET C# MVC 3 (with sample working code)

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>