[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Email Multiple attachments using SMTP method in C#

Posted on 2011-03-02
9
Medium Priority
?
1,694 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

650 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