C# 如何使用c在电子邮件中选择所需的文本#
我想用c#从电子邮件中选择所需的文本?你能帮我整理一下吗 我需要以下示例电子邮件格式中的所需文本:C# 如何使用c在电子邮件中选择所需的文本#,c#,asp.net,C#,Asp.net,我想用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&l
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>
Hi xxxx,
下面是一些列表的列表,
标题:城市
电邮:xxxx@hotmail.com
套餐:特权客户
谢谢,
问候,,
xxxxx.您可以在一定程度上使用正则表达式。此表达式,
*:(*)
,将返回所有键值对。但是,您需要在事后取出匹配项并去掉
和
标记
var matches = Regex.Match(input, ".*:(.*)");
下面是一个例子来证明这一点。这是示例代码。将解析出电子邮件内容
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();
}
}
}
static void Main(字符串[]args)
{
var client=new Pop3Client();
client.Connect(“pop.gmail.com”,995,true);
客户端身份验证(“xxxxx@gmail.com“,“您的密码”);
var count=client.GetMessageCount();
Message Message=client.GetMessage(计数);
int messageCount=client.GetMessageCount();
//我们想下载所有消息
List allMessages=新列表(messageCount);
//消息在间隔[1,messageCount]中编号
//因此:消息编号是以1为基础的。
//大多数服务器提供的最新消息数量最多
对于(int i=messageCount;i>0;i--)
{
allMessages.Add(client.GetMessage(i));
}
foreach(所有消息中的消息项)
{
MessagePart mpart=item.FindFirstPlainTextVersion();
字符串s=Encoding.UTF8.GetString(mpart.Body,0,mpart.Body.Length);
如果(s.Contains(“名称”)&&s.Contains(“电子邮件”)&&s.Contains(“”)
{
List newstrlist=新列表();
字符串[]strarry=新字符串[1000];
strarry=s.Split('\n');
foreach(strarry中的字符串strar)
{
if(strar.Contains(“@”))
{
newstrlist.Add(strar.Remove(0,6).Trim(“,”/”,“b”,“,“,”,“,”,“-”,“:”,“;”,“!”,“^',“(”,“,”),“,”,“+”,“\n',“\r',“\t',“\b',”);//电子邮件,姓名,订阅
}
如果(战略包含(“名称”))
{
newstrlist.Add(strar.Remove(0,5).Trim(“,”/”,“b',“,”,“,”,“-”,“:”,“;”,“!”,“^',“(”,“),“,”,“+”,“\n',“\r',“\t',“,”,“\b”);
}
if(strar.Contains(“”)
{
newstrlist.Add(strar.Trim().Remove(0,4).Trim(“”,‘,‘,’,‘,’,‘,‘,’,‘,’,‘-’,‘:‘,‘,’;‘,’!’,‘^’,‘(‘,’),‘,‘,‘+’,‘\n’,‘\r’,‘\t’,‘\b’,‘));
}
//控制台写入线(strar);
}
foreach(newstrlist中的字符串str)
{
控制台写入线(str);
}
}
}
Console.ReadKey();
}
“不要比这更多的东西”你很谦虚。这些信息是什么格式的?它只是代码中的一个字符串吗?您尝试过如何解析该字符串?为了进一步参考,尝试“挑选所需文本,不再需要任何内容”通常被认为是“解析”。更具体地说,查找正则表达式和模式匹配。在.NET中有一个整洁的类,名为Regex
,它可以以各种方式使用,其中一些会对您有很大帮助。
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();
}