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?
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
@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:
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?
I don’t think I have the option to change in my activity at least
Ah. What version are you using? Does it say which “language” your invoke code is using?
Mine looks like this:
This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.