0

我想实现与此相同的功能:

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

这工作正常。该方法“NewMessageReceived”在每次新邮件接收时成功调用。

但问题是我无法获取邮件。在这种方法中,我只得到了 INBOX 的总数。

此代码使用库:MailSystem.net

我的代码如下:

        Imap4Client _imap;
        public static int cnt = 0;
        protected void Application_Start(object sender, EventArgs e)
        {
            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(ConfigurationManager.AppSettings["IncomingServerName"], Convert.ToInt32(ConfigurationManager.AppSettings["InPort"]));
            _imap.Login(ConfigurationManager.AppSettings["EmailAddress"], ConfigurationManager.AppSettings["Password"]);

            var inbox = _imap.SelectMailbox("INBOX");

            _imap.NewMessageReceived += new NewMessageReceivedEventHandler(NewMessageReceived);

            inbox.Subscribe();

            _imap.StartIdle();
        }

        public static void NewMessageReceived(object source, NewMessageReceivedEventArgs e)
        {
            cnt = e.MessageCount; //Inbox Count
            //here i want to fetch all unread mail from inbox
        }

我找到了以下获取未读邮件的解决方案:

http://mailsystem.codeplex.com/discussions/651129

它说我使用MailKit库。由于我已经在使用MailSystem.net库,有没有什么方法可以通过只使用一个库来满足我的要求?

我也准备好使用任何其他库。

请给我建议。谢谢

4

1 回答 1

2

是的,通过单个库获得它:MailKit

查看 global.asax 文件的以下代码:

        static CancellationTokenSource _done;
        ImapClient _imap;
        protected void Application_Start(object sender, EventArgs e)
        {
            var worker = new BackgroundWorker();
            worker.DoWork += new DoWorkEventHandler(StartIdleProcess);

            if (worker.IsBusy)
                worker.CancelAsync();

            worker.RunWorkerAsync();
        }

        private void StartIdleProcess(object sender, DoWorkEventArgs e)
        {
            _imap = new ImapClient();

            _imap.Connect(ConfigurationManager.AppSettings["IncomingServerName"], Convert.ToInt32(ConfigurationManager.AppSettings["InPort"]), Convert.ToBoolean(ConfigurationManager.AppSettings["IncomingIsSSL"]));
            _imap.AuthenticationMechanisms.Remove("XOAUTH");
            _imap.Authenticate(ConfigurationManager.AppSettings["EmailAddress"], ConfigurationManager.AppSettings["Password"]);

            _imap.Inbox.Open(FolderAccess.ReadWrite);
            _imap.Inbox.MessagesArrived += Inbox_MessagesArrived;
            _done = new CancellationTokenSource();
            _imap.Idle(_done.Token);
        }
        static void Inbox_MessagesArrived(object sender, EventArgs e)
        {
            var folder = (ImapFolder)sender;
            //_done.Cancel(); // Stop idle process
            using (var client = new ImapClient())
            {
                client.Connect(ConfigurationManager.AppSettings["IncomingServerName"], Convert.ToInt32(ConfigurationManager.AppSettings["InPort"]), Convert.ToBoolean(ConfigurationManager.AppSettings["IncomingIsSSL"]));

                // disable OAuth2 authentication unless you are actually using an access_token
                client.AuthenticationMechanisms.Remove("XOAUTH2");

                client.Authenticate(ConfigurationManager.AppSettings["EmailAddress"], ConfigurationManager.AppSettings["Password"]);

                int tmpcnt = 0;
                client.Inbox.Open(FolderAccess.ReadWrite);
                foreach (var uid in client.Inbox.Search(SearchQuery.NotSeen))
                {
                    try
                    {
                        var message = client.Inbox.GetMessage(uid);
                        client.Inbox.SetFlags(uid, MessageFlags.Seen, true);

                        List<byte[]> listAttachment = new List<byte[]>();

                        if (message.Attachments.Count() > 0)
                        {
                            foreach (var objAttach in message.Attachments)
                            {
                                using (MemoryStream ms = new MemoryStream())
                                {
                                    ((MimeKit.ContentObject)(((MimeKit.MimePart)(objAttach)).ContentObject)).Stream.CopyTo(ms);
                                    byte[] objByte = ms.ToArray();
                                    listAttachment.Add(objByte);
                                }
                            }
                        }

                        string subject = message.Subject;
                        string text = message.TextBody;
                        var hubContext = GlobalHost.ConnectionManager.GetHubContext<myHub>();
                        hubContext.Clients.All.modify("fromMail", text);
                        tmpcnt++;
                    }
                    catch (Exception)
                    { }
                }
                client.Disconnect(true);
            }
        }

现在,我能够接收新邮件到达事件,并且还能够在该事件中获取未读邮件。感谢 MailKit。

于 2016-05-11T05:37:26.260 回答