我看到很多有关如何使用 a 发送电子邮件的示例,但我希望运行一个操作来检查电子邮件帐户。
有谁知道这是否可以做到(我确信可以)并给我指出一些例子?
最佳答案
您可以通过多种方式获取 Gmail 收件箱。
OpenPop
如果您确实只想使用 POP,并且不介意使用外部库,这看起来是最好/最简单的方法。OpenPop 允许您访问安全/不安全的电子邮件帐户并让您选择端口。请参阅this post开始吧。
OpenPop is an open source C#.NET code bundle that implements mail fetching and parsing. As of this writing, it only uses Microsoft .NET framework libraries to do the required. But for accessing secure pop servers, openPop can be extended by using some SSL library.
例如,要通过 Pop 访问 Gmail:
POPClient poppy = new POPClient();
poppy.Connect("pop.gmail.com", 995, true);
poppy.Authenticate(<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6e1b1d0b1c000f030b2e09030f0702400d0103" rel="noreferrer noopener nofollow">[email protected]</a>, "password");
int Count = poppy.GetMessageCount();
if (Count > 0)
{
for (int i = Count; i >= 1; i -= 1)
{
OpenPOP.MIMEParser.Message m = poppy.GetMessage(i, false);
//use the parsed mail in variable 'm'
}
}
TcpClient POP3:
要通过 Pop3 从任何提供商检索电子邮件,您可以使用 TcpClient。对于 Gmail,情况略有不同,因为 Gmail 使用 SSL 和 POP 端口 995。有一个例子here :
// 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");
Gmail Atom Feed:
第一种方法是使用 GmailAtomFeed
,它是 C# .Net Gmail Tools 的一部分。 。该网站说:
The GmailAtomFeed class provides a simple object layer for programmatic access to gmails atom feed. In just a couple lines of code the feed will be retreived from gmail and parsed. After that the entries can be accessed through an object layer AtomFeedEntryCollection, plus access to the raw feed and the feeds XmlDocument is also available.
这是如何使用它的示例:
// Create the object and get the feed
RC.Gmail.GmailAtomFeed gmailFeed = new RC.Gmail.GmailAtomFeed("username", "password");
gmailFeed.GetFeed();
// Access the feeds XmlDocument
XmlDocument myXml = gmailFeed.FeedXml
// Access the raw feed as a string
string feedString = gmailFeed.RawFeed
// Access the feed through the object
string feedTitle = gmailFeed.Title;
string feedTagline = gmailFeed.Message;
DateTime feedModified = gmailFeed.Modified;
//Get the entries
for(int i = 0; i < gmailFeed.FeedEntries.Count; i++) {
entryAuthorName = gmailFeed.FeedEntries[i].FromName;
entryAuthorEmail = gmailFeed.FeedEntries[i].FromEmail;
entryTitle = gmailFeed.FeedEntries[i].Subject;
entrySummary = gmailFeed.FeedEntries[i].Summary;
entryIssuedDate = gmailFeed.FeedEntries[i].Received;
entryId = gmailFeed.FeedEntries[i].Id;
}
IMAP
如果您不限于 POP,另一种方法是使用 IMAP。使用 IMAP,您可以连接到 SSL 服务器并选择一个端口:
using (Imap imap = new Imap())
{
imap.ConnectSSL("imap.gmail.com", 993);
imap.Login("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="81e0efe6e4eddef8c1e2eeecf1e0eff8afe2eeec" rel="noreferrer noopener nofollow">[email protected]</a>", "xyx***"); // MailID As Username and Password
imap.SelectInbox();
List<long> uids = imap.SearchFlag(Flag.Unseen);
foreach (long uid in uids)
{
string eml = imap.GetMessageByUID(uid);
IMail message = new MailBuilder()
.CreateFromEml(eml);
Console.WriteLine(message.Subject);
Console.WriteLine(message.TextDataString);
}
imap.Close(true);
}
关于c# - netduino检查电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14064416/