1

this morning my application developed in C# ASP.NET has a problem that has never occurred before

this application stores the filename within the column of a database

when it comes time to send the attachment via email, the application looks for the file name and attaches it to the message

the application cannot find the attached files on the physical path of the server even if these files are correctly present and block sending an email with attachment

the error is

The system cannot find the file specified 'C:\inetpub\wwwroot\public\newfile.pdf'

the file newfile.pdf instead it is located inside the public folder

how to do resolve this?

my simplified code below

List<string> listofattachments = new List<string>();


using (OdbcDataReader reader = cmd.ExecuteReader())
{
    if (reader.HasRows)
    {
        while (reader.Read())
        {
            listofattachments.Add(reader["attachment"].ToString());
        }
    }
}


foreach (string attachments in listofattachments)
{
   if (!string.IsNullOrEmpty(attachments))
   {
      mailMessagePlainText.Attachments.Add(new Attachment(@"C:\\inetpub\\wwwroot\\public\\" + attachments.Trim().ToString())); //line of error
   }
}
MickyD
  • 13,463
  • 6
  • 39
  • 60
  • 2
    code doesn't break by itself. what have you changed since it last worked? meaning both code _and_ server configuration? – Franz Gleichmann Aug 07 '20 at 08:36
  • @FranzGleichmann thanks for reply. nothing has changed in the code, in fact other users are able to send the email message... only one user reported this –  Aug 07 '20 at 08:37
  • 2
    Code is not the only variable here, how about permissions for the apppool to read the directory file ext. is it hidden... – TheGeneral Aug 07 '20 at 08:41
  • @TheGeneral thanks, on the public folder i have setting everyone user write and read. the newfile.pdf was entered two days ago, but this morning when you try to invalidate the message it crashes with error –  Aug 07 '20 at 08:43
  • 2
    `Attachment(@"C:\\inetpub\\wwwroot\\public\\" + attachments.Trim().ToString()` is really messy. Instead you should use `Path.Combine` then follow that up with a `File.Exists` or better yet `FileInfo.Exists`. Using `string` for path manipulation is error prone – MickyD Aug 07 '20 at 08:48
  • 2
    This is not my area of expertise, But "*everyone*" may not be enough for IIS (maybe someone could clarify that). You could try give your directory explicit permission , ISS AppPool\ – TheGeneral Aug 07 '20 at 08:50
  • 2
    The comment above by @TheGeneral poses some good points. Again, I'm no expert but I did find [this interesting post](https://stackoverflow.com/a/36597241/585968) – MickyD Aug 07 '20 at 08:55
  • thank you all for suggestions, i have solved using `Path.Combine` and `FileInfo.Exists` –  Aug 07 '20 at 15:11
  • @"C:\\inetpub\\wwwroot\\public\\" is wrong. It should either be @"C:\inetpub\wwwroot\public\" or "C:\\inetpub\\wwwroot\\public\\" – Flutterish Aug 07 '20 at 19:02

0 Answers0