MailKit

by Voodoo Editor
3 years ago
1099 Views

Table of Contents


Using the “MailKit” Library in the Voodoo RPA Studio

If you have used the IE-mail listening step in your processes, it saves the incoming e-mails in the file you specify in .eml format. Attachment from email, CC, Mail to, Subject etc. If you want to get the “MailKit, MimeKit” library, you need to use.

Not : Libraries;

  • using System.Buffers
  • using System.Linq
  • using MailKit
  • using MimeKit
    • Steps to help when using:

1. System.Buffers.dll, It comes by default in VooDoo Studio and must be used when using the MimeKit Library. Download .Buffers.dll”> from here and add it back to the EDE File references.
Respectively; You can reference the library by going to
Advanced -> Code -> Action -> Open Editor -> Manage -> File References -> Add You can use these steps to reach the point that described before and refer System.Buffer.dll library to VooDoo RPA Studio.

2. MimeMessage.Load(“”) : The file path you specified in the E-mail listening step. (folder containing .eml files)

3. MimeMessage.Load(“”).Attachments.ToList() : Finds and lists Attachments in “.eml” file

4. MimeMessage.Load(“”).Subject.ToString() : Parses the Subject Content of the “.eml” file and turns it into a textual structure

5. MimeMessage.Load(“”).HtmlBody.ToString() : Parses the Mail Content of the “.eml” file and turns it into a textual structure

Example #1

The Example below shows how to parse E-Mail Attachment, Subject, E-Mail Text Content from “.eml” file.

namespace VoodooImplementation {
    using System;
    using VooDooCommonData.CommonInterface;
    using VooDooCommonData.InstanceData;
    using System.Linq;
    using System.IO;
    using System.Buffers;
    using MimeKit;
    
    public class ComputationEvaluator : System.MarshalByRefObject, IPlugInComputation {
        
        private bool result_;
        
        private VooDooCommonData.InstanceData.PlanDataInstanceManager planDataInstanceManager_;
        
        /// Default Constructor for class
        public ComputationEvaluator() {
        }
        
        public bool result {
            get {
                return result_;
            }
        }
        
        public virtual VooDooCommonData.InstanceData.PlanDataInstanceManager planDataInstanceManager {
            get {
                return planDataInstanceManager_;
            }
            set {
                planDataInstanceManager_ = value;
            }
        }
        
        /// Calculate
        public virtual void ExecuteComputation()
        {
            string emlFile = @"";                     // The path which is emphasizing ".eml" file for parsing Attachments from mail file.

            string parsedAttachmentFilePath = @"";    // The path is emphasizing the place which is parsed Attachments from eml file.

            string exportFile = @"" + "\\" + "fileName.txt"; // The path is emphasizing the place which is writing the parsed MailBody to a txt file which is related with path.

            // Subject of Mail Returned to Value of String            
            string subjectMail = parseEmailSubject(emlFile);
            
            // Running the method of parsing Attachment.
            extractAttachs(emlFile,parsedAttachmentFilePath);
            
            // Running the method of Parsing MailBody
            parseBodyToFile(emlFile,exportFile)
        }

        /*
        * MailKit : PARSING ATTACHMENT FROM EML FILE
        *
        * This Method is written for parsing Attachments from ".eml" file to a static folder path
        * It can be used current state or can be written in main function
        * Static folder path can be configured by developer
        *
        */

        public static void extractAttachs(string emlFile, string extractFolder)
        {
            if(!Directory.Exists(extractFolder))
            {
                Directory.CreateDirectory(extractFolder);
            }

            var mimeMessage = MimeMessage.Load(emlFile);
            var attachments = mimeMessage.Attachments.ToList();

            foreach (var attachment in attachments)
            {

                string fileName = "";
                
                using (var memory = new MemoryStream())
                {
                    if (attachment is MimePart)
                    {
                        ((MimePart)attachment).Content.DecodeTo(memory);
                        fileName = ((MimePart)attachment).FileName; 
                    }
                    else
                    {
                        ((MessagePart)attachment).Message.WriteTo(memory);
                        fileName = ((MimePart)attachment).ContentDisposition?.FileName;
                    }

                    File.WriteAllBytes(extractFolder + fileName, memory.ToArray());
                }

          } // Method Ends
        
        
        /*
        * MailKit : PARSING SUBJECT PART FROM EML FILE
        * 
        * It is used for Parsing Subject Part of mail from ".eml" file.
        * It returns the value as string of subject by helps of MailKit Library
        *
        */

        public static string parseEmailSubject(string emlFile)
        {
            var subject = MimeMessage.Load(emlFile);
            return subject.Subject.ToString();

        } // Method Ends

        
        /*
        * Mailkit : PARSING MAIL BODY FROM EML FILE
        *
        * It is used for Parsing Body Part of mail from ".eml" file.
        * Method parses MailBody and writes to a txt file.
        *
        */

        public static void parseBodyToFile(string emlFile, string exportFile)
        {
            var body = MimeMessage.Load(emlFile);

            using (StreamWriter sw = File.AppendText(exportFile))
            {
                sw.WriteLine(body.HtmlBody.ToString());
            }

        } // Method Ends
    }
}