Impossible to link replied mail to conversation in mailbox

Copper Contributor

Hi,

 

I'm trying to reply/replyAll with my .Net app to external mail sent to our generic email address.

The mail is correctly sent without error, but when we check the recipient mailbox, it has not linked to the rest of the conversation.

However, the conversation ID is the same as the email sent ID.

 

Someone can help me? 

 

Thank you

 

public async Task<string?> SendMailAsync(SendMailDto mail, CancellationToken cancellationToken = default)
    {
        if (mail == null)
            throw new ArgumentNullException(nameof(mail));
        await EnsureAccountId();
        var message = MapToMessage(mail);

        Message messageToSend;

        if (!string.IsNullOrEmpty(mail.ExternalMailId) && (mail.IsReply || mail.IsReplyAll))
        {
            var replyMessageBuilder = _graphClient.Users[AccountId]
                                                  .Messages[mail.ExternalMailId];

            if (mail.IsReply)
                messageToSend = await replyMessageBuilder.CreateReply(message)
                                                         .Request()
                                                         .PostAsync(cancellationToken);
            else
                messageToSend = await replyMessageBuilder.CreateReplyAll(message)
                                                         .Request()
                                                         .PostAsync(cancellationToken);
        }
        else
        {
            messageToSend = await _graphClient.Users[AccountId]
                                              .Messages.Request()
                                              .AddAsync(message, cancellationToken);
        }

        if (mail.Attachments.Length > 0)
            await mail.Attachments.ExecuteAsync(async (a) =>
                await CreateUploadSession(messageToSend.Id, a, cancellationToken));

        await _graphClient.Users[AccountId]
                          .Messages[messageToSend.Id]
                          .Send()
                          .Request()
                          .PostAsync(cancellationToken);

        var conversationionIdRes = await _graphClient.Users[AccountId]
                                                     .Messages.Request()
                                                     .Filter(
                                                         $"internetMessageId eq '{messageToSend.InternetMessageId}'")
                                                     .Select(m => m.ConversationId)
                                                     .GetAsync(cancellationToken);

        _logger.LogInformation("Mail {@Mail} sent successfully", mail);

        return conversationionIdRes.ElementAtOrDefault(0)
                                   ?.ConversationId;
    }

    private async Task CreateUploadSession(string messageId, SendMailAttachment attachment,
                                           CancellationToken cancellationToken = default)
    {
        var attachmentItem = new AttachmentItem
        {
            AttachmentType = AttachmentType.File,
            Name = attachment.FileName,
            Size = attachment.FileContent.LongLength,
        };

        //initiate the upload session for large files (note that this example doesn't handle multiple attachment).
        var uploadSession = await _graphClient.Users[AccountId]
                                              .Messages[messageId]
                                              .Attachments.CreateUploadSession(attachmentItem)
                                              .Request()
                                              .PostAsync(cancellationToken);

        var maxChunkSize = 1024 * 320;
        using var stream = new MemoryStream(attachment.FileContent);

        stream.Position = 0;
        var largeFileUploadTask = new LargeFileUploadTask<FileAttachment>(uploadSession, stream, maxChunkSize);

        await largeFileUploadTask.UploadAsync();
    }

private Message MapToMessage(SendMailDto mail)
    {
        var message = new Message
        {
            From = new Recipient {EmailAddress = new EmailAddress {Address = _settings.NotificationMailAddress}},
            Subject = mail.Subject,
            Body =
                new ItemBody
                {
                    ContentType = mail.Body.ContentType == "html" ? BodyType.Html : BodyType.Text,
                    Content = mail.Body.Content
                },
            ToRecipients =
                mail.ToRecipient.Select(r => new Recipient {EmailAddress = new EmailAddress {Address = r.Email}}),
            CcRecipients =
                mail.CcRecipient.Select(r => new Recipient {EmailAddress = new EmailAddress {Address = r.Email}}),
            BccRecipients =
                mail.BccRecipient.Select(r => new Recipient {EmailAddress = new EmailAddress {Address = r.Email}}),
            Attachments = new MessageAttachmentsCollectionPage()
        };

        return message;
    }

 

0 Replies