System.Web.HttpException: Invalid mail attachment [c#]

frewfrux

Newcomer
Joined
Nov 15, 2005
Messages
8
System.Web.HttpException: Invalid mail attachment-edited-[c#]

I am trying to create a web form that emails a client when the "submit" button is clicked. In the form is the option to include an attachment. The emailing works fine, the attachment not so much.

I have a public Mailer class that actually sends the email (it works fine) and a method called addAttachment that barfs when trying to create the attachment object (I think). Here's the code (projectPath and TEMP_PATH are defined in a web.xml file, and mailer is declared globally):

private string addAttachment(HtmlInputFile file)
{
if (file.PostedFile != null)
{
// Get a reference to PostedFile object
HttpPostedFile ulFile = file.PostedFile;
// Get size of the file
int nFileLen = ulFile.ContentLength;
// Make sure the size of the file is > 0
if( nFileLen > 0 )
{
// Get the file name
string strFileName = Path.GetFileName(file.PostedFile.FileName);
// Create the path for the file
strFileName = System.Configuration.ConfigurationSettings.AppSettings["projectUrl"] + System.Configuration.ConfigurationSettings.AppSettings["TEMP_PATH"] + strFileName;
// Save the file on the server
file.PostedFile.SaveAs(Server.MapPath(strFileName));


/**********************************
This line is the one it keeps complaining about
**********************************/
// Create the email attachment with the uploaded file
MailAttachment myAttachment = new MailAttachment(@strFileName, MailEncoding.Base64);


mailer.message.Attachments.Add(myAttachment);
// Store filename so we can delete it later
return strFileName;
}
}
return null;
}

I've looked at file permissions and they seem to all be fine. I've tried this with and without the MailEncoding at it doesn't make a difference. The directory that it gets saved in is a virtual directory and the files are actually being saved there (I checked). I tried to see if there was any IOStream that might need to be closed prior to attaching the file but I couldn't find any (nothing I did seemed to make a difference), but I could have done it wrong.

Any help would be *greatly* appretiated!!!

Edit: I just realized that the line I was saying it was complaining about was the wrong line. I've corrected it now, but if you have viewed this before please be aware that this has changed.
 
Last edited:
The value of strFileName at the time of the error is "/www/temp/myFile.txt".

It is being saved in a virtual directory and does actually appear there (the upload is working just fine). I've checked the files permissions and they are all what I expected (everyone can read the file).
 
I'm pretty sure that it has something to do with not closing an IO stream explicetly, or something. Anyone know how to do that in c#?
 
Depends on how you are creating the file but all Stream derived classes have a close (and Dispose) method, the various ReaderClasses (StreamReader, BinaryReader etc.) also should have a Close (and Dispose) method.
If you are using C# you might want to wrap the stream handling code within a using block to ensure the stream is closed properly.
 
Last edited:
Ok, I've now found out that I needed to change the problem line to:

MailAttachment myAttachment = new MailAttachment(Server.MapPath(strFileName));

This fixed the error in the sense that I no longer get that error. However, I am also not getting an email, let alone an email with an attachment.

Other forms on the site send out emails just fine, but this one still isn't working...it's the only one using attachments.
 
Last edited:
ok...I've now got it working. For anyone who is interested, here's the reason it wasn't working:

http://cr.yp.to/docs/smtplf.html

My code created the body of the email using '\n' characters, and our new smtp server didn't automatically convert these to "\r\n". In the end I used the command "Environment.NewLine" to grab whatever new-line character was accepted and it worked.

After realizing that it worked I re-wrote the Mailer class to automatically convert all \n, \r, and \r\n to Environment.NewLine so that I wouldn't have to change all the varrious instances of \n.

I hope this may help someone else in the future.
 
Back
Top