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 8032237
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T01:19:51+00:00 2026-06-05T01:19:51+00:00

I was recently looking at a piece of code that was attaching a file

  • 0

I was recently looking at a piece of code that was attaching a file to an email using System.Net.Mail.Attachment. The current implementation was storing the attachment as a file to a storage device and giving it a unique name. This file then was appended to the MailMessage as follows:

Attachment data = new Attachment(filename);
message.Attachments.Add(data);

Email was then sent and the file was then deleted from the storage device.

Wouldn’t it be faster/better for the file to just be stored in a MemoryStream (which it already is) and then just pass that as the attachment to the Email? I would imagine somewhere in the MailMessage class the file is being read into a stream and sent in the very same manner, and the whole process of storing it to disk is completely unnecessary.

My gut… Writing to disk is completely unnessarry and using the stream should be implemented. (am i wrong?)

  • 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-05T01:19:53+00:00Added an answer on June 5, 2026 at 1:19 am

    Yes, it would be better to avoid writing it to disk, unless that step is necessary for some other reason. The Attachment class can take a stream and a file name as arguments to its constructor, rather than giving it a physical file path.

    http://msdn.microsoft.com/en-us/library/6sdktyws.aspx

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

Sidebar

Related Questions

I recently started looking into building web applications using .NET MVC and I stumbled
I recently was looking at some code that uses SetLength to allocate memory for
I have recently been looking at code, specifically component oriented code that uses threads
So I was recently looking at someone's code and I saw that the compiler
I found this in some production login code I was looking at recently... HttpContext.Current.Trace.Write(query
Recently I was looking at some source code provided by community leaders in their
Recently I've been looking at opening a tab using this script: $('.tofour').click(function() { //
I have a piece of mature geospatial software that has recently had areas rewritten
I´ve recently started looking a web service calls using PHP. I can´t get the
Recently I've added some load-balancing capabilities to a piece of software that I wrote.

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.