Problem with Mail.Messages and Casting



Hi All

I am struggling to view emails that I have retrieved

I can scan a folder and check that a mail is present but I cant seem to open it. I think it has something to do with casting


I have tried to adjust to Web.Mail and that allows me to use .Body, .Subject etc but I cant seem to cast it correctly prior to this step

Trace below - Any suggestions

I had a look on forum but nothing to highlight step by step around what should be done

Email_Process has thrown an exception

Source: For each

Message: Unable to cast object of type ‘System.Net.Mail.MailMessage’ to type ‘System.Web.Mail.MailMessage’.

Exception Type: InvalidCastException

System.InvalidCastException: Unable to cast object of type ‘System.Net.Mail.MailMessage’ to type ‘System.Web.Mail.MailMessage’.
at UiPath.Core.Activities.ForEach1.InternalExecute(NativeActivityContext context, IEnumerator valueEnumerator) at UiPath.Core.Activities.ForEach1.Execute(NativeActivityContext context)
at System.Activities.NativeActivity.InternalExecute(ActivityInstance instance, ActivityExecutor executor, BookmarkManager bookmarkManager)
at System.Activities.ActivityInstance.Execute(ActivityExecutor executor, BookmarkManager bookmarkManager)
at System.Activities.Runtime.ActivityExecutor.ExecuteActivityWorkItem.ExecuteBody(ActivityExecutor executor, BookmarkManager bookmarkManager, Location resultLocation)


Hi @RogerFenergo,

If you are reading the mail from uipath activity it will return the datatype. You can’t convert net to web.

Arivu :slight_smile:


Thanks - how do I read the content though


Hi @RogerFenergo,

Using this data type its self you can use, refer the previous post and try it.

Arivu :slight_smile:


Here is some more detail on the error


Hi @RogerFenergo,

change the Properties->TypeArgumaent to “System.Net.Mail.MailMessage”

Arivu :slight_smile:


Perfect - Thanks Arivu