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);  
}  

About Yasser Shaikh

Hello, I am a Web Dev, employed with Media.Net where I primarily work on ASP.NET MVC and Angular JS based developement.

Questions? Enquiries? Comments? Or even just want to say “Hi” to me, you are welcome to all of that. Just drop me a message at “mail@yassershaikh.com” and I’ll get back to you as soon as possible.

Cheers !

4 comments

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>