pls advice me

ramesh perera asked on January 19, 2017 12:18

I have several coppies of one .cst file uploaded on server location. I need to email the latest file as a email attachment. please advice me how to do this

Thank you Ramesh

Recent Answers


Dawid Jachnik answered on January 19, 2017 12:38 (last edited on January 19, 2017 13:01)

Hi, you can do it using System.Linq:

var directory = new DirectoryInfo(Server.MapPath("/files/"));
var myFile = (from f in directory.GetFiles()
                  where f.Name.EndsWith(".cst") 
                  orderby f.LastWriteTime descending
                  select f).FirstOrDefault();


var email = new EmailMessage()
        {
            ...
        };

        email.Attachments.Add(new System.Net.Mail.Attachment(Server.MapPath(myFile.Name)));
        // Send e-mail
        EmailSender.SendEmail(email);
2 votesVote for this answer Mark as a Correct answer

ramesh perera answered on January 20, 2017 06:24

Hi Dawid! Thanks for your help. var directory = new DirectoryInfo(Server.MapPath("/files/")); in this line I Enter "C:/inetpub/uploadFiles/" as the path of the directory which is a server location. but if i tried to if (directory.Exists) it returns false (folder is not there)

Thank you! Ramesh

0 votesVote for this answer Mark as a Correct answer

ramesh perera answered on January 23, 2017 07:57

Hi Currently when i attache file using your code. it sent only the name of the attach file with the size of the file. but It is not downloadable

help me pls Ramesh

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.