Exemplo n.º 1
0
        public void QueueMessageNonBlocking(SerializableMailMessage mailMessage)
        {
            if (mailMessage == null)
            {
                return;
            }

            Coordinator.SharedInstance.AddMail(mailMessage);
        }
Exemplo n.º 2
0
        public void QueueMessageWithMailSettings(SerializableMailMessage mailMessage, IMailServerSettings mailSettings)
        {
            if (mailMessage == null)
            {
                return;
            }

            mailMessage.MailSettings = mailSettings;

            Coordinator.SharedInstance.AddMail(mailMessage);
        }
Exemplo n.º 3
0
        public void QueueMessageWithSmtpSettingsNonBlocking(SerializableMailMessage mailMessage, string smtpServer, int port, bool ssl, bool authenticate, string username, string password)
        {
            if (mailMessage == null)
            {
                return;
            }

            QueueMessageWithMailSettingsNonBlocking(mailMessage, new SmtpMailServerSettings
            {
                Host                   = smtpServer,
                Port                   = port,
                RequiresSsl            = ssl,
                RequiresAuthentication = authenticate,
                Username               = username,
                Password               = password
            });
        }
Exemplo n.º 4
0
        public static bool WriteMailToFile(SerializableMailMessage message, string path)
        {
            try
            {
                var serializer = new XmlSerializer(typeof(SerializableMailMessage));

                using (var stream = new FileStream(path, FileMode.Create, FileAccess.ReadWrite, FileShare.None))
                    using (var streamWriter = new StreamWriter(stream, Encoding.UTF8))
                    {
                        serializer.Serialize(streamWriter, message);
                    }

                return(true);
            }
            catch
            {
                return(false);
            }
        }
Exemplo n.º 5
0
        public void AddMail(SerializableMailMessage message)
        {
            string tempPath = Files.CreateEmptyTempFile();

            if (WriteMailToFile(message, tempPath))
            {
                string queuePath = Properties.Settings.Default.QueueFolder;
                try { queuePath = Files.MapPath(queuePath); }
                catch { }

                bool   success = false;
                string file    = Path.Combine(queuePath, DateTime.Now.ToString(@"yyyyMMddHHmmss") + @"_" + mailIdCounter.ToString().PadLeft(8, '0') + @".mail");
                while (File.Exists(file))
                {
                    mailIdCounter++;
                    file = Path.Combine(queuePath, DateTime.Now.ToString(@"yyyyMMddHHmmss") + @"_" + mailIdCounter.ToString().PadLeft(8, '0') + @".mail");

                    var originalSendingState = _sendingFileNames.ContainsKey(file);

                    try
                    {
                        _sendingFileNames[file] = true;
                        File.Move(tempPath, file);
                        success = true;
                        break;
                    }
                    catch (DirectoryNotFoundException)
                    {
                        break;
                    }
                    catch (PathTooLongException)
                    {
                        break;
                    }
                    catch (FileNotFoundException)
                    {
                        break;
                    }
                    catch (IOException)
                    {
                        continue;
                    }
                    finally
                    {
                        if (!originalSendingState)
                        {
                            _sendingFileNames.TryRemove(file, out originalSendingState);
                        }
                    }
                }

                if (!success)
                {
                    var originalSendingState = _sendingFileNames.ContainsKey(file);

                    try
                    {
                        _sendingFileNames[file] = true;
                        File.Move(tempPath, file);
                        success = true;
                    }
                    catch
                    {
                    }
                    finally
                    {
                        if (!originalSendingState)
                        {
                            _sendingFileNames.TryRemove(file, out originalSendingState);
                        }
                    }
                }

                if (success)
                {
                    SharedInstance.ContinueSendingEmails();
                }
            }
        }
Exemplo n.º 6
0
        private async Task SendMailAsync(string fileName)
        {
            bool workerInUse = false;

            try
            {
                if (ConsoleLogEnabled)
                {
                    Console.WriteLine("Reading mail from " + fileName);
                }

                SerializableMailMessage message = null;

                try
                {
                    message = ReadMailFromFile(fileName);
                }
                catch (FileNotFoundException)
                {
                    MarkSkipped(fileName);
                    return;
                }

                Interlocked.Increment(ref _concurrentWorkers);
                workerInUse = true;

                if (ConsoleLogEnabled)
                {
                    Console.WriteLine("Sending " + fileName + " task to worker");
                }

                var mailSettings = message.MailSettings;
                if (mailSettings == null || mailSettings.IsEmpty)
                {
                    mailSettings = SettingsController.GetMailSettings();
                }

                var success = await SenderFactory.SendMailAsync(message, mailSettings);

                if (!success)
                {
                    if (ConsoleLogEnabled)
                    {
                        Console.WriteLine("No mail server name, skipping " + fileName);
                    }

                    MarkSkipped(fileName);
                }
                else
                {
                    if (ConsoleLogEnabled)
                    {
                        Console.WriteLine("Sent mail for " + fileName);
                    }

                    MarkSent(fileName);
                }

                if (ConsoleLogEnabled)
                {
                    Console.WriteLine("Releasing worker from " + fileName + " task");
                }

                // Task ended, decrement counter and pulse to the Coordinator thread
                Interlocked.Decrement(ref _concurrentWorkers);
                workerInUse = false;

                lock (_actionMonitor)
                {
                    Monitor.Pulse(_actionMonitor);
                }
            }
            catch (Exception ex)
            {
                if (ConsoleLogEnabled)
                {
                    if (ConsoleLogExceptions)
                    {
                        Console.WriteLine("Exception thrown for " + fileName + ":\n    " +
                                          ex.Message.ToString().Replace("\n", "\n    "));
                    }

                    Console.WriteLine("Task failed for " + fileName);
                }

                if (Properties.Settings.Default.LogErrorsToOs)
                {
                    using (var eventLog = new EventLog("Application"))
                    {
                        eventLog.Source = "Application";
                        eventLog.WriteEntry(
                            "Failed to send mail from queue.\n\n" + ex.Message + "\n\n" + ex.StackTrace,
                            EventLogEntryType.Warning);
                    }
                }

                if (workerInUse)
                {
                    // Decrement counter and pulse to the Coordinator thread
                    Interlocked.Decrement(ref _concurrentWorkers);
                }

                try { MarkFailed(fileName); }
                catch { }

                lock (_actionMonitor)
                {
                    Monitor.Pulse(_actionMonitor);
                }
            }
        }