I have a folder which will contain .pdf
and .xls
but the numbers will vary from time to time and this is the main issue for me.Now as per my requirement i have to read the location of both of these types of files and add as attachment into my smtp mail code like this ..
Attachment att = new Attachment(sourceDir);
mail.Attachments.Add(att);
This is for the one file attachment .How to add all as described above as attachement in this code.. Please help me..
You could try something like this:
// Get all the files in the sourceDir
string[] filePaths = Directory.GetFiles(@"sourceDir");
// Get the files that their extension are either pdf of xls.
var files = filePaths.Where(filePath => Path.GetExtension(filePath).Contains(".pdf")
|| Path.GetExtension(filePath).Contains(".xls"));
// Loop through the files enumeration and attach each file in the mail.
foreach(var file in files)
{
var attachment = new Attachment(file);
mail.Attachments.Add(attachment);
}