Jump to content
Xtreme .Net Talk

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


Recommended Posts

Posted (edited)

[PLAIN]System.Web.HttpException: Invalid mail attachment-edited-[c#][/PLAIN]

 

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.

Edited by frewfrux
Posted

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).

  • Administrators
Posted (edited)

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.

Edited by PlausiblyDamp

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted (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.

Edited by frewfrux
Posted

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.

  • 3 years later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...