How can we convert from String to System.Net.Mail.MailMessage

Hi All,

The requirement is that process emails(.msg files) from shared folder one by one. Open email, extract some data form mail body, get mail subject, from ID, received date and count of attachment. I have used directory. get files to get the emails(.msg files)from shared folder as ‘Array of stings’. Now how I can convert string to mailmessage so that I can get required all data to process further.

Do let me know some ways to achieve this.

Thanks,
Ravi

Hi,

why do you have to convert string to System.net.mail.message?

I dont think there is no way that string can be converted to Mail message variable type…

this might help

TIA

That array that you have is an array of the file paths and filenames, not an array of data inside the .msg files.

There are tons of posts on how to read .msg files.

https://forum.uipath.com/search?q=read%20msg%20file

Hello @rchin you need to create the custom activity of the given code just need to pass the path of the mail file below is the code and later on you can make this publish to market place which help other

Approach 1

Approach 2

Code for activities

using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Mail;
using MsgReader;
using System.Data;

namespace EmailProcessingLibrary
{
    public class EmailProcessor
    {
        public DataTable ProcessEmails(string folderPath)
        {
            DataTable emailData = new DataTable();
            emailData.Columns.Add("Subject", typeof(string));
            emailData.Columns.Add("From", typeof(string));
            emailData.Columns.Add("ReceivedDate", typeof(DateTime));
            emailData.Columns.Add("AttachmentCount", typeof(int));
            emailData.Columns.Add("Body", typeof(string));

            string[] msgFiles = Directory.GetFiles(folderPath, "*.msg");

            foreach (string msgFile in msgFiles)
            {
                MailMessage mailMessage = ParseMsgFile(msgFile);

                string subject = mailMessage.Subject;
                string from = mailMessage.From.Address;
                DateTime receivedDate = mailMessage.Headers.Date;
                int attachmentCount = mailMessage.Attachments.Count;
                string body = mailMessage.Body;

                emailData.Rows.Add(subject, from, receivedDate, attachmentCount, body);

                mailMessage.Dispose();
            }

            return emailData;
        }

        private MailMessage ParseMsgFile(string msgFilePath)
        {
            using (var msgReader = new Reader(msgFilePath))
            {
                return msgReader.CreateMailMessage();
            }
        }
    }
}

Note: you need to modify the miner code as per UiPath custom activities

@rchin it helps you solve your issue.

@mukesh.singh
Could you please share the sample workflow for approach 2 or explain how I can use this approach 2 in code?