0

I'm trying to parse the last unread message from gmail, but for some reason my code fails while the formatting of the message is a bit different.

When I manually send the message to the email, this code works. In this case, the mail looks like that: Picture

The code itself looks like that:

        public static int GetMail()
    {

        //OTP READ FROM GMAIL
        var client = new Pop3Client();
        client.Connect("pop.gmail.com", 995, true);
        client.Authenticate("MAIL@gmail.com", "PASSWORD");
        var count = client.GetMessageCount();
        Message messages = client.GetMessage(count);
        MessagePart plainTextPart = messages.FindFirstPlainTextVersion();
        string message = plainTextPart.GetBodyAsText();
        string resultString = Regex.Match(message, @"\d+").Value;
        int Mail = Int32.Parse(resultString);
        client.DeleteAllMessages();
        client.Disconnect(); 

        return Mail;

    }

But when I install an app on my cellphone (which automatically forwards new messages to your email), the application change the message formatting and email message looks like that: Picture 2

As it seems, when the email message formatting changes, it doesn't work anymore. Could you guys please give me suggestion of how to modify the code so it works with the formatting given in Picture 2.

Shako
  • 31
  • 9
  • When you say "last" do you mean "latest received" or "oldest unread"? Also how does your question relate to selenium? – ProgrammingLlama Oct 04 '19 at 08:43
  • Possible Duplicate of [Read Latest Email Using Pop3 C#](https://stackoverflow.com/questions/40153786/how-to-read-latest-email-using-pop3-c-sharp) – HeadJ.E.M. Oct 04 '19 at 08:47
  • @John I mean latest unread message. The code is integrated with selenium and I'm working in that library. – Shako Oct 04 '19 at 08:50
  • Oh! Is Pop3Client a Selenium library? – ProgrammingLlama Oct 04 '19 at 08:52
  • No, I mean I work in Selenium library and I use the code for in that library – Shako Oct 04 '19 at 08:54
  • Okay so i'm not really familiar with the selenium library But the formatting should not be a problem id suggest 2 possible solutions Mining the info as is from plaintext If(message.Contains("message")) { Mine it out via c# } or get the page as html (since it looks to be html) and use the value attribute on Message – HeadJ.E.M. Oct 04 '19 at 08:57
  • can you edit your answer and paste both examples not as images but as text ? also try and get HTML Versions and not just textversions so we can assist you more – HeadJ.E.M. Oct 04 '19 at 08:58

1 Answers1

0

There is a problem with your regex. This line:

string resultString = Regex.Match(message, @"\d+").Value;

The "\d+" matches one or more digits. In Picture2 there are several results for this regex: the 6 digits you want to match and parts of the date.

Try changing the regex to "\d{6}":

string resultString = Regex.Match(message, @"\d{6}").Value;

This assumes the match you want is always 6 digits.

You can read up on C# regex here: https://learn.microsoft.com/en-us/dotnet/standard/base-types/regular-expression-language-quick-reference And test your regex here: https://regex101.com/

chbjerre
  • 1
  • 1
  • I just read up on Regex.Match. It should match the first occurence of one or more digits. Perhaps parts of the censored text includes digits and it is matching that? – chbjerre Oct 04 '19 at 09:05
  • Hello and thank you for your contribution. The problem occurs on the string message line, the code shows exception with definition `$exception {"Object reference not set to an instance of an object."} System.NullReferenceException".` The code is with the line: `string message = plainTextPart.GetBodyAsText();` – Shako Oct 04 '19 at 10:31
  • Oh okay. That should probably be part of your problem description. That would suggest "plainTextPart" is null. Have you tried to debug each case, perhaps you will be able to see what goes wrong. – chbjerre Oct 04 '19 at 11:58