2

I'm having difficulty attaching files to mail messages using C# MailMessage. I have searched on this at length and tried many things that I have come across. The current incarnation is:

string uploadFile1 = fulAttachment1.FileName;
MyMessage.Attachments.Add(
    new Attachment(fulAttachment1.PostedFile.InputStream, uploadFile1));

Where fulAttachment1 is the file picker control.

The errors I keep getting start out like this:

System.IO.FileNotFoundException: Could not find file 'C:\Windows\SysWOW64\inetsrv\neptun-300x299.jpg'

I don't understand why it's looking on the file system at all.

I should add that many of the techniques I have tried work in Visual Studio 2012 and when deployed to IIS on my development machine. The problems arise when it is deployed to IIS on the Internet host. They say there is no problem with the sites configuration, and I don't know enough about it to say otherwise.

Here's the rest of the error message:

at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost) 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.AttachmentBase..ctor(String fileName) at System.Net.Mail.Attachment..ctor(String fileName) at RSG.Webforms.Contact.btnSubmit_Click(Object sender, EventArgs e) in c:\Users\suser\Documents\Visual Studio 2012\Projects\RSA\Webforms\Contact.aspx.cs:line 40

Another thing I don't understand is why IIS on a remote server would refer to my local Visual Studio project

Echo Train
  • 99
  • 11
  • Looks like you are using the `FileUpload` control. Have you tried [`fulAttachment1.FileContent`](https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.fileupload.filecontent(v=vs.110).aspx) instead of `fulAttachment1.PostedFile.InputStream`? I'm not sure if it's just a shortcut for `PostedFile.InputStream`, but worth a shot. – crush Jul 28 '15 at 16:34
  • Thanks for the suggestion, but that did not solve the problem. I'm adding the full text of the error message to my original post... – Echo Train Jul 28 '15 at 17:12
  • 2
    It's not referring to your local visual studio project. It's referring to the debug symbols that are compiled with your program. That's the `.pdb` file. The project got compiled locally before being published, thus, it included the local paths in the PDB file. As for your problem, I'm not sure why it's trying to reference a file instead of consuming a stream. – crush Jul 28 '15 at 18:08

1 Answers1

2

As noted in the comments fuAttachment1.FileName is referencing to the file name; you have to access file byte stream instead.

string uploadFile1 = System.IO.Path.GetFileName(fulAttachment1.FileName);
MyMessage.Attachments.Add(new Attachment(fulAttachment1.FileContent, uploadFile1)));

References:

https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.fileupload_properties(v=vs.110).aspx

https://msdn.microsoft.com/en-us/library/system.net.mail.attachment.attachment(v=vs.110).aspx

Ondrej Svejdar
  • 21,349
  • 5
  • 54
  • 89
  • Thanks, this makes sense to me. I will try that route and report back. – Echo Train Jul 29 '15 at 22:13
  • OK, this failed at the line: string uploadFile1 = System.IO.Path.GetFileName(fulAttachment1.FileName); which makes it look like attaching the file is not so much the problem as that the control thinks the file is on the server, not the web client. I don't understand it. – Echo Train Jul 29 '15 at 22:20
  • This worked. It turned out I had two problems: Visual Studio was not copying files to the server when I tried to publish a single file, even thought it indicated the file had been successfully published, so some of the other variations I thought I was trying would probably have worked, too. That's a problem for a different thread. Thanks for the help. – Echo Train Aug 11 '15 at 18:41