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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T23:09:59+00:00 2026-05-17T23:09:59+00:00

Trying to upload a word document then attach it to an email which is

  • 0

Trying to upload a word document then attach it to an email which is sent.

Currently getting an IO exception – Illegal characters in path.

Here’s code:

try
{
    var fileName = Path.Combine(Server.MapPath("/"), ResumeUpload.FileName);

    ResumeUpload.SaveAs(fileName);

    using (var resumeContent = new StreamReader(fileName))
    {
        while (!resumeContent.EndOfStream)
        {
            var emailAttachement = new Attachment(resumeContent.ReadToEnd());
            message.Attachments.Add(emailAttachement);
        }
    }

    var client = new SmtpClient();
    client.Send(message);
}
catch(Exception exception)
{
    // Handle exception...
}

Currently, the fileName variable is getting set to:

d:\sites\websitename\website.com.au\home\filename.docx

Any ideas why this would break?

Here’s exception

Illegal characters in path.   at System.IO.Path.CheckInvalidPathChars(String path)
  at System.IO.Path.GetFileName(String path)
  at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share)
  at System.Net.Mail.AttachmentBase.SetContentFromFile(String fileName, String mediaType)
  at System.Net.Mail.Attachment..ctor(String fileName)
  at JobApplication.JobApplication.SendEmail(StringBuilder emailText)
  • 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-05-17T23:10:00+00:00Added an answer on May 17, 2026 at 11:10 pm

    Attachment(string) accepts a string of the path. StreamReader.ReadToEnd returns the string contents. I suspect you simply want:

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

    Alternatively, use the constructor that accepts a Stream and the name/content-type. Note that there are some things to consider if you let people upload arbitrary files into the root of your web-server; I would try to avoid the need to write them to disk (unless you want them for archive), and I wouldn’t be writing them to my web-root.

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

Sidebar

Related Questions

I'm trying to upload a word document I have created in memory to Sharepoint
I'm trying to upload an MP3 file (using the paperclip plugin) and then read
I am trying to upload a file via a form and then save in
I´m trying to upload a file which is fetched from a server. But when
Im trying to upload my app to the app store. I'm getting bundle Identifier
Trying to upload a form with a file input, only getting the form fields
I'm trying to upload a file using Watin and C#. Under Windows XP, Watin
I am trying to upload a file from a flash widget to my rails
i am trying to upload an image from a jsp page using servlet. but
I'm trying to upload files within MVC. Most solution I saw on SO is

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.