Sending Email with attachement using Asp.net

Background

In this the article we will learn how to send emails with attachment from an ASP.Net C# web application. Let us see step-by-step so beginners also can understand the logic to send emails from any provider. There are many techniques to send emails from ASP.Net with attachments but in this article, we will use an SMTP server with the Gmail provider to send the emails.

Prerequisites

  • Active internet connection.
  • Email id of any provider such as Gmail, Yahoo or your organization to send emails.

Let us create the application to send the emails from ASP.Net as:

  1. "Start" - "All Programs" - "Microsoft Visual Studio 2010".
  2. "File" - "New Project" - "C#" - "Empty web site" (to avoid adding a master page).
  3. Provide the project a name, such as "Sending Emails with file" or another as you wish and specify the location.
  4. Then right-click on Solution Explorer and select "Add New Item" - "Default.aspx" page and one class file.
  5. Drag and drop three Text Boxes and two buttons and one file upload control to attach files.
To send Emails in ASP.Net, the following namespaces must be added:
  1. using System.Net.Mail;  
  2. using System.Net;
To learn about the two namespaces above refer to the MSDN.

Now, move to the web.config file and add the email credentials in the app setting section so in the future, if your email credentials are changed then their is no need to change them in your code file since you can simply the change values in your app settings of the web.config file. After adding the credentials, the app setting section of the web.config file looks as in the following:

web config file source code
In the preceding app settings section, we are adding the sender email details credentials such as email id and word, so we can send the emails and later on we will read these values in the .cs file.

Note

In the preceding app Settings section add your valid email and word so emails can be sent from your email id to others.

now open default.aspx.cs page and create the function to send Email with attachment as

Emailfunction.png

Now call the preceding  function on the button send click of the "default.aspx.cs" page as in the following:
  1. protected void btnsend_Click(object sender, EventArgs e)  
  2. {  
  3.     SendmsgwithFile();       
  4. }  
I hope you have done all the preceding steps, now run the application and add the email details in all the text boxes with file and click on the send button as:

attachmentUI.jpg

From the preceding example we learned how to send emails with attachment, I hope you have done it.

Note
  • For detailed code please download the Zip file attached above.
  • Don't forget to update the "Web.config" file for your email credential details.

Summary

We learned how to send emails in ASP.Net with attachment. I hope this article is useful for all students and beginners. If you have any suggestion related to this article then please contact me.


Similar Articles