Sending emails with attachment in MVC 4

Here I am sharing how you can send email with attachment using C# MVC 4. Before you copy and paste the code you need to consider below few points:

1) Enable gmail account to allow Less Secure App.

2) You need to add below two lines to make below code work.

using System.Net;
using System.Net.Mail; 

3) Gmail requires you to use a secure connection for sending emails else it throws error like below: "The server response was: 5.7.0 Must issue a STARTTLS command first. i16sm1806350pag.18 - gsmtp" To fix/overcome this issue make sure you enable SSL in web.config file as below:

<network host="" enableSsl="true" ... />

Or just add a param while sending email as shown in code.

public void email()
    MailMessage mail = new MailMessage();
    SmtpClient SmtpServer = new SmtpClient("");
    mail.From = new MailAddress("");
    mail.Subject = "Test Email";
    mail.Body = "Sample Email with Attachment";
    //making attachment
    System.Net.Mail.Attachment attachment;
    attachment = new System.Net.Mail.Attachment("C:/Users/Administrator/Desktop/php.txt");

    //Gmail Port
    SmtpServer.Port = 587;
    SmtpServer.Credentials = new System.Net.NetworkCredential("", "xxxxxxxxx");

    //You can specifiy below line of code either in web.config file or as below.
    SmtpServer.EnableSsl = true;  

Hope this will someone!!!

Your feedbacks are most welcome..