Attaching files to email in .net application.

Posted on 2008-10-19
Medium Priority
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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 2000 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 = "you@company.com";  
        MyMail.To = "sendingto@somebody.com";
        MyMail.Subject = "Your Subject";
        MyMail.Body = "This is the body of the message";
        SmtpMail.SmtpServer = "smtp.server.com";
        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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Suggested Courses

752 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