Extract Email Attachment

Dear Forum Members,

I have designed a workflow for extracting email data in which user will give the number of emails he want to read, then user will select an option, based on which option he wants to extract the data( Email ID of the Sender, Sender Name, Email Subject), then BOT will filter the data based on that option and then write the filtered data into excel.

Now, in this workflow, I want to add that based on the input, BOT should check that email has attachment or not, if an email has attachment, then BOT should save the attachment and write the details into excel.

My BOT is working in that way that from the number of mails, it is checking that if email has attachment then it is saving the attachment. But it should first filter based on the user input. I don’t know, where I am wrong.

Please help with this. Thanks. :slight_smile:

1 Like

Hey @dimple.khurana,

You want to apply the filter in the Get outlook mail activity filter property

Something like below.

If you want more help on this, please let me know.

Thanks :slight_smile:

1 Like

Hey @Nithinkrishna,

Thanks for your response. I am using Gmail account and I have read the inbox by asking the user about the number of emails. Then by using for each, I have looped each email and used the if condition like if(mail.Attachments.Any) , this will collect all the emails which have attachment in a variable.

Then user will select, based on what he wants to download the attachment, then I am inputting the value and the details are also saved in excel along with Attachment Name.

I am not getting, where to use the “save attachment”, because if I use save attachment in the for loop above, then it will save all the attachments. And outside that for loop, how can I save the attachment, because I would need to use “mail” or “item” that could be of type “system.net.mail.mailmessage”. I hope, I have elaborate it right. :slight_smile:

1 Like

The place where you write the data to excel, here you need to add logic for save attachments.

You are iterating the mails and writing the data in excel right, if i’m not wrong.

If possible, please share xaml for better debugging.

Thanks :slight_smile:

1 Like

Actually, I am not writing all the data into excel. Just the data which is extracted after filteration.
Thanks :slight_smile:

1 Like

Hey @dimple.khurana, Now I got little idea on your logic.

Current Logic: Mails → DT → Processing

What I feel the best approach is to have all the list of mails as it is. Dont change it to a DT which is kind of redundant step.

Based on user input you filter, write and save attachment.

Updated Logic: Mails → Processing (UserInput → List Filter → Write to Excel & Save Attachment)

This is what I feel, the better one.

Correct me if I’m wrong.

Hope this helps… Thanks :slight_smile:

1 Like

I was using DT because as it iterate through each mail, it will save Sender Name, email id, email body to a variable DT, where I used Add Data Row. So that it could help in filteration part. Because in the end, I need all the seperate value to write into excel. Is there any way to achieve the results with this logic.?? Because if we don’t change into DT, after filteration, the result will come in DT. And for filteration, I need 2 DT, one for input and one for output.

1 Like

Yes I understand.

If you have it in list, you can filter and fetch a mail record where you can access the subject, sender name and attachments from the filtered object.

which will help you save the attachments.

Datatable you are using is for filter purpose right, filter and writing the data back to excel. You can achieve the same without it using a list.

1 Like

Yes, I can access these values. But my point, Suppose all the emails are in list, then for filtering, I would need the values, where I could set the condition. And If after filtering, I used for each to loop through each email, then it will loop through each email and not through the filtered one. I don’t know, May be I am not elaborating right.

1 Like

Okay, could you please confirm you may have multiple emails in filtered output right ?

1 Like

Yes, It can have multiple emails.

1 Like

Okay, So below is the logic,

  1. Get the user input (let’s assume email subject)

  2. Filter the overall list and iterate (just a sample)

    mailList.Where(function(mail) mail.Subject.Equals("<userinput>")).ToList

  3. Iterate with foreach - using above statement as collection

  4. Inside foreach body, add excel steps and save attachment step.

Here the original list always remains the same, but the query we used in step two gives us the temp filtered list to be used in foreach.

Hope this helps

Thanks :slight_smile:

1 Like

I would need to use different for each for different user input, i.e. one for email id of sender, one for sender name and one for email subject… Like this you are saying…??

Hey @Nithinkrishna,

Sorry to bother you again. But I tried the way you told yesterday. The statement “mailList.Where(function(mail) mail.Subject.Equals(”“)).ToList” is not filtering any mail. After inputting the text, the variable does not have any mail that is filtered, the email is present in the emaillist.
Can you please help with this. Thanks

1 Like


The code is working fine now as per the requirement. Thank You so much for your time and help.

1 Like

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