MailKit - Voodoo RPA

by Voodoo Editor
2 sene ago
413 Views

İçindekiler Tablosu


Voodoo RPA Programında “MailKit” Kitaplığının Kullanımı

IE-posta dinleme adımını süreçlerinizde kullandıysanız, gelen e-postaları belirttiğiniz dosyaya .eml formatında kaydeder. E-postadan ek, CC, Mail to, Subject vb. almak istiyorsanız “MailKit,MimeKit” kütüphanesini kullanmanız gerekmektedir.

Not : Kitaplıklar;

  • using System.Buffers
  • using System.Linq
  • using MailKit
  • using MimeKit
    • Kullanırken yardımcı olacak adımlar:

1. System.Buffers.dll, VooDoo Studio İçerisinde varsayılan olarak gelmektedir ve MimeKit Kitaplığını kullanırken kullanılması zorunludur.
İlgili Kitaplık versiyonu ile ilgili bir problem olması anında manuel olarak kütüphaneyi buradan indirin ve EDE Dosya referanslarına tekrar ekleyin.
Sırasıyla;
Gelişmiş -> Kod -> Aksiyon -> Editör Aç -> Yönet -> Dosya Referansları -> Ekle şeklinde ilerleyerek kitaplığı referans olarak gösterebilirsiniz.

2. MimeMessage.Load(“”) : E-posta dinleme adımında belirttiğiniz dosya yolu. (.eml dosyaları içeren dosya)

3. MimeMessage.Load(“”).Attachments.ToList() : “.eml” dosyası içerisindeki Attachment’ları bulur ve listeler

4. MimeMessage.Load(“”).Subject.ToString() : “.eml” dosyasına ait Konu İçeriğini ayrıştırır ve metinsel bir yapıya çevirir

5. MimeMessage.Load(“”).HtmlBody.ToString() : “.eml” dosyasına ait Mail İçeriğini ayrıştırır ve metinsel bir yapıya çevirir

Örnek #1

Aşağıdaki Örnek, “.eml” dosyasından E-Posta Eki, Konu, E-Posta Yazı İçeriğini nasıl ayrıştırılacağını gösterir.

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
    }
}