Solved

Attaching files to email in .net application.

Posted on 2008-10-19
3
952 Views
Last Modified: 2013-12-17
hello,

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.

Thanks,
SRM.
0
Comment
Question by:SRM19
3 Comments
 
LVL 13

Expert Comment

by:josgood
ID: 22753290
System.IO.Directory exposes static methods for creating, moving, and enumerating through directories and subdirectories
http://msdn.microsoft.com/en-us/library/system.io.directory.aspx

This link shows how to add attachments to a MailMessage
http://msdn.microsoft.com/en-us/library/system.net.mail.mailmessage.attachments.aspx

You can send a MailMessage with the SmtpClient class
http://msdn.microsoft.com/en-us/library/system.net.mail.smtpclient.send.aspx
0
 
LVL 2

Accepted Solution

by:
motocross_geek earned 500 total points
ID: 22753452
SRM19,
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";

        SmtpMail.Send(MyMail);
 

        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

                Console.WriteLine(fileName);
 

                //Attach File to MyMail Object

                MailAttachment attachment = new MailAttachment(Server.MapPath(fileName));

                MyMail.Attachments.Add(attachment);

            }
 

            // 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) !=

                         FileAttributes.ReparsePoint)
 

                    ProcessDir(subdir, recursionLvl + 1);

        }
 

        SmtpMail.Send(MyMail);

    }

Open in new window

0
 

Author Closing Comment

by:SRM19
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.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

895 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now