当SMTP服务器收到新的电子邮件

当SMTP服务器收到新的电子邮件

本文介绍了通知C#客户端,当SMTP服务器收到新的电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的ASP.NET应用程序中收到所有具有某个CC收件人的电子邮件。为了将来的电子邮件,我不想轮询所有的时间来获得他们。但是我找不到方法,我如何可以使用push来即时获取电子邮件。 C#中的任何框架是否帮助我?



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



我希望这是可能的代码如下:

  void Application_Start()
{
...
ConnectWithTheSmtpServer );
RegisterMethodForNotification(DoSomethink);
...
}
void DoSomethink(Mail newMail)
{
//执行邮件
}

编辑:



。它的工作非常好,很容易实现。



示例代码:

  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)
{
//用源代码...
}


解决方案

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

SMTP不支持接收邮件(不介意PUSH邮件)。 POP3是您可以用于检索邮件的,但它也不支持PUSH(所以你必须拉邮件)。



扩展名是大多数引用为PUSH邮件 - 所以您将需要找到一个支持C#的库IMAP4 IDLE。我发现一些信息可以让你走正确的方向(没有理由在这里复制):








选择需要支持IDLE的解决方案时,请记住。
我非常喜欢,因为它符合您的要求。



请记住,您的邮件服务器还需要启用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 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).

这篇关于通知C#客户端,当SMTP服务器收到新的电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 01:38