Invoke Code activity error - Get Date from Headers

can you tell me what is the issue with the below c# code inside UiPath?

using System;
using System.Runtime.InteropServices;
using Outlook = Microsoft.Office.Interop.Outlook;

public void GetEmailHeaders(Outlook.MailItem email)
{
string headers = string.Empty;
Outlook.PropertyAccessor prop = email.PropertyAccessor;
string headerNames = “urn:schemas:mailheader:date”;

headers = (string)prop.GetPropertyr(headerNames);

MessageBox.show(headers, "Email Headers", MessageBoxButtons.OK, MessageBoxIcon.Information);

}

i just need to get the Date from the email headers, where i use the MailMessage item as Input of the Invoke Code action

Thank you

@ioannis

Can you tell if you are getting any error? Or are you getting wrong output?

And if you need the date only then why not use directly in an assign activity varmailmessge.Headers("theheadername").ToString…if you dont know the header key name properly then run the bot in debug mode…go to locals panel amd expand the mailmesaage variable which will contain all the information and check the header name

Cheers

Thanks for the quick response!
Below the error i see:

I was using the simple ‘CDate(out_TransactionItem.Headers(“Date”)).ToString(“dd/MM/yyyy”)’ but for some unknown reason, specific emails don’t have the Headers collection full completed (missing lots of items like Date, etc.), so i am trying to use pure code to check if resolves the issue.

@ioannis

To Start with imports would be done in the imports menu at the bottom of your studio…So remove using

And coming to the issue…yes sometimes headers are missing…but with using invoke code also the issue would be same …just so that you know…

but give it a try may be you might be successful…As mentioned remove the using statements to start with

cheers

1 Like

Hi,

This should be as the following. (I modified some incorrect lines.)

string headers = string.Empty;
Microsoft.Office.Interop.Outlook.PropertyAccessor prop = email.PropertyAccessor;
string headerNames = "urn:schemas:mailheader:date";
headers = (string)prop.GetProperty(headerNames);
System.Windows.Forms.MessageBox.Show(headers, "Email Headers", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information);

And also please check the following sample.

Sample20230314-1aL.zip (2.5 KB)

Regards,

1 Like

Thank you very much for the answer.
Seems that the code has no errors. Now, for testing it I need to convert my System.Net.MailMessage to MailItem so that I can input the var.

As soon as i figure out i will come back :slight_smile:

Hi @ioannis ,

If the Final goal is to get the Date of the MailMessage, then even if you convert the MailMessage item to an Outlook Item, the Date would not be available.

I believe the fix was applied in the latest versions of the UiPath Mail Activities, so if you are already using the Latest versions, I do not think it would be an issue. Check the below post on the same. However a testing may be necessary to be performed.

1 Like

Thank you for your message.

The thing is that the existing mail activities are working fine for some emails, but not correct for other emails. And specifically the issue is noticed in emails of certain Outlook subfolder.

Already waiting for Windows Client answers on that, but i wanted to cover in parallel other ways of retrieving the Date.

Hello again all,

Didn’t manage to run the Invoke Code activity, as i had to convert my input var, but the issue was solved by just upgrading the Outlook Mail Activities package from 1.15.2 → 1.18.2 version.

Thank you all!

1 Like

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