none
How to read the latest reply message in the mail retrieve from gmail by using IMAP in C#? RRS feed

  • Question

  • How can I separate an email from its previous answer using IMAP and C#? I only want the latest reply

    For example:

    • Mail 1: Customer sends an email with the text: "Hello"
    • Mail 2: I answer the email with text figures: "Yes?"
    • Mail 3: The customer replies: "What are you doing?"

    But when I collect these 3 mails with the phrase, GetBodyAsText I get the entire conversation from every mail - containing 1,2 and 3.

    I only want mail 3 content : How can this be done? 

    Wednesday, August 7, 2019 12:51 PM

Answers

  • Assuming both sides are using normal quoting procedures, the body of message #3 does actually include all the previous messages.  The body of the message might look like:

        On Sat, Aug 3, RaviJunnuri wrote:
        > On Fri, Aug 2, customer@joe.com wrote:
        >> Hello
        >
        > Yes?

        What are you doing?

    All of that literally is the body of message #3.  Now, if you want to strip out the quoting, you can eliminate the lines that start with ">".

    Outlook uses a somewhat different standard, where the new text is on top, and the quoted message follows a horizontal line.  That's a bit trickier to parse.  And if one side uses the Outlook rule while another side uses the ">" rule, it gets almost impossible.


    Tim Roberts | Driver MVP Emeritus | Providenza & Boekelheide, Inc.

    Wednesday, August 7, 2019 5:27 PM
  • Hi RaviJunnur,

    Thank you for posting here.

    Based on your description, you want to get the latest reply message in the mail by using IMAP.

    First, you could install-Package AE.Net.Mail.

    Then, please try the following code.

                ImapClient ic = new ImapClient("imap.gmail.com", "useremail", "password",
                    AuthMethods.Login, 993, true);
                ic.SelectMailbox("INBOX");
                int maxmium = ic.GetMessageCount();
                Console.WriteLine(maxmium);
                var list = ic.GetMessages(0, maxmium).ToList();
                var list1 = new List<MailMessage>();
                foreach (var item in list)
                {
                    if(item.From.Address=="another's email")
                    {
                        list1.Add(item);
                    }
                }
                string m = list[list.Count - 1].Subject;
                Console.WriteLine(m);
                ic.Dispose();

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, August 9, 2019 5:08 AM
    Moderator

All replies

  • Assuming both sides are using normal quoting procedures, the body of message #3 does actually include all the previous messages.  The body of the message might look like:

        On Sat, Aug 3, RaviJunnuri wrote:
        > On Fri, Aug 2, customer@joe.com wrote:
        >> Hello
        >
        > Yes?

        What are you doing?

    All of that literally is the body of message #3.  Now, if you want to strip out the quoting, you can eliminate the lines that start with ">".

    Outlook uses a somewhat different standard, where the new text is on top, and the quoted message follows a horizontal line.  That's a bit trickier to parse.  And if one side uses the Outlook rule while another side uses the ">" rule, it gets almost impossible.


    Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

    Wednesday, August 7, 2019 5:27 PM
  • Hi RaviJunnur,

    Thank you for posting here.

    Based on your description, you want to get the latest reply message in the mail by using IMAP.

    First, you could install-Package AE.Net.Mail.

    Then, please try the following code.

                ImapClient ic = new ImapClient("imap.gmail.com", "useremail", "password",
                    AuthMethods.Login, 993, true);
                ic.SelectMailbox("INBOX");
                int maxmium = ic.GetMessageCount();
                Console.WriteLine(maxmium);
                var list = ic.GetMessages(0, maxmium).ToList();
                var list1 = new List<MailMessage>();
                foreach (var item in list)
                {
                    if(item.From.Address=="another's email")
                    {
                        list1.Add(item);
                    }
                }
                string m = list[list.Count - 1].Subject;
                Console.WriteLine(m);
                ic.Dispose();

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, August 9, 2019 5:08 AM
    Moderator
  • Hi

    Is your problem solved? If so, please post "Mark as answer" to the appropriate answer. So that it will help other members to find the solution quickly if they face the similar issue.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, August 21, 2019 8:06 AM
    Moderator