Exemplo n.º 1
0
        private static void CheckIfMailsHaveToBeSend()
        {
            logger.Info("check if mails have to be sent.");

            MongoServer   mongoServer   = MyMongoDB.GetServer();
            MongoDatabase mongoDatabase = mongoServer.GetDatabase("email");
            MongoCollection <LazyBsonDocument> mongoCollection = mongoDatabase.GetCollection <LazyBsonDocument>("users");

            MongoCursor <LazyBsonDocument> mongoCursor = mongoCollection.FindAll();

            foreach (LazyBsonDocument bsonDocument in mongoCursor)
            {
                if (Options.Verbose)
                {
                    Console.WriteLine("checking user-id: " + bsonDocument["_id"].ToString() + "; username: "******"Username"].ToString());
                }

                try {
                    MongoDatabase mongoUserDatabase = mongoServer.GetDatabase("email_user_" + bsonDocument["_id"].ToString());
                    MongoCollection <LazyBsonDocument> mongoUserCollection = mongoUserDatabase.GetCollection <LazyBsonDocument>("mails");
                    MongoCursor <LazyBsonDocument>     mongoUserCursor     = mongoUserCollection.Find(Query.EQ("Folder", "SENT"));
                    foreach (LazyBsonDocument bsonUserDocument in mongoUserCursor)
                    {
                        Console.WriteLine("found email in SENT-Folder: " + bsonUserDocument["_id"].ToString());
                        eMail userMail = new eMail(bsonUserDocument);
                        userMail.Send();
                        break;
                    }
                } catch (Exception ex) {
                    logger.ErrorException(ex.Message, ex);
                }
            }
        }
Exemplo n.º 2
0
        private static void Check()
        {
            Console.WriteLine("running precheck...");

            MongoServer mongoServer = MyMongoDB.GetServer();

            MongoDatabase mongoDatabase = mongoServer.GetDatabase("email");
            MongoCollection <LazyBsonDocument> mongoCollection = mongoDatabase.GetCollection <LazyBsonDocument>("mails");

            MongoCursor <LazyBsonDocument> mongoCursor = mongoCollection.FindAll();

            foreach (LazyBsonDocument bsonDocument in mongoCursor)
            {
                if (Options.Verbose)
                {
                    Console.WriteLine("checking email-id: " + bsonDocument["_id"].ToString());
                }
                try {
                    if (User.EMailExists(bsonDocument["RecipientTo"].AsString))
                    {
                        Console.WriteLine("user with email-address found: " + bsonDocument["RecipientTo"].AsString);
                        User newMailUser = new User();
                        newMailUser.RefreshById(User.GetIdByEMail(bsonDocument["RecipientTo"].AsString));
                        eMail mail = new eMail(bsonDocument);
                        mail.AssignToUser(newMailUser);
                    }
                } catch (Exception ex) {
                    logger.ErrorException(ex.Message, ex);
                }
            }

            Console.WriteLine("precheck finished...");
        }
Exemplo n.º 3
0
        public User(string username, string password, string eMail)
        {
            this._mongoServer = MyMongoDB.GetServer();

            this._username = username;
            this._password = password;
            this._eMail    = eMail;
        }
Exemplo n.º 4
0
        public User(string username, string password, UserAuthorization authorization, UserStatus status)
        {
            this._mongoServer = MyMongoDB.GetServer();

            this._username      = username;
            this._password      = password;
            this._authorization = authorization;
            this._status        = status;
        }
Exemplo n.º 5
0
        public static bool NameExists(string name)
        {
            MongoServer   mongoServer   = MyMongoDB.GetServer();
            MongoDatabase mongoDatabase = mongoServer.GetDatabase("email");
            MongoCollection <UserEntity> mongoCollection = mongoDatabase.GetCollection <UserEntity>("users");
            IMongoQuery queryEMail = Query <UserEntity> .Where(e => e.Username == name);

            UserEntity user = mongoCollection.FindOne(queryEMail);

            if (user != null)
            {
                return(true);
            }

            return(false);
        }
Exemplo n.º 6
0
        public static string GetIdByName(string name)
        {
            MongoServer   mongoServer   = MyMongoDB.GetServer();
            MongoDatabase mongoDatabase = mongoServer.GetDatabase("email");
            MongoCollection <UserEntity> mongoCollection = mongoDatabase.GetCollection <UserEntity>("users");
            IMongoQuery queryEMail = Query <UserEntity> .Where(u => u.Username == name);

            UserEntity user = mongoCollection.FindOne(queryEMail);

            if (user != null)
            {
                return(user.Id.ToString());
            }

            return(String.Empty);
        }
Exemplo n.º 7
0
        public static string GetIdByEMail(string eMail)
        {
            MongoServer   mongoServer   = MyMongoDB.GetServer();
            MongoDatabase mongoDatabase = mongoServer.GetDatabase("email");
            MongoCollection <UserEntity> mongoCollection = mongoDatabase.GetCollection <UserEntity>("users");
            IMongoQuery queryEMail = Query <UserEntity> .Where(u => u.eMail == eMail || u.eMailAliases.Any(ua => ua.Address == eMail));

            UserEntity user = mongoCollection.FindOne(queryEMail);

            if (user != null)
            {
                return(user.Id.ToString());
            }

            return(String.Empty);
        }
Exemplo n.º 8
0
        public static bool EMailExists(string eMail)
        {
            MongoServer   mongoServer   = MyMongoDB.GetServer();
            MongoDatabase mongoDatabase = mongoServer.GetDatabase("email");
            MongoCollection <UserEntity> mongoCollection = mongoDatabase.GetCollection <UserEntity>("users");
            IMongoQuery queryEMail = Query <UserEntity> .Where(u => u.eMail == eMail || u.eMailAliases.Any(ua => ua.Address == eMail));

            UserEntity user = mongoCollection.FindOne(queryEMail);

            if (user != null)
            {
                return(true);
            }

            return(false);
        }
Exemplo n.º 9
0
        public eMail(eMailEntity entity)
        {
            this._mongoServer = MyMongoDB.GetServer();

            this.SetClientName(entity.ClientName);
            this.SetFrom(entity.MailFrom);
            this.SetHeaderFrom(entity.HeaderFrom);
            this.SetHeaderTo(entity.HeaderTo);
            this.SetId(entity.Id.ToString());
            this.SetMessage(entity.Message);
            this.SetRecipient(entity.RecipientTo);
            this.SetReplyTo(entity.HeaderReplyTo);
            this.SetSubject(entity.Subject);
            this.SetTime(entity.Time);
            this.SetFolder(entity.Folder);
            this._flags = entity.Flags;
        }
Exemplo n.º 10
0
 public User()
 {
     this._mongoServer = MyMongoDB.GetServer();
 }
Exemplo n.º 11
0
        public static void Main(string[] args)
        {
            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US");
            WaitHandle[] waitHandles = new WaitHandle[] {
                new AutoResetEvent(false),
                new AutoResetEvent(false)
            };

            Parser parser = Parser.Default;

            if (!parser.ParseArguments(args, Options))
            {
                LogManager.Configuration = null;
                return;
            }

            MongoServer mongoServer = MyMongoDB.GetServer();

            try {
                mongoServer.Connect();
            } catch (MongoConnectionException e) {
                Console.WriteLine("MongoConnectionException: " + e.Message);
                LogManager.Configuration = null;
                return;
            }

            if (Options.Check)
            {
                Check();
            }


            CheckHttpsConnection();
            CheckIfMailsHaveToBeSend();

            HttpListener httpListener       = null;
            TcpListener  smtpListener       = null;
            TcpListener  secureSmtpListener = null;
            TcpListener  imapListener       = null;
            TcpListener  secureImapListener = null;

            if (Options.ServerCertificateFilename != String.Empty)
            {
                if (File.Exists(Options.ServerCertificateFilename))
                {
                    try {
                        TcpRequestHandler.TcpRequestHandler.SetServerCertificate(Options.ServerCertificateFilename);
                    } catch (Exception ex) {
                        logger.ErrorException("Certificate Exception message: " + ex.Message, ex);
                        LogManager.Configuration = null;
                        return;
                    }
                }
                else
                {
                    logger.Error("Can't find certificate file: " + Options.ServerCertificateFilename);
                    LogManager.Configuration = null;
                    return;
                }
            }

            if (Options.ServerKeyFilename != String.Empty)
            {
                if (File.Exists(Options.ServerKeyFilename))
                {
                    try {
                        TcpRequestHandler.TcpRequestHandler.SetServerKeyFile(Options.ServerKeyFilename);
                    } catch (Exception ex) {
                        logger.ErrorException("Server Key-File Exception message: " + ex.Message, ex);
                        LogManager.Configuration = null;
                        return;
                    }
                }
                else
                {
                    logger.Error("Can't find server key file file: " + Options.ServerKeyFilename);
                    LogManager.Configuration = null;
                    return;
                }
            }

            LimitedConcurrencyLevelTaskScheduler taskScheduler = new LimitedConcurrencyLevelTaskScheduler(500);
            TaskFactory factory = new TaskFactory(taskScheduler);

            if (!Options.DisableHttpServer)
            {
                if (!HttpListener.IsSupported)
                {
                    logger.Error("HttpListener is not supported by this System!");
                    LogManager.Configuration = null;
                    return;
                }
                string[] prefixes = { "http://*:" + Options.HttpPort + "/" };
                httpListener = new HttpListener();
                foreach (string s in prefixes)
                {
                    httpListener.Prefixes.Add(s);
                }

                try {
                    httpListener.Start();
                } catch (Exception e) {
                    logger.Error("HttpListener: " + e.Message);
                    LogManager.Configuration = null;
                    return;
                }

                logger.Info("Listening on HTTP-Port " + Options.HttpPort);

                // Http-Listener Task
                factory.StartNew(() => {
                    Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US");
                    bool repeatHttpListener             = true;
                    do
                    {
                        try {
                            factory.StartNew((context) => {
                                Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US");
                                HttpRequestHandler handler          = new HttpRequestHandler((HttpListenerContext)context);
                                try {
                                    handler.ProcessRequest();
                                } catch (Exception e) {
                                    logger.ErrorException(e.Message, e);
                                    logger.Error(e.StackTrace);
                                } finally {
                                    handler.OutputResult();
                                }
                            }, (object)httpListener.GetContext(), TaskCreationOptions.PreferFairness);
                        } catch (AggregateException e) {
                            logger.Error("Es sind " + e.InnerExceptions.Count + " Fehler aufgetreten");
                            AggregateException eFlatten = e.Flatten();
                            eFlatten.Handle(exc => {
                                logger.Error(exc.Message);
                                return(true);
                            }
                                            );
                        } catch (Exception e) {
                            logger.Error("Exception aufgetreten: " + e.Message);
                        }
                    } while(repeatHttpListener);

                    ((AutoResetEvent)waitHandles[0]).Set();
                }
                                 );
            }
            else
            {
                ((AutoResetEvent)waitHandles[0]).Set();
            }

            if (!Options.DisableSmtpServer)
            {
                smtpListener       = new TcpListener(IPAddress.Any, Options.SmtpPort);
                secureSmtpListener = new TcpListener(IPAddress.Any, Options.SecureSmtpPort);
                imapListener       = new TcpListener(IPAddress.Any, Options.ImapPort);
                secureImapListener = new TcpListener(IPAddress.Any, Options.SecureImapPort);

                try {
                    smtpListener.Start();
                    secureSmtpListener.Start();
                    imapListener.Start();
                    secureImapListener.Start();
                } catch (Exception e) {
                    logger.Error("TcpListener: " + e.Message);
                    LogManager.Configuration = null;
                    return;
                }

                logger.Info("Listening on SMTP-Port " + Options.SmtpPort);
                logger.Info("Listening on Secure SMTP-Port " + Options.SecureSmtpPort);
                logger.Info("Listening on IMAP-Port " + Options.ImapPort);
                logger.Info("Listening on Secure IMAP-Port " + Options.SecureImapPort);

                Action <object> receiveRequest = (object listener) => {
                    Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US");
                    bool repeatSmtpListener = true;
                    do
                    {
                        try {
                            factory.StartNew((context) => {
                                Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US");

                                TcpClient tcpClient = (TcpClient)context;
                                IPEndPoint endPoint = (IPEndPoint)tcpClient.Client.LocalEndPoint;
                                IRequestHandler handler;
                                if (endPoint.Port != Options.ImapPort && endPoint.Port != Options.SecureImapPort)
                                {
                                    handler = new SmtpServer(tcpClient, Options.SecureSmtpPort);
                                }
                                else
                                {
                                    handler = new ImapServer(tcpClient, Options.SecureImapPort);
                                }

                                try {
                                    handler.Start();
                                    handler.WaitForClosing();
                                } catch (Exception e) {
                                    logger.ErrorException(e.Message, e);
                                    logger.Error(e.StackTrace);
                                } finally {
                                    handler.OutputResult();
                                }
                            }, (object)((TcpListener)listener).AcceptTcpClient(), TaskCreationOptions.PreferFairness);
                        } catch (AggregateException e) {
                            logger.Error("Es sind " + e.InnerExceptions.Count + " Fehler aufgetreten");
                            AggregateException eFlatten = e.Flatten();
                            eFlatten.Handle(exc => {
                                logger.Error(exc.Message);
                                return(true);
                            }
                                            );
                        } catch (Exception e) {
                            logger.Error("Exception aufgetreten: " + e.Message);
                        }
                    } while(repeatSmtpListener);

                    ((AutoResetEvent)waitHandles[1]).Set();
                };

                // Smtp-Listener Task
                factory.StartNew(receiveRequest, smtpListener);
                factory.StartNew(receiveRequest, secureSmtpListener);
                factory.StartNew(receiveRequest, imapListener);
                factory.StartNew(receiveRequest, secureImapListener);
            }
            else
            {
                ((AutoResetEvent)waitHandles[1]).Set();
            }

            WaitHandle.WaitAll(waitHandles);

            if (!Options.DisableHttpServer)
            {
                if (httpListener != null)
                {
                    httpListener.Close();
                }
            }
            if (!Options.DisableSmtpServer)
            {
                if (smtpListener != null)
                {
                    smtpListener.Stop();
                }
                if (secureSmtpListener != null)
                {
                    secureSmtpListener.Stop();
                }
                if (imapListener != null)
                {
                    imapListener.Stop();
                }
                if (secureImapListener != null)
                {
                    secureImapListener.Stop();
                }
            }

            if (Options.DisableHttpServer && Options.DisableSmtpServer)
            {
                Console.WriteLine("Invalid Parameter Combination: You have disabled the HTTP-Server and the SMTP-Server.");
            }

            LogManager.Configuration = null;
        }
Exemplo n.º 12
0
        public eMail(LazyBsonDocument bsonDocument)
        {
            this._mongoServer = MyMongoDB.GetServer();

            this.SetId(bsonDocument["_id"].AsObjectId.ToString());
            if (bsonDocument["ClientName"] != null)
            {
                this.SetClientName(bsonDocument["ClientName"].AsString);
            }
            if (bsonDocument["MailFrom"] != null)
            {
                this.SetFrom(bsonDocument["MailFrom"].AsString);
            }
            if (bsonDocument["HeaderFrom"] != null && bsonDocument["HeaderFrom"].IsBsonDocument)
            {
                BsonDocument bsonHeaderFrom = bsonDocument["HeaderFrom"].AsBsonDocument;
                if (bsonHeaderFrom["Name"] != null && bsonHeaderFrom["Address"] != null)
                {
                    this.SetHeaderFrom(new eMailAddress(bsonHeaderFrom["Name"].AsString, bsonHeaderFrom["Address"].AsString));
                }
            }
            if (bsonDocument["HeaderTo"] != null && bsonDocument["HeaderTo"].IsBsonArray)
            {
                BsonArray           bsonHeaderTo = bsonDocument["HeaderTo"].AsBsonArray;
                List <eMailAddress> headerTo     = new List <eMailAddress>();
                foreach (var currentBsonHeaderTo in bsonHeaderTo)
                {
                    if (currentBsonHeaderTo.IsBsonDocument && currentBsonHeaderTo["Name"] != null && currentBsonHeaderTo["Address"] != null)
                    {
                        headerTo.Add(new eMailAddress(currentBsonHeaderTo["Name"].AsString, currentBsonHeaderTo["Address"].AsString));
                    }
                }
                this.SetHeaderTo(headerTo);
            }
            if (bsonDocument["Message"] != null)
            {
                this.SetMessage(bsonDocument["Message"].AsString);
            }
            if (bsonDocument["RecipientTo"] != null)
            {
                this.SetRecipient(bsonDocument["RecipientTo"].AsString);
            }
            if (bsonDocument["HeaderReplyTo"] != null && !bsonDocument["HeaderReplyTo"].IsBsonNull)
            {
                BsonDocument bsonHeaderReplyTo = bsonDocument["HeaderReplyTo"].AsBsonDocument;
                if (bsonHeaderReplyTo["Name"] != null && bsonHeaderReplyTo["Address"] != null)
                {
                    this.SetReplyTo(bsonHeaderReplyTo["Name"].AsString, bsonHeaderReplyTo["Address"].AsString);
                }
            }
            if (bsonDocument["Subject"] != null)
            {
                this.SetSubject(bsonDocument["Subject"].AsString);
            }
            if (bsonDocument["Folder"] != null)
            {
                this.SetFolder(bsonDocument["Folder"].AsString);
            }
            if (bsonDocument["Time"] != null)
            {
                this.SetTime((DateTime)bsonDocument["Time"].AsBsonDateTime);
            }
            if (bsonDocument["Flags"] != null)
            {
                //this._flags = bsonDocument["Flags"].AsBsonArray;
            }
        }
Exemplo n.º 13
0
 public eMail()
 {
     this._mongoServer = MyMongoDB.GetServer();
 }