通知C#的客户端,在SMTP服务器收到新的电子邮件客户端、电子邮件、服务器、通知

2023-09-03 09:18:57 作者:苦痛不由衷

我想在我的ASP.NET应用程序,它有一定的CC-收件人的所有邮件。要使用此为将来的邮件我不想轮询所有的时间,让他们。但我不能找到一种方法,我怎么可以用推,以获得电子邮件瞬间。是他们在C#中的任何框架,帮助我呢?

我希望跟我的应用程序到邮件服务器和注册方法X。总是当新邮件到达邮件服务器,我的申请已经得到通知,我的应用程序应该执行的方法X。

我希望这是可能的code是这样的:

 无效的Application_Start()
{
    ...
    ConnectWithTheSmtpServer();
    RegisterMethodForNotification(DoSomethink);
    ...
}
无效DoSomethink(邮件NEWMAIL)
{
    //做Somethink与邮件
}
 

编辑:

我做到了与 MailSystem.Net 。它的工作原理很细,而且非常容易实现。

样品code:

 无效的Application_Start()
{
    VAR工人=新的BackgroundWorker();
    worker.DoWork + =新DoWorkEventHandler(StartIdleProcess);

    如果(worker.IsBusy)
        worker.CancelAsync();

    worker.RunWorkerAsync();
}

私人无效StartIdleProcess(对象发件人,DoWorkEventArgs E)
{
    如果(_imap = NULL和放大器;!&安培; _imap.IsConnected)
    {
        _imap.StopIdle();
        _imap.Disconnect();
    }

        _imap =新Imap4Client();
        _imap.ConnectSsl(服务器名,993);
        _imap.Login(用户名,passwort);

        VAR收件箱= _imap.SelectMailbox(收件箱);

        _imap.NewMessageReceived + =新NewMessageReceivedEventHandler(NewMessageReceived);

        inbox.Subscribe();

        _imap.StartIdle();
    }

    公共静态无效NewMessageReceived(对象源,NewMessageReceivedEventArgs E)
    {
        //做一些与源...
    }
 

解决方案 outlook设置126邮箱,发送测试电子邮件失败

您是从错误的角度接近这一点。

SMTP不支持接收邮件(别介意邮件推送)。 POP3是一种可用于检索信函,但它没有对PUSH支持,无论是(这样你就必须对其进行轮询邮件)。

借助 IMAP4 IDLE 扩展是大多数称之为推送邮件 - 所以你需要找到C#库支持IMAP4 IDLE。我发现了一些信息,这将让你在正确的方向前进(没有理由在这里重复):

Using C#.NET库来检查IMAP邮件 访问IMAP在C#

当选择,它需要支持空闲解决记住。我真的很喜欢 MailSystem.Net 的外观,因为它满足您的要求。

请记住,您的邮件服务器也需要启用IMAP4和IMAP4 IDLE。有些邮件服务器不支持它,所以你可能是干净的运气了(而且必须使用POP3轮询)。

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

解决方案

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 poll 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):

Using C# .Net Libraries to Check for Imap Messages Accessing IMAP in C#

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 polling).