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

  • Home
  • SEARCH
  • 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 122481
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T04:08:07+00:00 2026-05-11T04:08:07+00:00

I have an excel document represented as a byte[] and I’m wanting to send

  • 0

I have an excel document represented as a byte[] and I’m wanting to send it as an attachment in an email.

I’m having a bit of trouble constructing the attachment.

I can create an Attachment which has the following constructors:

(Stream contentStream, ContentType contentType) (Stream contentStream, string name) (Stream contentStream, string name, string mediaType) 

My idea at the moment is to create a MemoryStream from the byte[] and pass it to the method which creates the attachment.

Unfortunately I can’t see a way to obtain the intended filename and content type from the MemoryStream and I also can’t see how to supply the correct content type. There are options for plain text, Pdf, Rtf etc but none that I can see that immediately jump out at me as the one I should use for an Excel document.

The closest I can find is MediaTypeNames.Application.Octet which states:

The Octet member designates that the attachment contains generic binary data.

However, even if this is the one to use, unless it can be passed as a property of the Stream then my method for sending emails will only be able to send a byte[] as an Excel document…

Is there perhaps some other sort of Stream I could use? Or will I have to create my own type of Stream that has the details I need.

Surely someone out there has done this thing before and surely Microsoft would have thought this through to this level….

Any help would be much appreciated.

Update: Please don’t vote for any answers that use the constructors that take the filename as a string. I’m really needing help using the ones that take a Stream…I want to avoid having to write the file to disk, email it, and then immediately delete it. Since there is a method that allows me to do that I’d like to use that one if at all possible.

Solution Update

Conrad managed to find what I was looking for! Thanks heaps man!

I’ll just document the suggested solution just in case something happens to the content at the supplied link.

Credit for this solution goes to http://www.systemnetmail.com

static void AttachmentFromStream() {  //create the mail message MailMessage mail = new MailMessage();  //set the addresses mail.From = new MailAddress('me@mycompany.com'); mail.To.Add('you@yourcompany.com');  //set the content mail.Subject = 'This is an email'; mail.Body = 'this content is in the body';  //Get some binary data byte[] data = GetData();  //save the data to a memory stream MemoryStream ms = new MemoryStream(data);  //create the attachment from a stream. Be sure to name the data  //with a file and  //media type that is respective of the data mail.Attachments.Add( new Attachment( ms, 'example.txt', 'text/plain' ));  SmtpClient smtp = new SmtpClient('127.0.0.1'); smtp.Send(mail); } 

In my case, it just means I’ll have to change my method to take the filename and fileformat as strings. I’ll try using the Octet one…but failing that I’ll just pass in the official MIME type.

All things considered, this is a pretty obvious solution…but I do appreciate the help in solving it…and the good thing is this solution will be documented for future programmers who have the same problem.

Thanks again everyone for you help!

  • 1 1 Answer
  • 1 View
  • 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. 2026-05-11T04:08:08+00:00Added an answer on May 11, 2026 at 4:08 am

    The attachment constructor does indeed have a constructor that does what you need. I’m assuming you’re using the System.Net.MailMessage class from .NET Framework 2. If so read this link for some sample code of what you need

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

Sidebar

Ask A Question

Stats

  • Questions 214k
  • Answers 214k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer According to http://www.quirksmode.org/css/contents.html Adjacent Sibling Selectors are not correctly supported… May 12, 2026 at 10:41 pm
  • Editorial Team
    Editorial Team added an answer svn export -r BASE . /target/for/old-version I didn't test this… May 12, 2026 at 10:41 pm
  • Editorial Team
    Editorial Team added an answer Complex object graphs are serialized fine. Even cyclic references can… May 12, 2026 at 10:41 pm

Related Questions

I have an excel document represented as a byte[] and I'm wanting to send
Given a list of objects, I am needing to transform it into a dataset
I have a repeatable business process that I execute every week as part of
Whereever I have looked, the functional specifcations are some sort of documents with the
I have an interesting problem which I've been looking into and would appreciate some

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.