当SMTP服务器收到新电子邮件时,通知C#客户端

我希望在我的ASP.NET应用程序中获取具有特定CC收件人的所有电子邮件。 要将此用于将来的电子邮件,我不想一直轮询来获取它们。 但是我找不到办法,如何使用push来即时获取电子邮件。 他们在C#中的任何框架都可以帮助我吗?

我想将我的应用程序连接到邮件服务器并注册方法“X”。 总是当新邮件到达邮件服务器时,我的应用程序必须得到通知,我的应用程序应该执行方法’X’。

我希望这可以用这样的代码:

void Application_Start() { ... ConnectWithTheSmtpServer(); RegisterMethodForNotification(DoSomethink); ... } void DoSomethink(Mail newMail) { // Do Somethink with the mail } 

编辑:

我用MailSystem.Net做到了 。 它工作得很好,很容易实现。

示例代码:

 void Application_Start() { var worker = new BackgroundWorker(); worker.DoWork += new DoWorkEventHandler(StartIdleProcess); if (worker.IsBusy) worker.CancelAsync(); worker.RunWorkerAsync(); } private void StartIdleProcess(object sender, DoWorkEventArgs e) { if (_imap != null && _imap.IsConnected) { _imap.StopIdle(); _imap.Disconnect(); } _imap = new Imap4Client(); _imap.ConnectSsl(server-name, 993); _imap.Login(username, passwort); var inbox = _imap.SelectMailbox("INBOX"); _imap.NewMessageReceived += new NewMessageReceivedEventHandler(NewMessageReceived); inbox.Subscribe(); _imap.StartIdle(); } public static void NewMessageReceived(object source, NewMessageReceivedEventArgs e) { // Do something with the source... } 

你是从错误的角度接近这个。

SMTP不支持接收邮件(更不用说PUSH邮件)了。 POP3是您可以用来检索邮件的,但它也不支持PUSH(因此您必须提取邮件)。

IMAP4 IDLE扩展名大多数称为PUSH邮件 – 因此您需要找到支持IMAP4 IDLE的C#库。 我找到了一些可以让你朝着正确的方向前进的信息(没有理由在这里复制它):

  • 使用C#.Net库检查IMAP消息
  • 在C#中访问IMAP

选择需要支持IDLE的解决方案时请记住。 我非常喜欢MailSystem.Net的外观,因为它满足您的要求。

请记住,您的邮件服务器还需要启用IMAP4和IMAP4 IDLE。 有些邮件服务器不支持它,所以你可能运气不好(并且必须使用POP3拉动)。

您可以将电子邮件的副本(即在PostFix中使用/ etc / aliases文件)发送到您可以处理的邮件服务器。 到那里之后,您可以实现一个MAIL PROCESSOR,无论何时收到满足条件的邮件,都可以随心所欲地执行任何操作。

希望有所帮助,

你可以试试这个:

 using System; using System.Collections.Generic; using System.Text; using System.IO; using EAGetMail; //add EAGetMail namespace namespace receiveemail { class Program { static void Main(string[] args) { // Create a folder named "inbox" under current directory // to save the email retrie enter code here ved. string curpath = Directory.GetCurrentDirectory(); string mailbox = String.Format("{0}\\inbox", curpath); // If the folder is not existed, create it. if (!Directory.Exists(mailbox)) { Directory.CreateDirectory(mailbox); } // Gmail IMAP4 server is "imap.gmail.com" MailServer oServer = new MailServer("imap.gmail.com", "gmailid@gmail.com", "yourpassword", ServerProtocol.Imap4 ); MailClient oClient = new MailClient("TryIt"); // Set SSL connection, oServer.SSLConnection = true; // Set 993 IMAP4 port oServer.Port = 993; try { oClient.Connect(oServer); MailInfo[] infos = oClient.GetMailInfos(); for (int i = 0; i < infos.Length; i++) { MailInfo info = infos[i]; Console.WriteLine("Index: {0}; Size: {1}; UIDL: {2}", info.Index, info.Size, info.UIDL); // Download email from GMail IMAP4 server Mail oMail = oClient.GetMail(info); Console.WriteLine("From: {0}", oMail.From.ToString()); Console.WriteLine("Subject: {0}\r\n", oMail.Subject); // Generate an email file name based on date time. System.DateTime d = System.DateTime.Now; System.Globalization.CultureInfo cur = new System.Globalization.CultureInfo("en-US"); string sdate = d.ToString("yyyyMMddHHmmss", cur); string fileName = String.Format("{0}\\{1}{2}{3}.eml", mailbox, sdate, d.Millisecond.ToString("d3"), i); // Save email to local disk oMail.SaveAs(fileName, true); // Mark email as deleted in GMail account. oClient.Delete(info); } // Quit and purge emails marked as deleted from Gmail IMAP4 server. oClient.Quit(); } catch (Exception ep) { Console.WriteLine(ep.Message); } } } }