Bali C
Bali C

Reputation: 31231

Add email attachments from string array

I have some code that I am using to send an email with an attachment. The next thing I wanted to do was add attachments from a string array or a List. I had a google but I could only find attachments from byte arrays. Is it possible to do something like this?

Attachment[] attachments = new Attachment(string array);
mail.Attachments.Add(attachments);

I know that won't compile but just so you get the idea. Or is the only way to foreach an array and create attachments one at a time? Thanks.

Upvotes: 0

Views: 1904

Answers (1)

Austin Salonen
Austin Salonen

Reputation: 50225

There is no AddRange on an AttachmentCollection so you're out of luck doing it as a slick one-liner (short of creating an extension method). You could use something like this:

string[] attachmentNames = ...

foreach(var attachment in attachmentNames.Select(n => new Attachment(n)))
{
    mail.Attachments.Add(attachment);
}

Upvotes: 1

Related Questions