The task is as follows:

  1. Get all emails from the mailer (from the Inbox folder of a specific account).
  2. Find the right letter and get a hyperlink from it.

So, I managed to get emails using the MailKit library, but I can’t get a hyperlink from the desired email, as I can do it using the MailKit library. Here is a piece of code with which I get all the necessary emails

public void GetMessages() { SetUpConnection(); var inbox = client.Inbox; inbox.Open(FolderAccess.ReadOnly); var folders = client.GetFolder(client.PersonalNamespaces[0]); System.Diagnostics.Debug.WriteLine("Folder test {0}", folders.Count); foreach(var folder in folders.GetSubfolders()) { foreach(var subFolder in folder.GetSubfolders()) { if (subFolder.Name.Equals("Вся почта") || subFolder.Name.Equals("All Mail")) { subFolder.Open(FolderAccess.ReadOnly); for (int i = subFolder.Count - 1; i > subFolder.Count - 20; i--) { System.Diagnostics.Debug.WriteLine("Subject: " + subFolder.GetMessage(i).Subject); System.Diagnostics.Debug.WriteLine("TextBody: " + subFolder.GetMessage(i).TextBody); } } } } } 

    1 answer 1

    Since you have the body of the email, that is, the text ... Then you can use it to find all the links in the text with a regular expression.

    I will naguglit such here:

     ^((http[s]?|ftp):\/)?\/?([^:\/\s]+)((\/\w+)*\/)([\w\-\.]+[^#?\s]+)(.*)?(#[\w\-]+)?$ 

    It is dumb, but it will bring together any kind of links, http, https, ftp, etc.

    It will be something like this:

     Regex regex = new Regex(@"^((http[s]?|ftp):\/)?\/?([^:\/\s]+)((\/\w+)*\/)([\w\-\.]+[^#?\s]+)(.*)?(#[\w\-]+)?$"); Match match = regex.Match(emailBody); 

    PS: By what features to distinguish the necessary letter from the unnecessary ones, I will not tell you because it is not an oracle)

    • I had a slightly different link in my view, I need to get a link that is embedded in the link that comes to the email. I read something that seems to be able to parse it in Html text, and then somehow get the attribute value, but I could not find a working example. - Main Star
    • Well, then the path or the same one I gave above (just hidml the text), or you download the htmlAgrilityPack, you load the html there and look for the node <a> with the href attribute and pull the link. - Andrew
    • So how do I get this html text? - Main Star
    • @MainStar Think logically. You have the subFolder.GetMessage(i).TextBody , what does the TextBody do here and what should be put in place of it? - EvgeniyZ
    • With mailKit did not work. But google is talking about hiding the HtmlBody. And also this code here var body = message.BodyParts.OfType ().FirstOrDefault (x => x.ContentType.Matches ("text", "html")); . But it’s better to use it if you find it: D - Andrew