Example #1
0
        public void SendMail()
        {
            #region RabbitMq
            var envelope   = new MailLetter();
            var rbImpl     = new RabbitMqImpl();
            var connection = RabbitMqService.RabbitMqConnection;
            var channel    = RabbitMqService.RabbitMqModel;
            RabbitMqService.SetupInitialTopicQueue(channel);
            channel.BasicQos(prefetchSize: 0, prefetchCount: 1, global: false);
            var consumer = new EventingBasicConsumer(channel);
            consumer.Received += (model, ea) =>
            {
                var body    = ea.Body;
                var message = Encoding.UTF8.GetString(body);
                envelope = JsonConvert.DeserializeObject <MailLetter>(message);
                channel.BasicAck(deliveryTag: ea.DeliveryTag, multiple: false);

                #region email
                var clientDetails = new SmtpClient
                {
                    Port        = 587,
                    Host        = "smtp.gmail.com",
                    EnableSsl   = true,
                    Credentials = new NetworkCredential(envelope.Mime.From, PasswordHandler.GetPassword())
                };
                var         from        = new MailAddress(envelope.Mime.From, envelope.Envelope);
                var         to          = new MailAddress(envelope.Mime.To, envelope.Recipient);
                MailMessage mailMessage = new MailMessage(from, to)
                {
                    Subject = envelope.Mime.Subject,
                    Body    = envelope.Mime.TextVersion
                };
                if (envelope.Mime.Attachments.Base64String.Length > 0)
                {
                    var attachement = new Attachment(new MemoryStream(envelope.Mime.Attachments.Base64String), "Receipt.pdf", MediaTypeNames.Application.Pdf);
                    mailMessage.Attachments.Add(attachement);
                }
                try{
                    clientDetails.Send(mailMessage);
                }
                catch (Exception ex)
                {
                    System.Diagnostics.Debug.WriteLine("Exception caught while sending email: {0}", ex.ToString());
                }
                #endregion
            };
            channel.BasicConsume(RabbitMqService.SerialisationQueueName, autoAck: false, consumer: consumer);
            #endregion
        }
Example #2
0
        public void Base64Pdf(PaymentViewModel payment)
        {
            var rbImpl = new RabbitMqImpl();

            byte[] bytes;
            byte[] imageBytes = Convert.FromBase64String(payment.base64StringReceipt.Replace("data:image/png;base64,", ""));
            var    image      = Image.GetInstance(imageBytes);

            using (MemoryStream memoryStream = new MemoryStream())
            {
                var document = new Document(PageSize.A4, 88f, 88f, 10f, 10f);
                var writer   = PdfWriter.GetInstance(document, memoryStream);
                document.Open();
                document.Add(image);
                document.Close();
                bytes = memoryStream.ToArray();
                memoryStream.Close();
                rbImpl.AddMessageToQueue(new Payment {
                    Base64Receipt = bytes
                });
            }
        }