Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8868503
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T17:17:14+00:00 2026-06-14T17:17:14+00:00

I have an excel file (Excel 2003 / xls format) and I want to

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T17:17:16+00:00Added an answer on June 14, 2026 at 5:17 pm

    I found a solution:

    ...
    var attachment = CreateAttachment(WriteFileToMemory(Common.TempPath + excelName), excelName);
    attachmentCollection.Add(attachment);
    ...
    
    private Stream WriteFileToMemory(string filePath)
    {
        var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
        _openedStreams.Add(fileStream);
        return fileStream;
    }
    
    public static Attachment CreateAttachment(Stream attachmentFile, string displayName)
    {
        var attachment = new Attachment(attachmentFile, displayName);
        attachment.ContentType = new ContentType("application/vnd.ms-excel");
        attachment.TransferEncoding = TransferEncoding.Base64;
        attachment.NameEncoding = Encoding.UTF8;
        string encodedAttachmentName = Convert.ToBase64String(Encoding.UTF8.GetBytes(displayName));
        encodedAttachmentName = SplitEncodedAttachmentName(encodedAttachmentName);
        attachment.Name = encodedAttachmentName;
        return attachment;
    }
    
    private static string SplitEncodedAttachmentName(string encoded)
    {
        const string encodingtoken = "=?UTF-8?B?";
        const string softbreak = "?=";
        const int maxChunkLength = 30;
        int splitLength = maxChunkLength - encodingtoken.Length - (softbreak.Length * 2);
        IEnumerable<string> parts = SplitByLength(encoded, splitLength);
        string encodedAttachmentName = encodingtoken;
        foreach (var part in parts)
        {
            encodedAttachmentName += part + softbreak + encodingtoken;
        }
        encodedAttachmentName = encodedAttachmentName.Remove(encodedAttachmentName.Length - encodingtoken.Length, encodingtoken.Length);
        return encodedAttachmentName;
    }
    
    private static IEnumerable<string> SplitByLength(string stringToSplit, int length)
    {
        while (stringToSplit.Length > length)
        {
            yield return stringToSplit.Substring(0, length);
            stringToSplit = stringToSplit.Substring(length);
        }
        if (stringToSplit.Length > 0)
        {
            yield return stringToSplit;
        }
    }
    

    Based on this source: http://social.msdn.microsoft.com/Forums/en-US/dotnetframeworkde/thread/b6c764f7-4697-4394-b45f-128a24306d55

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am looking to write to an excel (.xls MS Excel 2003 format) file
I have used the below code to open an Excel file in MS Access
In Excel 2003 I have a positional file and I want to space fill
I Have an Excel 2003 file with a line similar to this: I need
I have an excel file, validating one column to enter numbers only but it
I have an excel file and I want to read each column and create
I have an excel file that I want to embed in my C# assembly.
I'm trying to read an Excel file (.xls, I think Excel 2003 compatible) via
I want to import data from an Excel file - assume Excel 2003 /
I need to connect to an open Excel 2003 file using .NET 3.5 It

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.