Ejemplo n.º 1
0
        private static void NotifyByEmail(MimeMessage originalMessage, CloudBlobContainer outgoingContainer, ICollector <string> outgoingMail, ILogger log)
        {
            var emailConfig = Fn.EmailConfig.Load();

            log.LogInformation($"... Notifying admin of error via email ({emailConfig.AdminEmailAddress})");

            var forwarded = MailUtility.ForwardMessage(
                originalMessage,
                "Here's an error email.",
                emailConfig.AdminEmailAddress,
                emailConfig.FromEmailAddress);

            outgoingMail.Add(EmailStorage.SaveEmailToStorage(forwarded, outgoingContainer));
        }
Ejemplo n.º 2
0
        public static void Run(
            [TimerTrigger("0 */1 * * * *")] TimerInfo timer,
            [Queue(queueName: Fn.Qs.IncomingMail, Connection = Fn.ConnectionKeyName)] ICollector <string> outputQueue,
            ILogger log)
        {
            using (var client = new ImapClient())
            {
                var emailConfig = Fn.EmailConfig.Load();
                client.ServerCertificateValidationCallback = (s, c, h, e) => true;

                client.Connect(emailConfig.ImapServer, emailConfig.ImapPort, SecureSocketOptions.SslOnConnect);
                client.Authenticate(emailConfig.Username, emailConfig.Password);

                var inbox = client.Inbox;
                inbox.Open(FolderAccess.ReadWrite);

                var storageConfig  = Fn.StorageConfig.Load();
                var storageAccount = CloudStorageAccount.Parse(storageConfig.ConnectionString);
                var blobClient     = storageAccount.CreateCloudBlobClient();
                var container      = blobClient.GetContainerReference(storageConfig.IncomingMailContainerName);
                //var telemetry = new TelemetryClient(new TelemetryConfiguration(Fn.InstrumentationKey()));

                inbox.Search(SearchQuery.NotSeen)
                .Select(uid => new { uid, message = inbox.GetMessage(uid) })
                .ToList()
                .ForEach(x => {
                    var filename = EmailStorage.SaveEmailToStorage(x.message, container);

                    outputQueue.Add(filename);
                    inbox.AddFlags(x.uid, MessageFlags.Seen, false);

                    log.LogInformation($"Queued email - {x.message.From.ToString()} - {x.message.Subject} - {filename}");
                    //telemetry.TrackEvent(Events.ReceivedMail);
                });

                client.Disconnect(true);
            }
        }
Ejemplo 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}");
        }