Solved

Email Multiple attachments using SMTP method in C#

Posted on 2011-03-02
9
1,150 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
  • 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
 
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

762 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

19 Experts available now in Live!

Get 1:1 Help Now