public void AddMessageToQueue(Payment payment) { var mImpl = new MailImpl(); var connection = RabbitMqService.RabbitMqConnection; var channel = RabbitMqService.RabbitMqModel; RabbitMqService.SetupInitialTopicQueue(channel); var basicProperties = channel.CreateBasicProperties(); basicProperties.DeliveryMode = 2; var envelope = new MailLetter { Envelope = "ParkingAds A/S", //to be replaced with customer email //[email protected] Recipient = "Customer", //to be replaced Mime = new Mime { From = "*****@*****.**", To = "*****@*****.**", Subject = "Receipt payment: ", // complete email subject with payment details TextVersion = "Dear customer, " + Environment.NewLine + Environment.NewLine + "Your payment receipment issued " + DateTime.Now + " is attached above. " + Environment.NewLine + "Thank you for your doing business with us ! ", //add payment details Attachments = new Attachments { Base64String = payment.Base64Receipt } } }; byte[] customerBuffer = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(envelope)); channel.BasicPublish(RabbitMqService.SerialisationExchangeName, RabbitMqService.SerialisationRoutingKey, basicProperties, customerBuffer); mImpl.SendMail(); }
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 }