Save attachments activity overwriting attachments with the same name

Hi, I’m trying to download the attachments from a mail message with multiple files and the activity overwrites the files with the same name instead of giving it an index. I tried looping with a for each going trough the mail.attachments but the activity downloads all of the files and not only that specific one.
Any idea on how I can rename the attachments in the download process or downloading them one by one and rename them before the next attachment comes?

1 Like

Hi @JohnnyViquez,

Try to check this post below :

If the this post helped you to solve your problem, just mark this post as solved my friend.

If not , let me know.

Happy Automation!

~Diego Turati

Hi @DiegoTurati thanks but the when the attachments are saved is when they get overwrited so I need to download each attachment individually and rename it or rename them before downloadin

1 Like

@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));

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


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

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


Hi, this sounds great only that I’m getting some errors from the invoke code it doesn’t seems to be recognizing the declarations on the variables, is the any extra configurations I have to do?

Did you set Invoke Code Language properties to “CSharp”?

I don’t think I have the option to change in my activity at least

image This are the properties I have

Ah. What version are you using? Does it say which “language” your invoke code is using?
Mine looks like this:

Updated the dependencies and it’s working great, thank you so much @sagacity

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.