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

发布于 2024-12-03 14:17:27 字数 1479 浏览 1 评论 0原文

我想要获取我的 ASP.NET 应用程序中具有特定抄送收件人的所有电子邮件。为了将其用于将来的电子邮件,我不想一直轮询来获取它们。但我找不到一种方法,如何使用推送来立即获取电子邮件。他们有任何 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...
    }

I want to get all emails in my ASP.NET application that have a certain CC-recipient. To use this for future emails I didn't want to polling all the time to get them. But I can't find a way, how I can use push to get the emails instantly. Are their any frameworks in C# to help me for this?

I want to connect with my application to a mail server and register a method 'X'. Always when a new message arrived to the mail server, my application have to be notified and my application should execute the method 'X'.

I hope that this is possible with code like this:

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

EDIT:

I did it with the MailSystem.Net. It works very fine and is very easy to implement.

Sample Code:

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...
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

空宴 2024-12-10 14:17:28

你从错误的角度来看待这个问题。

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

IMAP4 IDLE 扩展是大多数人所说的推送邮件 - 因此您需要找到一个库对于支持 IMAP4 IDLE 的 C#。我找到了一些信息,可以让您朝着正确的方向前进(没有理由在此处重复):

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

请记住,您的邮件服务器还需要启用 IMAP4 和 IMAP4 IDLE。有些邮件服务器不支持它,因此您可能会运气不佳(并且必须使用 POP3 拉取)。

You are approaching this from the wrong angle.

SMTP does not support receiving mail (never mind PUSH mail). POP3 is what you can use for retrieving mail, but it does not have support for PUSH either (so you would have to pull for mail).

The IMAP4 IDLE extension is what most refer to as PUSH mail - so you will need to find a library for C# that supports IMAP4 IDLE. I found some information that will get you going in the right direction (no reason to duplicate it here):

Keep in mind when choosing a solution that it needs to support IDLE.
I really like the look of MailSystem.Net as it fulfills your requirements.

Remember that your mail server also needs to have IMAP4 and IMAP4 IDLE enabled. Some mail servers don't support it, so you might be clean out of luck (and will have to use POP3 pulling).

初见你 2024-12-10 14:17:28

您可以将电子邮件的副本(即使用 PostFix 中的 /etc/aliases 文件)发送到您可以处理的邮件服务器。一旦到达那里,您就可以实现一个邮件处理器,只要满足某些条件的邮件到达,它就可以执行您想要的任何操作。

希望有帮助,

You could send a copy of your emails(i.e. using /etc/aliases file in PostFix) to a MAIL SERVER YOU CAN HANDLE. Once there, you can implement a MAIL PROCESSOR that do whatever you want anytime a mail that MEET CERTAIN CONDITIONS arrives.

Hope that helps,

猫瑾少女 2024-12-10 14:17:28

你可以试试这个:

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",
                "[email protected]", "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);
            }
        }
    }
}

You can try this:

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",
                "[email protected]", "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);
            }
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文