IGraphClient CopyMessage Failing

Dear Team,

I need to copy some messages to Outlook. I am using latest Aspose.Email v24.9 & Aspose.Email.Clients.Graph.IGraphClient CopyMessage function. It takes two parameters. New ParentId, ItemId. My query is i have other object type MailMessage. MailMessage object.ItemId value is null. So how do i copy my MailMessage to Outloook using GraphClient CopyMessage. Kindly help.

Constants.clientTarget.CopyMessage(graphFolderInfo.ItemId, mailMessage.ItemId);

Its not working because mailMessage.ItemId value is null.

Regards
XCoder

@perfectdata.techno

To copy a MailMessage to Outlook using the IGraphClient.CopyMessage method, you need to ensure that the ItemId of the MailMessage is not null. The CopyMessage method requires both a new parent folder ID and the ID of the message you want to copy.

Since the ItemId of your MailMessage is null, you will first need to create the message in the target folder and obtain its ItemId. Here’s how you can do it:

  1. Create the Message: Use the IGraphClient to create a new message in the desired folder. This will return the ItemId of the newly created message.

  2. Copy the Message: Once you have the ItemId, you can then use the CopyMessage method to copy it to the desired location.

Here’s a sample code snippet to illustrate this process:

// Assuming you have an instance of IGraphClient named graphClient
// and a MailMessage object named mailMessage

// Step 1: Create the message in the target folder
var createdMessage = graphClient.Messages.Create(graphFolderInfo.ItemId, mailMessage);

// Step 2: Now you can copy the created message
Constants.clientTarget.CopyMessage(graphFolderInfo.ItemId, createdMessage.ItemId);

Make sure to replace graphFolderInfo.ItemId with the actual ID of the folder where you want to create the message. After creating the message, you can then copy it as needed.

If you have any further questions or need additional assistance, feel free to ask!

When i applied the same code changes. I am getting following exception at Step 1:

“Server error Status: InvalidInternetMessageHeaderCollection
Description: Maximum number of headers in one message should be less than or equal to 5.
Details:”

try
{
//Step 1
var createdMessage = Constants.clientTarget.CreateMessage(graphFolderInfo.ItemId, mailMessage);

// Step 2: Now you can copy the created message Constants.clientTarget.CopyMessage(graphFolderInfo.ItemId, createdMessage.ItemId.UniqueId);
}
catch(Exception e)
{
}

Regards
XCoder

Hello @perfectdata.techno,

Please clarify your case. Where are the source messages you are trying to copy located? Are they on the server or locally?

If they are on the server and you need to copy them to another folder, you should first list the MessageInfo objects in the source folder using the ListMessages method. Then, for each MessageInfo, you use the ItemId property to copy it to another folder using the CopyMessage method.

If the source messages are located locally, you should use the CreateMessage method, which takes the destination folder’s ID as the first parameter and the MailMessage object as the second.

As for the error you’re encountering: InvalidInternetMessageHeaderCollection suggests that the message you’re trying to upload contains too many headers or invalid headers, and the server rejects it because it exceeds the limit of allowed headers in a message.
Ensure that the MailMessage object does not have more than 5 headers. You can inspect the headers and remove or reduce them if they exceed the limit.

→ I am copying mail messages from Office 365 to Outlook. Office 365 is using EWS services & Outlook.com is using IGraphClient services. objImapClient object is of type IEwsClient.

-Office 365 code

objImagPageInfo = objImapClient.ListMessagesByPage(strfullPath, query, iItemsPerPage);
objImapMessageInfoCollection = objImagPageInfo.Items;
 foreach (ExchangeMessageInfo objImapMessageInfo in objImapMessageInfoCollection)
  {
      mailMessage = objImapClient.FetchMessage(objImapMessageInfo.UniqueUri);
Outlook code
    {
    //Step 1
    var createdMessage = Constants.clientTarget.CreateMessage(graphFolderInfo.ItemId, mailMessage);
    
    // Step 2: Now you can copy the created message 
   Constants.clientTarget.CopyMessage(graphFolderInfo.ItemId, createdMessage.ItemId.UniqueId);
    }
    catch(Exception e)
    {
    }
 }

When i applied the same code changes. I am getting following exception at Step 1:

“Server error Status: InvalidInternetMessageHeaderCollection
Description: Maximum number of headers in one message should be less than or equal to 5.
Details:”

XCoder

@perfectdata.techno,

As I said above,

InvalidInternetMessageHeaderCollection suggests that the message you’re trying to upload contains too many headers or invalid headers, and the server rejects it because it exceeds the limit of allowed headers in a message. Ensure that the MailMessage object does not have more than 5 headers. You can inspect the headers and remove or reduce them if they exceed the limit.

Or, why don’t you use EwsClient to append messages to Outlook too? Use EwsClient.AppendMessage(string folderUri, MailMessage message) to append messages.

I have checked the same suggested changes in the code. But it didn’t worked. Plz find my code below :slight_smile:

// Create a new message
MapiMessage mm = new MapiMessage();
mm.Subject = "EMAILNET-39318 " + Guid.NewGuid().ToString();
mm.Body = “EMAILNET-39318 REST API v1.0 - Create Message”;
mm.SetProperty(KnownPropertyList.DisplayTo, “to@host.com”);
mm.SetProperty(KnownPropertyList.SenderName, “from”);
mm.SetProperty(KnownPropertyList.SentRepresentingEmailAddress, “from@host.com”);

// Step 1: Create the message in the target folder
IGraphClient graphClient = null;
var createdMessage = Constants.clientTarget.CreateMessage(graphFolderInfo.ItemId, mm); //mailMessage

// Step 2: Now you can copy the created message
Constants.clientTarget.CopyMessage(graphFolderInfo.ItemId, createdMessage.ItemId);

This is the output in Outlook. Two messages are saved that too as draft.
SavingScreenshot.jpg (168.4 KB)

→ EWSClient we were using earlier. As Microsoft has stopped EWS services. Now we are switching to ClientGraph.

Regards
XCode

@perfectdata.techno ,

Thank you for the provided information. We’ll check it and contact you.