Asp net send email with excel attachement


ASP.NET is a popular programming language used for web applications. One common requirement in web applications is the ability to send emails with attachments. In this article, we will explore how to send an email with an Excel attachment using ASP.NET.


Before we begin, make sure you have the prerequisites:

  • Visual Studio installed on your machine
  • An SMTP server to send emails

Step 1: up the ASP.NET

First, let's create a new ASP.NET project in Visual Studio. Open Visual Studio and go to File > New > Project. Select the ASP.NET Web Application template and give your project a name. Click OK to create the project.

Step 2: Adding the Required NuGet Packages

In to send emails, we need to install the MailKit and MimeKit NuGet packages. Right-click on your project in the Solution Explorer and select Manage NuGet Packages. Search for MailKit and MimeKit and install both packages.

Step 3: Writing the Code

Now, let's the code to send an email with an Excel attachment. Open the HomeController.cs file in your project and add the following code:

using MailKit.Net.Smtp;
using MimeKit;

public class HomeController : 
    public IActionResult SendEmailWithAttachment()
        var message = new MimeMessage();
        message.From.Add(new MailboxAddress("Sender Name", ""));
        message.To.Add(new MailboxAddress("Recipient Name", ""));
        message.Subject = "Email with Excel Attachment";

        var builder = new BodyBuilder();
        builder.TextBody = "Please find the attached Excel file.";

        var attachment = new MimePart("application/")
            Content = new MimeContent(File.OpenRead("path/to/excel/file")),
            ContentDisposition = new ContentDisposition(ContentDisposition.Attachment),
            ContentTransferEncoding = ContentEncoding.Base64,
            FileName = "example.xlsx"

        message.Body = builder.ToMessageBody();

        using (var client = new ())
            client.Connect("", 587, );
            client.Authenticate("username", "password");

        return View();

In the above code, we first create a new instance of the MimeMessage class and set the sender, recipient, and subject of the email. We then create a BodyBuilder object and set the text body of the email. Next, we create a MimePart object for the Excel attachment and add it to the Attachments collection of the BodyBuilder. Finally, we send the email using the SmtpClient class.

Step 4: Testing the Email

To test the email sending functionality, let's add a button to the view. Open the Index.cshtml file in the Views/Home folder and add the following code:

    ViewBag.Title = "Home Page";

Welcome to ASP.NET!

To send an email with an Excel attachment, click the button below:

Now, when you run the application and click the “Send Email” button, it will send an email with the Excel attachment to the specified recipient.


In this article, we have learned how to send an email with an Excel attachment using ASP.NET. By following the steps outlined above, you can easily incorporate email sending functionality into your ASP.NET web applications.

Rate this post

Leave a Reply

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

Table of Contents