activities
latest
false
Productivity Activities
Last updated Oct 23, 2024

Create and Save an Email Locally

The example below explains how to create and save an Outlook mail with an attachment. It presents activities such as Save Mail Message and Save Attachments. You can find these activities in the UiPath.Mail.Activities package.

This is how the automation process can be built:

  1. Open Studio and create a new Process.
  2. Drag a Sequence container in the Workflow Designer.

    • Create the following variable:

      Variable Name

      Variable Type

      Default Value

      mailMessage

      System.Collections.Generic.List<System.Net.Mail.MailMessage>

       
  3. Drag an Invoke Code activity inside the Sequence container.

    • Click the Edit Code button and add the following code:

      "'Create a message and set up the recipients.
      Dim sender As String = "sender@outlook.com"
      Dim receiver As String = "receiver@outlook.com"
      Dim subject As String = "Project plan"
      Dim body As String = "See the attached document."
      message = New MailMessage(sender, receiver, subject, body)
      'Create the file attachment for this email message.
      Dim data As Attachment = New Attachment("Input\Project-plan.xlsx")
      'Add the file attachment to the email message.
      message.Attachments.Add(data)""'Create a message and set up the recipients.
      Dim sender As String = "sender@outlook.com"
      Dim receiver As String = "receiver@outlook.com"
      Dim subject As String = "Project plan"
      Dim body As String = "See the attached document."
      message = New MailMessage(sender, receiver, subject, body)
      'Create the file attachment for this email message.
      Dim data As Attachment = New Attachment("Input\Project-plan.xlsx")
      'Add the file attachment to the email message.
      message.Attachments.Add(data)"
    • Click the Edit Arguments button and create a new argument named message, with the Direction set as Out, the Type set as MailMessage, and the Value set as mailMessage.
      Note:
      Make sure to replace the sender@outlook.com and receiver@outlook.com email addresses with the desired ones.

      Different email accounts are used in the attached project.

  4. Drag a Save Mail Message activity below the Invoke Code activity.

    • Add the variable mailMessage in the Mail Message field.
    • Add the expression "Output\email.eml" in the File Path field.
  5. Drag a Save Attachment activity below the Save Mail Message activity.

    • Add the variable mailMessage in the Mail Message field.
    • Add the expression "Output\Attachments" in the Folder Path field.
    • This is how your workflow should look:



  6. Run the process. A mail is created with the sender and receiver set by you and with an attachment included. The mail and the attachment are then saved to the specified folder.

    Note: The output folder in the project zip already contains an attachment, the folder should be empty.

Was this page helpful?

Get The Help You Need
Learning RPA - Automation Courses
UiPath Community Forum
Uipath Logo White
Trust and Security
© 2005-2024 UiPath. All rights reserved.