I have created a web service to take all files found in a folder specified, eg C:/Incoming/20121018 and email them as attachments to an email address that I specify.
I can send a mail with one attachment successfully, but I thought I would pass several files via an array to be sent as attachments. The only problem is that when I try to read the folder containing the files, I get a Permission error, even though I have rights to that folder. Any idea on where I’m going wrong?
See my code below:
[WebMethod]
public string Sending_Email(string strEmailAddrFrom, string[] strEmailAddrTo, int intTotalEmailTo, string [] strAttachement)
{
DateTime LeadDate;
LeadDate = DateTime.Now.Date;
string Year = Convert.ToString(LeadDate.Year);
string Month = Convert.ToString(LeadDate.Month);
string Day = Convert.ToString(LeadDate.Day);
string[] arr1 = new string[150];
string Loc = "C:\\Incoming\\" + "" + Year + "" + Month + "" + Day + "";
StreamReader reader = File.OpenText(Loc);
string contents = reader.ReadToEnd();
reader.Close();
DirectoryInfo di = new DirectoryInfo(Loc);
FileInfo[] fileList = di.GetFiles(".*.");
int count = 0;
foreach (FileInfo fi in fileList)
{
arr1[count] = fi.Name;
}
EmailAlert NewMail = new EmailAlert();
return NewMail.EmailSent(strEmailAddrFrom, strEmailAddrTo, intTotalEmailTo, arr1);
}
your error lies here you are trying to open folder as stream which is not right way.