Sending email by checking the attachment file size

Hi There,

I want to send an email attaching the files in the specific folder. But i need to check the file size and the attachment file size should not exceeds 20MB.

In case if the file size exceeds 20MB then i need to split the files and send as two emails.

Please help me with the simplest logic.

TIA
Ramya A

Here’s a step-by-step breakdown:

  1. Get Files : Use the “Directory.GetFiles” method to get the list of files in the specific folder.
  2. Loop Through Files : Use a For Each loop activity to iterate through each file in the folder.
  3. Check File Size : Inside the loop, use the “File.Length” method to get the size of each file.
  4. Split Files : If the file size exceeds 20MB, split the file into smaller parts. You can use activities like “Read Text File” to read the content of the file and then split it based on your desired logic.
  5. Attach and Send Email: Use the “Send Outlook Mail Message” activity (assuming you’re using Outlook) to send the email. Attach the file or split files to the email as attachments.
  6. Repeat or End: Depending on your requirements, you may want to repeat the process for each file in the folder or end the process after sending all the emails.

Hi Rohan,

Thanks for your reply.

But the logic i need is not to split the file by itself.

I need to check the total size of email attachments. Total MB of the attached files should not exceed 20MB. In case if it exceeds, then i need to split the no of files, attach in 2 emails and send it accross.

Example:
Folder contains 6 files - 5 MB each, in this case i need to attach the first 4 files in one email and last 2 files in the second email.
So, how to derive this logic on the go while sending an email.

TIA
Ramya A

files = GetFilesInFolder(folderPath)
//initially need to assign
totalSize = 0
filesToSend =
emailsToSend =
// here need to calculate and check the file size
for each file in files:
fileSize = GetFileSize(file)
if (totalSize + fileSize <= 20MB):
AddFileToGroup(filesToSend, file)
totalSize += fileSize
else:
AddGroupToEmail(emailsToSend, filesToSend)
filesToSend = [file] // Start a new group
totalSize = fileSize

// Handle the last group of files
if (filesToSend.Count > 0):
AddGroupToEmail(emailsToSend, filesToSend)

for each email in emailsToSend:
SendEmailWithAttachments(email)

Thanks for the clear steps Rohan.

If you can help me with the UiPath code as well, it would be of great help.

TIA

I appreciate your kind words. If you need any assistance, feel free to ask! and also please mark as solution.