Save attachments activity overwriting attachments with the same name

@JohnnyViquez Try this. Create a For Each Activity within the For Each Activity for the emails. Whatever your email object is, add .Attachments so you can iterate through each attachment in the email, one at a time.

Then, use Invoke Code Activity to operate on each email attachment and save it with some random text in the name so it is not overwritten.

I wrote this C# code:
byte[] allBytes = new byte[item.ContentStream.Length];
int bytesRead = item.ContentStream.Read(allBytes, 0, (int)item.ContentStream.Length);
Random num = new Random();

 string destinationFile = @"C:\temp\" + item.Name + num.Next().ToString();

 BinaryWriter writer = new BinaryWriter(new FileStream(destinationFile, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None));
 writer.Write(allBytes);
 writer.Close();

“item” is the “In” argument with the assigned value of whatever your attachment object is.

SSs:

Note Im’ iterating through each “email” in the “messages” collection that came from the getmail activity.
Then, i’m iterating through each "attach"ment in the “email.attachments” collection for each email.
Then, for each attachment, I’m invoking the C# code to save the attachment with a unique name.

Obviously, modify to suit your needs.
Type argument for attachemnts is “System.Net.Mail.Attachment” as the get mail activity I used sets the collection full of “System.Net.Mail.MailMessage” objects

EDIT:
So in my example email, that had 2 attachments called: Add On.pdf it saved as:
image

6 Likes