?
Solved

Email Multiple attachments using SMTP method in C#

Posted on 2011-03-02
9
Medium Priority
?
1,508 Views
Last Modified: 2012-05-11
Hello Experts,

I want to attach more than one email attachments using same SendEmail Routine.

Currently, I'm doing for loop to send multiple emails for each attachment(or file). I need to attach multiple files located in same directory have PDF as extension(*.pdf) and send just one Email.

Thanks.
0
Comment
Question by:ASPDEV
[X]
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
  • 4
  • 3
  • 2
9 Comments
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 35021847
The MailMessage object (assuming you are using the standard .Net mail components) has an Attachments collection that you can add multiple items to. Details, and a sample, can be found here:

    http://msdn.microsoft.com/en-us/library/system.net.mail.mailmessage.attachments.aspx 
0
 

Author Comment

by:ASPDEV
ID: 35022087
There is AttachmentCollection class. But how can I say it attach all files with Extension .PDF in the directory.
0
 

Author Comment

by:ASPDEV
ID: 35022119
Since, I have many files located inthat directory, but I need only files to be attached is with *.PDF.
0
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 
LVL 52

Expert Comment

by:Carl Tawn
ID: 35022123
I'll assume you know how to create the attachments themselves (if not please say so and I can help with that); to get all of the PDFs in a directory and loop them you can do something like:
            System.IO.DirectoryInfo di = new System.IO.DirectoryInfo("path_to_your_folder");
            foreach (System.IO.FileInfo file in di.GetFiles("*.pdf"))
            {
                // add each 'file' as an attachment
                // each one exposes properties for creationdate, etc
            }

Open in new window

0
 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 35022180
Kinda two separate questions...

1) How to add multiple attachments:
Call MailMessage.Attachments.Add(new Attachment(path to attachment)); for each attachment.

2) How to list all files in a particular directory with a given extension:
With using System.IO;: new DirectoryInfo(path to folder).GetFiles(*.ext) will return an array of FileInfo's for each file in the folder path to folder with the extension *.ext

So, put the two together:
using System.IO;
using System.Net.Mail;

namespace ConsoleApplication1
{
	class Program
	{
		static void Main(string[] args)
		{
			using(SmtpClient smtp = new SmtpClient("mailserver.domain.com"))
			{
				using(MailMessage message = new MailMessage("sender@domain.com", "recipient@domain.com"))
				{
					message.Subject = "PDFs Attached";
					
					// Add all PDFs in "C:\test" to the message
					foreach(FileInfo file in new DirectoryInfo(@"C:\test").GetFiles("*.pdf"))
						message.Attachments.Add(new Attachment(file.FullName));

					smtp.Send(message);
				}
			}
		}

	}
}

Open in new window

0
 

Author Comment

by:ASPDEV
ID: 35022277
Carl tawn,

AttachmentCollection attachFile = new AttachmentCollection();

When I tried to create it , it gives  me compile error:

The type 'System.Net.Mail.AttachmentCollection' has no constructors defined
0
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 2000 total points
ID: 35022307
You don't need to create it yourself, it is already part of the MailMessage object. You simply need to add items to it:
// shorthand version without all the extra detail
message.Attachments.Add(file.FullName);

Open in new window

0
 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 35022309
Have a look at that link carl_town posted above in his first comment, http:#35021847

Basically you don't create an AttachmentCollection yourself, you just create the MailMessage - once that's done you can use message.Attachements.Add(), e.g.

MailMessage message = new MailMessage();
message.Attachments.Add(new Attachment("C:\\file1.txt"));
message.Attachments.Add(new Attachment("C:\\file2.txt"));
0
 

Author Closing Comment

by:ASPDEV
ID: 35022381
Awesome Post!
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

771 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