-4

我想使用 c# 从电子邮件中挑选出所需的文本?你能帮我整理一下吗?

我需要来自以下示例电子邮件格式的必需文本:

city,
xxxx@hotmail.com
privileged customer.

这是示例:

我收到了一封来自 xxxx@gmail.com 的电子邮件

内容如下:

Hi xxxx,


here is the some of the lists,

Title:CITY
Email:xxxx@hotmail.com
Package:<b>Privileged customer</b>

谢谢,

问候,xxxx。

4

3 回答 3

2

这是示例代码。将解析出电子邮件内容。

using System.Text.RegularExpressions

namespace ConsoleApplication1
{
  class Program
  {
    static void Main(string[] args)
    {
      string content = "Hi:Mr. Title:Sample Email:default123_11@gmail.com";

      // Here we call Regex.Match.
      Match match = Regex.Match(content, @"Email:([a-zA-Z0-9@._]*)",
            RegexOptions.IgnoreCase);

      // Here we check the Match instance.
      if (match.Success)
      {
        // Finally, we get the Group value and display it.
        string key = match.Groups[1].Value;
        Console.WriteLine(key);
      }
      Console.ReadKey();
    }
  }
}
于 2013-09-11T14:49:46.120 回答
1

您可以在一定程度上使用正则表达式。这个表达式 ,.*:(.*)将返回所有键值对。但是,您需要在事后获取匹配项并删除诸如<b></b>标签之类的内容。

var matches = Regex.Match(input, ".*:(.*)");

这是一个正则表达式 101来证明它。

于 2013-09-11T14:41:30.690 回答
0
                      static void Main(string[] args)
    {

        var client = new Pop3Client();
        client.Connect("pop.gmail.com", 995, true);
        client.Authenticate("xxxxx@gmail.com", "yourpassword");

        var count = client.GetMessageCount();
        Message message = client.GetMessage(count);
        int messageCount = client.GetMessageCount();

        // We want to download all messages
        List<Message> allMessages = new List<Message>(messageCount);

        // Messages are numbered in the interval: [1, messageCount]
        // Ergo: message numbers are 1-based.
        // Most servers give the latest message the highest number
        for (int i = messageCount; i > 0; i--)
        {
            allMessages.Add(client.GetMessage(i));
        }
        foreach (Message item in allMessages)
        {
            MessagePart mpart = item.FindFirstPlainTextVersion();
            string s = Encoding.UTF8.GetString(mpart.Body, 0, mpart.Body.Length);

            if (s.Contains("Name") && s.Contains("Email") && s.Contains("<b>"))
            {
                List<string> newstrlist=new List<string>();
                string[] strarry = new string[1000];
                strarry=  s.Split('\n');
               foreach (string strar  in strarry)
               {
                   if (strar.Contains("@"))
                   {
                       newstrlist.Add(strar.Remove(0, 6).Trim('<', '>', '/', 'b', ',', '.', '-', ':', ';', '!', '^', '(', ')', '_', '+', '\n', '\r', '\t', '\b',' '));//email ,name,subscription
                   }
                   if (strar.Contains("Name"))
                   {
                       newstrlist.Add(strar.Remove(0, 5).Trim('<', '>', '/', 'b', ',', '.', '-', ':', ';', '!', '^', '(', ')', '_', '+', '\n', '\r', '\t',' ', '\b'));
                   }
                   if (strar.Contains("<b>"))
                   {
                       newstrlist.Add(strar.Trim().Remove(0, 4).Trim('<', '>', '/', 'b', ',', '.', '-', ':', ';', '!', '^', '(', ')', '_', '+', '\n', '\r', '\t', '\b', ' '));
                   }
                 //  Console.WriteLine(strar);

               }
               foreach (string str in newstrlist)
               {
                   Console.WriteLine(str);
               }
            }

        }
        Console.ReadKey();
    }
于 2013-09-11T16:40:53.547 回答