vb.netuipath-studio

(vb.net) Convert Mail Attachment to Base64 String


Working in vb.net within UiPath Studio, I have a mail attachment object (System.Net.Mail.Attachment). I want to directly convert the attachment to a Base64 string. Is there a way to accomplish this, perhaps by utilizing the ContentStream method?


Solution

  • I managed to complete this using direct assignments. I'm working with an attachment object named Attachment. First, create the variables:

    Stream as System.IO.Stream
    Buffer as System.Byte[]
    BufferLength as Int32
    

    Then use an Assign activity for the following:

    Stream = Attachment.ContentStream
    Buffer = new Byte(Cint(Stream.Length)){}
    BufferLength = Stream.Read(Buffer, 0, Buffer.Length)
    File_Base64 = Convert.ToBase64String(Buffer)
    

    The BufferLength assignment is a little hacky, but the Stream.Read method returns a value that is the length of the Read. That value isn't helpful for me, but to make this work in an Assign activity, I just accept the value in an integer variable.