Ejemplo n.º 1
0
    public static void Main(string[] args)
    {
        var result = CommandLine.Parser.Default.ParseArguments <Options>(args);

        result.WithParsed(options =>
        {
            DownloadWebpage(options.Url).ContinueWith(
                (messageTask) =>
            {
                var factory = new ConnectionFactory()
                {
                    HostName = "localhost"
                };
                using (var connection = factory.CreateConnection())
                    using (var channel = connection.CreateModel())
                    {
                        channel.QueueDeclare(queue: QueueName,
                                             durable: false,
                                             exclusive: false,
                                             autoDelete: false,
                                             arguments: null);

                        var messageContent = messageTask.Result;

                        // TODO: Should be sent encoded? SSL?
                        var simpleEmail = new SimpleSmtpEmail
                        {
                            SmtpUser     = options.SmtpUser,
                            SmtpPassword = options.SmtpPassword,
                            SmtpAddress  = options.SmtpAddress,
                            SmtpPort     = options.SmtpPort,
                            NameTo       = options.NameTo,
                            EmailTo      = options.EmailTo,
                            NameFrom     = options.NameFrom,
                            EmailFrom    = options.EmailFrom
                        };

                        if (IsClassPresent(messageContent, options.Class))
                        {
                            simpleEmail.Subject = options.EmailSubjectPositive;
                            simpleEmail.Content = options.EmailContentPositive;
                        }
                        else
                        {
                            simpleEmail.Subject = options.EmailSubjectNegative;
                            simpleEmail.Content = options.EmailContentNegative;
                        }

                        QueueSendingEmail(channel, simpleEmail);
                    }
            }
                );
        });

        Console.ReadKey();
    }
Ejemplo n.º 2
0
    private static void QueueSendingEmail(IModel channel, SimpleSmtpEmail simpleEmail)
    {
        var message = JsonConvert.SerializeObject(simpleEmail);
        var body    = Encoding.UTF8.GetBytes(message);

        channel.BasicPublish(exchange: "",
                             routingKey: QueueName,
                             basicProperties: null,
                             body: body);

        Console.WriteLine(" [x] Sent");
    }
Ejemplo n.º 3
0
    private static void SendEmail(SimpleSmtpEmail mail)
    {
        try
        {
            using (var client = new SmtpClient())
            {
                // TODO: What does this do specifically?
                // For demo-purposes, accept all SSL certificates
                client.ServerCertificateValidationCallback = (s, c, h, e) => true;

                client.Connect(mail.SmtpAddress, mail.SmtpPort, false);

                client.AuthenticationMechanisms.Remove("XOAUTH2");

                client.Authenticate(mail.SmtpUser, mail.SmtpPassword);

                var message = new MimeMessage();
                message.From.Add(new MailboxAddress(mail.NameFrom, mail.EmailFrom));
                message.To.Add(new MailboxAddress(mail.NameTo, mail.EmailTo));
                message.Subject = mail.Subject;

                message.Body = new TextPart("plain")
                {
                    Text = mail.Content
                };

                client.Send(message);

                client.Disconnect(true);

                Console.WriteLine("Email sent.");
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
    }