如果我需要阅读Office 365电子邮件项目,然后在同一电子邮件链上响应,我该采用什么逻辑



graphapi要求:

  1. 阅读电子邮件。
  2. 回复同一电子邮件链

问题语句:
我无法在同一电子邮件链上发布答复。当我这样做时,发送了一封新电子邮件。但是我的要求是在现有的电子邮件链上发送答复。

输出就像:

嗨,团队

此电子邮件已从Graphapi Code

发送

谢谢

abhijeet

关联的代码:

-- -- -- Main Method-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
static void Main(string[] args)
{
    Console.Write("App started n");
    ReadingEmail.AppClientCall();
}

-- -- -- ReadingEmail.cs Class file-- -- -- -- -- -- -- -- -
class ReadingEmail {
    public static GraphServiceClient client;
    public static string _draftId = null;
    public static void AppClientCall() {
        client = GraphApiAuthenticationClient.GetAuthenticatedClientForApp();
        String email = "xyz@.onmicrosoft.com";
        var messages = client.Users[email].MailFolders.Inbox.Messages.Request().Select(e => new {
            e.Subject,
            e.IsRead,
            e.Id
        }).GetAsync();
    if (messages.Result.Count == 0) {
        Console.WriteLine("no messages in mailbox");
    }
    foreach(Message message in messages.Result) {
        if (message.Subject.Contains("190 Report") && message.IsRead == false) {
            Console.WriteLine("Found 190 Report named :" + message.Subject);
            message.IsRead = true;
            clsReplyAll.CreateReplyAll(message.Id); // Api Call too create draft                    
        }
            Console.WriteLine(message.Subject);
        }
        Console.ReadLine();
    }
}

-- -- -- clsReplyAll.cs class file-- -- -- -- -- -- -- -
class clsReplyAll
{
    public static GraphServiceClient client;
    public static async void CreateReplyAll(string p)
    {
        client = GraphApiAuthenticationClient.GetAuthenticatedClientForApp();
        String email = "xyz@.onmicrosoft.com";
        await client.Users[email].MailFolders.Inbox.Messages[p].CreateReplyAll().Request().PostAsync();
        Console.WriteLine("Inbox Id: n" + p);
        clsUpdateDraftedEmail.Update(p); // For api call to update Draft
    }
}

-- -- -- clsUpdateDraftedEmail.cs-- -- -- -- -- -- -- --
class clsUpdateDraftedEmail {
    public static GraphServiceClient client;
    public static async void Update(string _inboxEmailid)
    {
        client = GraphApiAuthenticationClient.GetAuthenticatedClientForApp();
        String email = "xyz@.onmicrosoft.com";
        Message _msg = PrepareEmail();
        var did = await client.Users[email].MailFolders.Drafts.Messages.Request()
            .Select(e => new {e.Id}).GetAsync()
        ;
        var _getDraftFileID = did.CurrentPage.First().Id;
        Console.WriteLine("InboxId: n" + _inboxEmailid + "n");
        Console.WriteLine("Draft id: n" + _getDraftFileID);
        CallGraphApiToUpdateTheDraftMessageWithBody(_getDraftFileID, _msg); // Call api to add body
    }
    private static Message PrepareEmail()
    {
        Message _msg = new Message {
            Body = new ItemBody {
                ContentType = BodyType.Html,
                Content = "Hi Team,<br> <br>This Email is send from GraphAPI Code </br> <br>Thanks</br> <br>Abhijeet</br>"
            },
            InferenceClassification = InferenceClassificationType.Other
        };
        return _msg;
    }
    private static async void CallGraphApiToUpdateTheDraftMessageWithBody(string _getDraftFileID, Microsoft.Graph.Message _msg)
    {
        client = GraphApiAuthenticationClient.GetAuthenticatedClientForApp();
        String email = "xyz@.onmicrosoft.com";
        await client.Users[email].MailFolders.Drafts.Messages[_getDraftFileID].Request().UpdateAsync(_msg);
        SendEmails.SendTheFileReply(_getDraftFileID);
    }
}

-- -- -- SendEmails.cs-- -- -- -- -- -- -- -- -- -
class SendEmails {
    public static GraphServiceClient client;
    public static async void SendTheFileReply(string _getDraftFileID)
    {
        client = GraphApiAuthenticationClient.GetAuthenticatedClientForApp();
        String email = "xyz@.onmicrosoft.com";
        await client.Users[email].MailFolders.Drafts.Messages[_getDraftFileID].Send().Request().PostAsync();
        Console.WriteLine("Mail Send");
    }
}

您创建一个回复(使用CreateReplyAll(,但切勿使用它!您的PrepareEmail函数只是创建一个新消息,该消息根本无法连接到您要回复的消息。

您应该从CreateReplyAll返回Message对象并改用它。类似:

var reply = await client.Users[email]
    .MailFolders
    .Inbox
    .Messages[p]
    .CreateReplyAll()
    .Request().PostAsync();

最新更新