Solved

Email Multiple attachments using SMTP method in C#

Posted on 2011-03-02
9
1,381 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
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

Technology Partners: 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!

Question has a verified solution.

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

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

690 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