Solved

Email Multiple attachments using SMTP method in C#

Posted on 2011-03-02
9
1,287 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 500 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

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

733 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