gpt4 book ai didi

c# - 如何在 c# 中使用 pop3 读取从 gmail 检索的邮件中的邮件正文?

转载 作者:行者123 更新时间:2023-11-30 14:34:56 27 4
gpt4 key购买 nike

这是代码:

protected void Button9_Click(object sender, EventArgs e)
{
try
{
// create an instance of TcpClient
TcpClient tcpclient = new TcpClient();

// HOST NAME POP SERVER and gmail uses port number 995 for POP
tcpclient.Connect("pop.gmail.com", 995);

// This is Secure Stream // opened the connection between client and POP Server
System.Net.Security.SslStream sslstream = new SslStream(tcpclient.GetStream());

// authenticate as client
sslstream.AuthenticateAsClient("pop.gmail.com");

//bool flag = sslstream.IsAuthenticated; // check flag

// Asssigned the writer to stream
System.IO.StreamWriter sw = new StreamWriter(sslstream);

// Assigned reader to stream
System.IO.StreamReader reader = new StreamReader(sslstream);

// refer POP rfc command, there very few around 6-9 command
sw.WriteLine("USER your_gmail_user_name@gmail.com");

// sent to server
sw.Flush(); sw.WriteLine("PASS your_gmail_password");

sw.Flush();

// RETR 1 will retrive your first email. it will read content of your first email
sw.WriteLine("RETR 1");
sw.Flush();

// close the connection
sw.WriteLine("Quit ");
sw.Flush(); string str = string.Empty;

string strTemp = string.Empty;
while ((strTemp = reader.ReadLine()) != null)
{
// find the . character in line
if (strTemp == ".")
{
break;
}
if (strTemp.IndexOf("-ERR") != -1)
{
break;
}
str += strTemp;
}

textbox1.text = str;
textbox1.text += "<BR>" + "Congratulation.. ....!!! You read your first gmail email ";
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}

邮件正文是一堆看似随机的字符。我知道这不仅仅是一堆随机字符,而是一些需要解析和转换的代码。如何阅读“消息正文”中的内容?

最佳答案

我知道我不会直接回复你的回答,但阅读电子邮件是一项非常复杂的任务,我认为你可以使用外部库更好更快地实现这一点,而不是自己实现。

有很多好的实现,我通常使用 OpenPop.NET,它运行良好并且是开源的。

https://sourceforge.net/projects/hpop/

你可以在网上找到很多例子,因为它真的很流行。

http://hpop.sourceforge.net/examples.php

您可以轻松获取所有邮件:

using(Pop3Client client = new Pop3Client())
{
// Connect to the server
client.Connect("pop.gmail.com", 995, true);

// Authenticate ourselves towards the server
client.Authenticate("username@gmail.com", "password", AuthenticationMethod.UsernameAndPassword);

// Get the number of messages in the inbox
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));
}
}

你可以获得完整的原始消息

var mailbody = ASCIIEncoding.ASCII.GetString(message.RawMessage);

或者如果它是 utf8 编码的电子邮件:

var encodedStringAsBytes = System.Convert.FromBase64String(message.RawMessage); 
var rawMessage =System.Text.Encoding.UTF8.GetString(encodedStringAsBytes);

相反,如果您只需要邮件正文,则必须深入研究邮件结构:

http://hpop.sourceforge.net/documentation/OpenPop~OpenPop.Mime.MessagePart.html

我知道这不是一件容易的事,但正如我上面所说的,电子邮件是复杂的对象。

关于c# - 如何在 c# 中使用 pop3 读取从 gmail 检索的邮件中的邮件正文?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13119653/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com