Getting mail from a mailbox

ELMA can work with email. For example, this article describes sending an email.

This article provides an example of receiving an email with the Aspose.Email library.

In this example, attachments will be created by creating a file from a stream.

Use the following assemblies:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
 
using Aspose.Email.Imap;
using Aspose.Email.Pop3;
using EleWise.ELMA.Files;
using EleWise.ELMA.Logging;
using EleWise.ELMA.Model.Common;
using EleWise.ELMA.Model.Entities;
using EleWise.ELMA.Model.Entities.ProcessContext;
using EleWise.ELMA.Model.Managers;
using EleWise.ELMA.Model.Services;
using EleWise.ELMA.Model.Types.Settings;
using EleWise.ELMA.Runtime.Managers;
using EleWise.ELMA.Services;
 

In this example, a connection is established via the IMAP protocol. For this, you need to specify a server address, mailbox, password and connection port. In this example, these data are stored in the corresponding context variables. After receiving emails, a block will be created with:

  • the email subject;
  • the email sender;
  • the email body;
  • the email attachments.

Context variables of the String type: context.Server, context.Address, context.Password.

P_Process_Emails - is the class name of the block that is filled in with emails.

Block properties:
      Sender - String type
      Subject - String type
      Body - Text type
      Attachments - Attachment (Object), Many-to-many link.

The block class name (P_Process_Emails) consists of the process name and the block name (P_Process - process class name, Emails - block name).

The function CreateBinaryFile is described in this article. You need to insert it into the process scripts with a separate method (as described in the article).

//attempt to connect
//create a connection to the server 143 - this is the connection port
ImapClient client = new ImapClient(context.Server, 143, context.Address, context.Password);
try
{
     
    //select the required folder, the default folder Incoming is selected this way:
    client.SelectFolder(ImapFolderInfo.InBox);
    //if you need to select, for example, the ELMA folder, then
    //client.SelectFolder("ELMA");
    //get the list of emails
    var mailList = client.ListMessages();
    foreach (var mail in mailList)
    {
        //if the email is unread
        if (!mail.IsRead)
        {
            //get the email number
            var mailNum = mail.SequenceNumber;
            //load the email by number
            var message = client.FetchMessage(mailNum);
            //create a block item, based on the email
            var newElement = InterfaceActivator.Create<P_Process_Emails>();
            newElement.Sender = message.From.Address;
            newElement.Subject = message.Subject;
            newElement.Body = message.Body;
            //if there are attachments, add them
            if (message.Attachments.Any())
            {
                for (int i = 0; i < message.Attachments.Count; i++)
                {
                    string attName = message.Attachments[i].Name;
                    //create an ELMA attachment
                    var newAtt = InterfaceActivator.Create<EleWise.ELMA.Common.Models.Attachment>();
                    //load the attchment by the email number and the attachment name
                    var mailAtt = client.FetchAttachment(mailNum, attName);
                    //create a file from a stream
                    newAtt.File = CreateBinaryFile(mailAtt.ContentStream, attName);
                    //and fill in the standard properties of the attachment
                    newAtt.CreationAuthor = context.WorkflowInstance.Initiator;
                    newAtt.CreationDate = DateTime.Now;
                    newAtt.Save();
                    newElement.Attachments.Add(newAtt);
                }
            }
            newElement.Save();
            context.Emails.Add(newElement);
 
        }
    }
    //disconnect from the server
    client.Dispose();
} catch (Exception ex)
{
    //client.Disconnect();
    Logger.Log.Error(ex.Message);
}
 

 

Connecting to and working with other protocols is very similar to the one described above. E.g., using POP3:

 
Note
The code below applies only to ELMA 3.10 and lower

 

Pop3Client client = new Pop3Client(context.Server, 143, context.Address, context.Password);
      try
      {
        var mailList = client.ListMessages();
        foreach(var mail in mailList)
        {
          var mailNum = mail.SequenceNumber;
          var message = client.FetchMessage(mailNum);
          var newElement = InterfaceActivator.Create<P_PolucheniePochty_Emails>();
          context.Emails.Add(newElement);
          newElement.Sender = message.Sender.Address;
          newElement.Subject = message.Subject;
          newElement.Body = message.Body;
          if(message.Attachments.Any())
          {
            for (int i = 0; i < message.Attachments.Count; i++)
            {
              string attName = message.Attachments[i].Name;
              var newAtt = InterfaceActivator.Create<EleWise.ELMA.Common.Models.Attachment>();
              newAtt.File = CreateBinaryFile(message.Attachments[i].ContentStream, attName);
              newAtt.CreationAuthor = context.WorkflowInstance.Initiator;
              newAtt.CreationDate = DateTime.Now;
              newAtt.Save();
              newElement.Attachments.Add(newAtt);
            }
          }
           
        }
        client.Disconnect();
      }
      catch(Exception ex)
      {
        client.Disconnect();
        Logger.Log.Error(ex.Message);
      }