我创建了一个 Web 服务来获取在指定文件夹中找到的所有文件,例如 C:/Incoming/20121018 并将它们作为附件通过电子邮件发送到我指定的电子邮件地址。
我可以成功发送带有一个附件的邮件,但我想我会通过一个数组传递几个文件以作为附件发送。唯一的问题是,当我尝试读取包含文件的文件夹时,即使我拥有该文件夹的权限,我也会收到权限错误。知道我要去哪里错了吗?
请参阅下面的代码:
[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);
}