Attaching files to email in .net application.

Posted on 2008-10-19
Last Modified: 2013-12-17

i need to look in a folder and if there are any files in the folder I need to attach all the files to an email in the .net application. can I please know how to check if any files exists in the folder and if exists then attach them to the email.

any help in this regard is appreciated.

Question by:SRM19
LVL 13

Expert Comment

ID: 22753290
System.IO.Directory exposes static methods for creating, moving, and enumerating through directories and subdirectories

This link shows how to add attachments to a MailMessage

You can send a MailMessage with the SmtpClient class

Accepted Solution

motocross_geek earned 500 total points
ID: 22753452
I have put the code in this message. Happy coding. I typically didn't do all the code for someone. But I thought this example will get you further then the previous links. You will need to reference the name spaces System.OI and System.Net.Mail
Make sure to mark this as the solution if it works out for you.

//If you want to control how many folder deep to scan in target
    const int HowDeepToScan = 4;
    public static void ProcessDir(string sourceDir, int recursionLvl)
        MailMessage MyMail = new MailMessage();
        MyMail.From = "";  
        MyMail.To = "";
        MyMail.Subject = "Your Subject";
        MyMail.Body = "This is the body of the message";
        SmtpMail.SmtpServer = "";
        if (recursionLvl <= HowDeepToScan)
            // Process the list of files found in the directory. 
            string[] fileEntries = Directory.GetFiles(sourceDir);
            foreach (string fileName in fileEntries)
                // do something with fileName
                //Attach File to MyMail Object
                MailAttachment attachment = new MailAttachment(Server.MapPath(fileName));
            // Recurse into subdirectories of this directory.
            string[] subdirEntries = Directory.GetDirectories(sourceDir);
            foreach (string subdir in subdirEntries)
                // Do not iterate through reparse points
                if ((File.GetAttributes(subdir) &
                     FileAttributes.ReparsePoint) !=
                    ProcessDir(subdir, recursionLvl + 1);

Open in new window


Author Closing Comment

ID: 31507630
hello motocross geek,

that was real kind of you to send the code. i will try this later but I am sure it will work. thanks a lot.

Featured Post

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK ( for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question