How to find out if Mail has attachments or if it has no attachments?

Hello Everyone, I’m a beginner in UI Path and I have been trying my hands on Email Automation. While Practising outlook automation I came across Save Attachment Activity. I was just curious if there is a way to determine if there is/are any attachment(s) in the mail. Or if there is a way to fire a message to sender if in case there is no attachment in the mail.

Thank you .

2 Likes

Yes You can use “Item.Attachments.Any”

Regards…!!
Aksh

9 Likes

Can I get more details?
Thanks :slight_smile:

Yes it will check System.Net.Mail.AttachmentCollection(Item is here a System.Net.Mail message) is having any attachment or not and with .Any() will return the output in boolean like true or false so i am using it directly inside if condition. You can use count as well to get the attachment collection counts and make your own conditions to proceed further.

Regards…!!
Aksh

@aksh1yadav

When I use this condition it is always TRUE even when the mail has no attachments.

Try Message.Attachments.Count as well and let me know.

Regards…!!
Aksh

would you happen to know how to check if the email has excel attachments, so specific attachments?

In my process, someone could email with just an excel attachment, or three attachments, one of which is excel, or multiple attachments, none of which are excel etc.

If there is no excel attachment in the email (even if it has multiple different attachments, as per the examples above) I need to respond saying “invalid email” (ie I have to check each email for an excel attachment, I cant use the filter in the get outlook mail messages activity, as I need to respond to every email).

In my if condition I tried using:

item.Attachments.ToString.Contains(“.xlsx”)

I tested this on an email with an excel attachment, but it failed, showing a message box i put in place saying “no excel attachment” lol

Any help would be great!
thank you

Will you be able to use this?

If (item.Attachments.Where(Function(x) x.Name.Contains(“.xlsx”)).Count >0)
{
//Valid Email
}
else
{
//Invalid Email
}

4 Likes

Yes we can by using following attached snapshot way and for your reference here is the already discussed forum link:

Regards…!!
Aksh

Hey @saivig,
try not having any image in your signature if you have. (if condition .Attachments.Any is always True)

Jakub

2 Likes

I didnt find .Attachment property for this. why?

Check the type of object you are firing. it is System.Net.Mail.MailMessage type or not.

Regards…!!
Aksh

1 Like

Solved!

When i am using this property- Attachements.Any then the loop does not go in then condition.
Can you tell what could be possibly wrong?

Did u mention item.Attachments.Any (or) Attachments.Any?

Thanks,
Sreekanth.k

@SHAISTA Once check your mail for attachments mailattachmnts.xaml (9.1 KB)Once execute this file with your mail credentials
Thanks,
sreekanth.k

1 Like

Hi sreekanth

Although there are no attachements in few mails,then also your program is also not executing else loop.

@shaista,For me It’s working fine.Check your code again.If there are no attachments why will it execute Then,It should execute Else