0

我在文件夹中有多个图像,但我希望在附件中发送一些图像。我有arraylist其中包含文件名,我想将这些arraylist项目传递给filepaths但它不起作用。filess在循环之外无法访问。请帮助我,谢谢

myMail.Subject = pSubject;
myMail.Body = "Hi ,your event ID is " + pBody;
string[] filePaths = Directory.GetFiles(@"D:\test");
ArrayList list = new ArrayList(27);
list.Add ("12");
list.Add ("1288");
list.Add("1232");
list.Add("1222");
list.Add("1099");

foreach (string listitem in list )
{
 var filess = filePaths.Where(x => 
                  Path.GetFileName(x).Contains(listitem));
}

// Loop through the files enumeration and attach each file in 
//the mail.
foreach (var file in filess)
{
    var attachment = new 
    System.Web.Mail.MailAttachment(file);
    myMail.Attachments.Add(attachment);
}

System.Web.Mail.SmtpMail.SmtpServer = 
"smtp.gmail.com:465";
System.Web.Mail.SmtpMail.Send(myMail);
4

1 回答 1

0

您可以在此处查看我的答案以获取完整的片段以发送带有附件的邮件。但这里是读取文件夹中所有文件并将它们添加为附件的代码。但是文件必须在网站的根文件夹中!

//find all files in a directory relative to the root folder of the website
string[] files = Directory.GetFiles(Server.MapPath("/images"));

//loop all files in an directory
foreach (var file in files)
{
    //add the file from the fileupload as an attachment
    var stream = new FileStream(file, FileMode.Open);
    message.Attachments.Add(new Attachment(stream, Path.GetFileName(file)));
}
于 2019-04-04T21:16:58.820 回答