How can I send an Excel file by email?

calendar_today Asked Nov 4, 2012
thumb_up 5 upvotes
history Updated April 14, 2026

Direct Answer

You are making it too complex, as @Magnus points out, new Attachment() can handle FileStreams so just pass a new filestream to the constructor. … var attachment = new…. This is a 6-line Excel VBA snippet, ranked #220th of 303 by community upvote score, from 2012.


The Problem (Q-score 8, ranked #220th of 303 in the Excel VBA archive)

The scenario as originally posted in 2012

I have an excel file (Excel 2003 / xls format) and I want to send it by email with c#.
My code send it successfully, but when I try to open the response file, it seems to encoded wrongly.

For example here is the response filename:

=_utf-8_B_RWxzesOhbW9sw6FzXzIwMTJfMTBfMTZf.dat

And here is the response file itself:

=?utf-8?B?VEdWdmJIWmhjMkZ1Wk1Pelh6UXlYekZmPz0NCiA9P3V0Zi04P0I/VGtW?=
=?utf-8?B?TlgwZFRXaTU0YkhNPT89?=” Content-Transfer-Encoding: base64
Content-Disposition: attachment

0M8R4KGxGuEAAAAAAAAAAAAAAAAAAAAAPgADAP7/CQAGAAAAAAAAAAAAAAABAAAA
AQAAAAAAAAAAEAAAIwAAAAEAAAD+////AAAAAAAAAAD/////////////////////
////////////////////////////////////////////////////////////////
////////////////////////////// ….

Here is my code fragment:

...
var attachment = new Attachment(WriteFileToMemory("fileFullPath"), "fileName.xls");
attachment.ContentType = new ContentType("application/vnd.ms-excel");
attachmentCollection.Add(attachment);
...


private Stream WriteFileToMemory(string filePath)
{
    var memoryStream = new MemoryStream();
    _openedStreams.Add(memoryStream);
    using (var file = new FileStream(filePath, FileMode.Open, FileAccess.Read))
    {
        var bytes = new byte[file.Length];
        file.Read(bytes, 0, (int) file.Length);
        memoryStream.Write(bytes, 0, (int) file.Length);
        file.Close();
    }
    memoryStream.Position = 0;
    return memoryStream;
}

How can I set the attachment encoding type, and which encoding should I use with Excel files?

Please help me solve this problem.
Thanks in advance.

Why community consensus is tight on this one

Across 303 Excel VBA entries in the archive, the accepted answer here holds niche answer (below median) status — meaning voters are unusually aligned on the right fix.


The Verified Solution — niche answer (below median) (+5)

6-line Excel VBA pattern (copy-ready)

You are making it too complex, as @Magnus points out, new Attachment() can handle FileStreams so just pass a new filestream to the constructor.

...
var attachment = new Attachment(File.Open("fileFullPath", FileMode.Open), "fileName.xls");
attachment.ContentType = new ContentType("application/vnd.ms-excel");
attachmentCollection.Add(attachment);
...

A word of warning though, kind is off topic, you cannot send a mail like that multiple times since the stream will not always reset itself properly.


When to Use It — vintage (14+ years old, pre-2013)

Ranked #220th in its category — specialized fit

This pattern sits in the 99% tail relative to the top answer. Reach for it when your scenario closely matches the question title; otherwise browse the Excel VBA archive for a higher-consensus alternative.

What changed between 2012 and 2026

The answer is 14 years old. The Excel VBA object model has been stable across Office 2013, 2016, 2019, 2021, 365, and 2024/2026 LTSC, so the pattern still compiles. Changes that might affect you: 64-bit API declarations (use PtrSafe), blocked macros in downloaded files (Mark-of-the-Web), and the shift toward Office Scripts for web-first workflows.

help
Frequently Asked Questions

This is a below-median answer — when does it still fit?
expand_more

Answer score +5 vs the Excel VBA archive median ~4; this entry is niche. The score plus 8 supporting upvotes on the question itself (+8) means the asker and 4 subsequent voters all validated the approach.

Does the 6-line snippet run as-is in Office 2026?
expand_more

Yes. The 6-line pattern compiles on Office 365, Office 2024, and Office LTSC 2026. Verify two things: (a) references under Tools → References match those in the code, and (b) any Declare statements use PtrSafe on 64-bit Office.

This answer is 14 years old. Is it still relevant in 2026?
expand_more

Published 2012, which is 14 year(s) before today’s Office 2026 build. The Excel VBA object model has had no breaking changes in that window. Three things to re-test: (1) blocked macros on downloaded files (Mark-of-the-Web), (2) 64-bit API declarations (PtrSafe, LongPtr), (3) any shift toward Office Scripts for web scenarios.

Which Excel VBA pattern ranks just above this one at #219?
expand_more

The pattern one rank above is “XLRDError: Expected BOF record; found 0x4b50”. If your use case overlaps, compare both before committing.

Data source: Community-verified Q&A snapshot. Q-score 8, Answer-score 5, original post 2012, ranked #220th of 303 in the Excel VBA archive. Last regenerated April 14, 2026.