Sending email with attachments from C#

In many cases you may required to send emails using ASP.NET. You can have all the classes required to send an email in the System.Net.Mail namespace. You can send an email from ASP.NET as follows :

First you have to add these namespaces to your code behind file:

1. using System.Net;
2. using System.Net.Mail;

Then write the following method

public void email_send()
    MailMessage mail = new MailMessage();
    SmtpClient SmtpServer = new SmtpClient("");
    mail.From = new MailAddress("your");
    mail.Subject = "Test Mail - 1";
    mail.Body = "mail with attachment";

    System.Net.Mail.Attachment attachment;
    attachment = new System.Net.Mail.Attachment("c:/textfile.txt");

    SmtpServer.Port = 587;
    SmtpServer.Credentials = new System.Net.NetworkCredential("your", "your password");
    SmtpServer.EnableSsl = true;



Using the above you can send attachemebts also. Hope this helps :-)


  1. This is really helpful for me, thank you very much

  2. Hi, thank you for sharing the codes.

    I am trying your code but I got this error "The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at"

    that error happens at --> SmtpServer.Send(mail);

    Do you know how to solve this? Thank you

    1. Hi, Can you please check this link once. It explained the same issue.

  3. This will guarantee that the messages that you send contain data that they are occupied with. It likewise decreases the shot of individuals unsubscribing because of getting an excessive number of messages that they are not intrigued by. TalkTalk mail