Esempio n. 1
0
        public static void Run(
            [QueueTrigger(queueName: Fn.Qs.OutgoingMail, Connection = Fn.ConnectionKeyName)] CloudQueueMessage message,
            ILogger log)
        {
            using (var client = new SmtpClient())
            {
                var storageConfig     = Fn.StorageConfig.Load();
                var storageAccount    = CloudStorageAccount.Parse(storageConfig.ConnectionString);
                var blobClient        = storageAccount.CreateCloudBlobClient();
                var outgoingContainer = blobClient.GetContainerReference(storageConfig.OutgoingMailContainerName);
                var emailConfig       = Fn.EmailConfig.Load();
                var telemetry         = new TelemetryClient(new TelemetryConfiguration(Fn.InstrumentationKey()));

                var email = EmailStorage.LoadEmailFromStorage(message.AsString, outgoingContainer);
                client.Connect(emailConfig.SmtpServer, emailConfig.SmtpPort);
                client.Authenticate(emailConfig.Username, emailConfig.Password);
                client.Send(email);
                client.Disconnect(true);
                telemetry.TrackEvent(
                    Events.SentMail,
                    Events.Properties.Init()
                    .Then(Events.Properties.EmailAddress, email.To.Mailboxes.First().Address));

                EmailStorage.DeleteEmailFromStorage(message.AsString, outgoingContainer);

                log.LogInformation($"sent mail to {email.To} - {email.Subject}");
            }
        }
        public static void Run(
            [QueueTrigger(queueName: Fn.Qs.ErrorNotification, Connection = Fn.ConnectionKeyName)] CloudQueueMessage message,
            [Queue(queueName: Fn.Qs.OutgoingMail, Connection = Fn.ConnectionKeyName)] ICollector <string> outgoingMail,
            ILogger log)
        {
            var storageConfig     = Fn.StorageConfig.Load();
            var storageAccount    = CloudStorageAccount.Parse(storageConfig.ConnectionString);
            var blobClient        = storageAccount.CreateCloudBlobClient();
            var outgoingContainer = blobClient.GetContainerReference(storageConfig.OutgoingMailContainerName);
            var errorContainer    = blobClient.GetContainerReference(storageConfig.ErrorMailContainerName);

            var originalEmail = EmailStorage.LoadEmailFromStorage(message.AsString, errorContainer);

            log.LogInformation($"Got error {originalEmail.Subject}... notifying... ");

            NotifyByEmail(originalEmail, outgoingContainer, outgoingMail, log);

            EmailStorage.DeleteEmailFromStorage(message.AsString, errorContainer);
        }
Esempio n. 3
0
        public static void Run(
            [QueueTrigger(queueName: Fn.Qs.IncomingMail, Connection = Fn.ConnectionKeyName)] CloudQueueMessage message,
            [Queue(queueName: Fn.Qs.OutgoingMail, Connection = Fn.ConnectionKeyName)] ICollector <string> outputQueue,
            [Queue(queueName: Fn.Qs.ErrorNotification, Connection = Fn.ConnectionKeyName)] ICollector <string> errorQueue,
            ILogger log)
        {
            var storageConfig     = Fn.StorageConfig.Load();
            var storageAccount    = CloudStorageAccount.Parse(storageConfig.ConnectionString);
            var blobClient        = storageAccount.CreateCloudBlobClient();
            var incomingContainer = blobClient.GetContainerReference(storageConfig.IncomingMailContainerName);
            var outgoingContainer = blobClient.GetContainerReference(storageConfig.OutgoingMailContainerName);
            var settingsContainer = blobClient.GetContainerReference(storageConfig.SettingsContainerName);
            var errorContainer    = blobClient.GetContainerReference(storageConfig.ErrorMailContainerName);
            var originalEmail     = EmailStorage.LoadEmailFromStorage(message.AsString, incomingContainer);
            var telemetry         = new TelemetryClient(new TelemetryConfiguration(Fn.InstrumentationKey()));

            var settingsStore = new SettingsStore(new CloudStore(settingsContainer));
            var reply         = MailProcessor.ProcessEmail(
                originalEmail,
                Fn.EmailConfig.Load().FromEmailAddress,
                settingsStore,
                telemetry);

            if (!reply.WasSuccessful)
            {
                var erroredFile = EmailStorage.SaveEmailToStorage(originalEmail, errorContainer);
                errorQueue.Add(erroredFile);
                log.LogInformation($"enqueueing error - {erroredFile}");
                log.LogError("Error processing email, ", reply.Exception);
            }
            EmailStorage.DeleteEmailFromStorage(message.AsString, incomingContainer);

            var filename = EmailStorage.SaveEmailToStorage(reply.Value, outgoingContainer);

            outputQueue.Add(filename);
            log.LogInformation($"extracted data, enqueueing reply - {reply.Value.To} - {filename}");
        }