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