Save outlook attachments issue

Hi

We are doing an automation where we receive pdf file over email.

Few of them may contains special characters and those emails are not getting saved. (with no error)

is there any way to handle this issue?


Hi @pankajdhane ,

Save Mail Attachments uses the original attachment name directly.
If the name contains Windows-invalid filename characters. Then, Windows rejects the file path β†’ UiPath often doesn’t throw a clear error β†’ file simply doesn’t get saved.

So we must rename/sanitize the filename before saving.

Steps to fix this issue in UiPath:

STEP 1 β€” Don’t use β€œSave Mail Attachments”

Because it uses the original name (which may be invalid), so it fails silently.

STEP 2 β€” Instead loop through each attachment

Use a For Each activity:

  • For Each attachment in currentMailMessage.Attachments

STEP 3 β€” Get the attachment name

Use an Assign activity:

rawName = attachment.Name

STEP 4 β€” Clean the name (remove bad symbols)

Replace invalid characters with _ so Windows accepts the name.

Use Assign activity:

safeName = rawName.Replace(β€œ\”,β€œ_”).Replace(β€œ/”,β€œ_”).Replace(β€œ:”,β€œ_”) _
.Replace(β€œ*”,β€œ_”).Replace(β€œ?”,β€œ_”).Replace(β€œβ€β€œβ€,β€œ_”) _
.Replace(β€œ<”,β€œ_”).Replace(β€œ>”,β€œ_”).Replace(β€œ|”,β€œ_”)

This step makes the filename safe.

STEP 5 β€” Create the final path

Use Assign:

fullPath = pdfFolder + "\" + safeName

Now we have a clean path with no bad characters.

STEP 6 β€” Save with β€œInvoke Method”

Use an Invoke Method activity:

  • TargetObject: attachment

  • MethodName: SaveAsFile

Add parameter:

  • In β†’ String β†’ fullPath

This saves the file correctly using your cleaned name.

In-Short:
Save Mail Attachments fails because the file name contains illegal characters. So we clean the filename first, then save it using Invoke Method β†’ SaveAsFile.

Kindly mark it as accepted solution, if this works and resolves your issue. Thanks in advance :slight_smile:

Hi @pankajdhane

Issue is coming because special character are not saved try this firstly clean the file name
Try this
cleanFileName = System.Text.RegularExpressions.Regex.Replace(
attachment.Name,
β€œ[\/:*?β€β€œ<>|]”,
β€œ_”
)
Regards
Nishi

1 Like

Hi @pankajdhane

This happens because some attachments have characters that are invalid in Windows file names, so they don’t get saved and no error is thrown.

You need to sanitize the attachment name (remove or replace special characters like / \ : * ? " < > |) before saving, or save first and then rename the file to a safe name.

1 Like

HI @Harshini_Here , target object giving error message

@pankajdhane

Generally it would save with scpecial characters also…are the attachments encrypted?

to verify can you send a sample email with special characters yourself and check..looks like the attachment encryption issue or something else

cheers

Hi @pankajdhane

Run in Debug and check if all attachments are counted.If the count is correct, then the issue is the file name.Use Save Attachments, but before saving, rename the attachment by replacing special characters with β€œ_”.
Read the attachment name, clean it with a Replace or regex, then save it.

Happy Automation

we can read the file name and change it but how can we download it?

change will not be applied to attachment without saving the files

@pankajdhane

i think might be this happen due to another issue not by special char. pls try to debug and extract all attachment names, or use Try Catch to see if an error is thrown(Share error screenshot).
OR
Then read all attachment names into a list, check each name for special characters using an array of invalid characters.

Happy Automation

issue has been resolved.thanks all.

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