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