Extract data out of body of a mail

Hi all,

I’m trying to identify how to extract the data out of this mail and store it as a variable.
I need to save “38” as variable “var_age”, “Prague” as “var_City” and so on.
The objective is to loop through all new mails with a certain subject, extract the values and write it in excel and then move the file from the inbox folder in Outlook to another folder.

Thanks in advance,
Kind regards

image

1 Like

It is achievable. Follow the below process.

  1. Read Email based on Subject filter like: “[subject]=‘Uipath demo’” . more details here :point_right: How to filter messages in Get outlook mail messages:

  2. Use option : Unread Email

  3. You will get an out List say mailList, so you can use forEach mail in the mailList, do the below:
    … a. Use Save Mail Message : pass mail, to: “temp.mht”
    … b. Use Start Process, pass “temp.mht”. This will open the mail in the browser
    … c. Use data scraping and scrape the table data into datatable
    … d. Covert dt data into a list type using bodytableList =(From row In DataTableMail.Select() Select String.Join(“,”,row.ItemArray)).ToList() → you will get List[{“Age”, “38”},{“City”, “Prague”}…]

  4. Now split to get each item: rowArray = bodytableList(0).Split(","c)
    So, you get rowArray(0) = Age, rowArray(1)= 38 and so on…

  5. Use activity Move Outlook Mail message, in: mail out, FolderName

See if this helps

7 Likes

Hi @nadim.warsi, thanks for your reply.
I think I’m doing something wrong at step 3.B. Can you have a look please?
When running it, i get the error “application can not be opened”.


image

Also, 3.d, what do you mean with “using bodytablelist”? which activity is this?

Thanks

2 Likes

Hi @yannip,

I believe that in your Start Process activity you should add the application path i.e. Outlook.

Besides that, you will also add the full path to your .mht file.

2 Likes

Like @acaciomelo was saying,
Start Process is to be used to open Applications, and .mht files are not applications. You can place “temp.mht” in the app arguments though, and put the application in the first field that will be opening the file (for example, the path to Internet Explorer or Outlook)

Regards.

4 Likes

Can you put the path that you have in the save process in the start process. That should fix this.

3 Likes

Hi @ClaytonM,

That indeed solved my problem. I don’t get the error anymore.
However my write line shows that the body of my mail is empty, however it is not empty in reality.


The mail of which I expect the body to be extracted:

What am I missing as it is getting the subject but not the body…

Thanks

4 Likes

Hmmm. Can you try outputting the body of several emails in the Loop, ie outside of the If activity, so you can see everything?; maybe it’s showing a different email where the body was empty.

2 Likes

Hi @ClaytonM,

I tried but all the write lines for item.body.tostring are empty.
The item.subject.tostring is being treated correctly.

Can you have a look at the xaml please? It must be something easy like a parameter or a variable type that is wrong, but I can’t find it: mail test.xaml (7.3 KB)

(I just changed the account of the mailbox to not share it, but I’m using the correct account mailbox.)

1 Like

It looks right to me.
You could try erasing .body and select it from the dropdown, shown in below image:
image

Although, that shouldn’t matter.

Only thing I can think of is that your messages don’t have a body. You should be able to tell that though by highlighting the text in the email to make sure it’s text-based. I think I would need an example of an email to look it over myself.

Another thing you can look at is the Headers which can contain other details of the message:
image
It’s a collection so I used String.Join to output all the items.

Regards.

1 Like

Hi @ClaytonM,

Thanks again for your reply. I tried your solutions (capital B for body) and joining the headers. But without result. Also, the mail is text based because when screen scraping, I do get the text.
However, when joining the headers I get a strange result as well in the output: “system.net.mime.Headercollection”.

Maybe the body problem and this have something related to each other?
Or is there another way to get the text out of the mails and storing it somewhere?

Thanks!!

1 Like

Sorry, to output the headers you need to convert the items to a string. You can use a For each loop or use the following one-liner:

String.Join("|",item.Headers.Select(Function(x) x.ToString))

I’m running out of ideas.

Hopefully, you can figure this out!

Regards

2 Likes

I tried it but get a different error now:

However, I am running out of ideas as well.
I found a lot of topics (also old ones) where people are having the same issue without solutions.
They can read the subject but not the body.

I also can’t use the activity “save mail attachments”.

1 Like

Sorry, it’s been a while since I was using the .Headers collection. You might need .ToArray before the .Select.

You can also hardcode the Header items, like item.Headers(0).ToString and item.Headers(1).ToString and so on.

Though, I don’t think that will give you the body anyway.

Another thing to try is if you can use the Exchange server then use Get Exchange Mail Messages.

2 Likes

@ClaytonM

This is the result for the headers… It seems to work…
However, the body is still not working.

As you can see, there is a body in this mail:

1 Like

hi @yannip have you found the solution for that?

I am having the same problem, empty body using get IMAP, instead, the Object is working…

1 Like

Hi @yannip @Stefano_Skjupyter

Does this post help you by any chance?

3 Likes

Hi, @Stefano_Skjupyter and @loginerror

I did not manage to solve it at that time. After running in circles for several months not finding a solution we decided not to automate this part. Also, we had another team at our company trying to automate such things and they did not manage either. For us, it seems to be an IT group policy thing that cannot be changed. It has something to do with a third party software not being allowed to use Outlook as far as I know.

Thanks anyhow…

1 Like

Jesus Christ.
To anyone: don’t do this.

This may be an old(ish) thread but don’t “open the mail in the browser” and then “use data scraping” from there - sub-optimal and waste of processing power.
Just imagine someone is implementing this suggestion (by a “Robot Master”?!) and uses it hundreds of times a day.

3 Likes

@MaurerDa It will be rather great if you provide a more optimal solution to a old thread if you feel there is one.
It will actually be helpful to the community readers reading this post :slight_smile:

https://forum.uipath.com/guidelines

5 Likes