I have some code that moves emails from a folder to another folder in C#.
I use EAGetMail for moving the emails. I tested this code on a basic Office365 account and it works fine, unfortunatelly I have issue with Exchange Online (plan 1).
For Office365 I can move the email, with Exchange Online the mail is not found whereas the Message-ID is retrieved.
Is this a configuration issue ?
public MoveEmailResponse MoveEmail(EmailAccountSettings emailAccountSettings)
{
try
{
MailServer oServer = Utils.SetMailServer(emailAccountSettings);
MailClient oClient = new MailClient("TryIt");
oServer.SSLConnection = emailAccountSettings.Authentication.Ssl;
oServer.Port = emailAccountSettings.Authentication.ServerPort;
oClient.Connect(oServer);
Imap4Folder storageRootFolder = Utils.FindFolder(emailAccountSettings.ExchangeRootFolder, oClient.GetFolders());
if (storageRootFolder == null)
{
throw new Exception("Folder not found!");
}
Console.WriteLine("Found !!");
oClient.SelectFolder(storageRootFolder);
MailInfo[] infos = oClient.GetMailInfos();
Console.WriteLine("Nombre de mails dans le folder : " + storageRootFolder + " / " + infos.Length);
// step 1: create folders in inbox folder
if (emailAccountSettings.StoragePath != null)
{
// split full path
Utils.CreateExchangeStorageFolders(oClient, emailAccountSettings.StoragePath);
}
if (emailAccountSettings.StoragePath != null)
{
oClient.GetMailInfosParam.HeaderContains = emailAccountSettings.EmailId;
infos = oClient.GetMailInfos();
if (infos.Length > 0)
{
MailInfo info = oClient.GetMailInfos()[0];
var folderPaths = emailAccountSettings.StoragePath.Split('/');
var count = 1;
// create folder path with \\
var results = folderPaths.Select(x =>
string.Join("\\", folderPaths.Take(count++))).ToList();
var maxIndex = results.Count - 1;
// taking the last element of the path
oClient.Move(info, Utils.FindFolder(results[maxIndex], oClient.Imap4Folders));
oClient.Quit();
return new MoveEmailResponse("OK", "Email moved");
}
}
return new MoveEmailResponse("KO", "Mail not found");
}
catch (Exception exception)
{
return new MoveEmailResponse("KO", "WebException: " + exception.Message);
}
}
The second implementation works fine but is less elegant. We're looping all headers of all mails whereas the previous code was using a feature of EAGetMail:
public MoveEmailResponse MoveEmail(EmailAccountSettings emailAccountSettings)
{
try
{
MailServer oServer = Utils.SetMailServer(emailAccountSettings);
MailClient oClient = new MailClient("TryIt");
oServer.SSLConnection = emailAccountSettings.Authentication.Ssl;
oServer.Port = emailAccountSettings.Authentication.ServerPort;
oClient.Connect(oServer);
Imap4Folder storageRootFolder = Utils.FindFolder(emailAccountSettings.ExchangeRootFolder, oClient.GetFolders());
if (storageRootFolder == null)
{
throw new Exception("Folder not found!");
}
else
{
oClient.SelectFolder(storageRootFolder);
// step 1: create folders in inbox folder
if (emailAccountSettings.StoragePath != null)
{
// split full path
Utils.CreateExchangeStorageFolders(oClient, emailAccountSettings.StoragePath);
}
MailInfo[] infos = oClient.GetMailInfos();
for (int i = 0; i < infos.Length; i++)
{
MailInfo info = infos[i];
Mail oMail = oClient.GetMail(info);
foreach (var headerItem in oMail.Headers.ToList())
{
if (headerItem.HeaderValue.Contains(emailAccountSettings.EmailId) ||
headerItem.DecodedValue.Contains(emailAccountSettings.EmailId))
{
if (emailAccountSettings.StoragePath != null)
{
var folderPaths = emailAccountSettings.StoragePath.Split('/');
var count = 1;
var results = folderPaths.Select(x =>
string.Join("\\", folderPaths.Take(count++))).ToList();
var maxIndex = results.Count - 1;
oClient.Move(info, Utils.FindFolder(results[maxIndex], oClient.Imap4Folders));
Console.WriteLine("Email moved");
return new MoveEmailResponse("OK", "Email moved");
}
return new MoveEmailResponse("KO", "Mail not found");
}
}
oClient.Quit();
}
}
Console.WriteLine("Completed");
return new MoveEmailResponse("KO", "Mail not found");
}
catch (Exception exception)
{
return new MoveEmailResponse("KO", "WebException: " + exception.Message);
}
}
Thanks