コード例 #1
0
        private async Task OnMessageReceived(object sender, MessageEventArgs e)
        {
            using (Stream stream = e.Message.GetData().Result)
            {
                string to = string.Join(", ", e.Message.Recipients);
                Console.WriteLine($"Message received. Client address {e.Message.Session.ClientAddress}. From {e.Message.From}. To {to}.");
                Message message = new MessageConverter().ConvertAsync(stream, e.Message.From, to).Result;
                message.IsUnread = true;

                await QueueTask(() =>
                {
                    Console.WriteLine("Processing received message");
                    Smtp4devDbContext dbContext = dbContextFactory();

                    Session dbSession = dbContext.Sessions.Find(activeSessionsToDbId[e.Message.Session]);
                    message.Session   = dbSession;
                    dbContext.Messages.Add(message);

                    dbContext.SaveChanges();

                    Smtp4devServer.TrimMessages(dbContext, serverOptions.Value);
                    dbContext.SaveChanges();
                    messagesHub.OnMessagesChanged().Wait();
                    Console.WriteLine("Processing received message DONE");
                }, false).ConfigureAwait(false);
            }
        }
コード例 #2
0
        private async Task OnSessionCompleted(object sender, SessionEventArgs e)
        {
            int messageCount = (await e.Session.GetMessages()).Count;

            Console.WriteLine($"Session completed. Client address {e.Session.ClientAddress}. Number of messages {messageCount}.");

            await QueueTask(async() =>
            {
                Smtp4devDbContext dbContent = dbContextFactory();

                Session dbSession = dbContent.Sessions.Find(sessionToDbId[e.Session]);
                await UpdateDbSession(e.Session, dbSession);
                await dbContent.SaveChangesAsync();

                Smtp4devServer.TrimSessions(dbContent, serverOptions.Value);
                await dbContent.SaveChangesAsync();

                await sessionsHub.OnSessionsChanged();
            }, false);
        }