Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using EP.MailWorker.Services;
- using EP.Platform.ML;
- using EP.Platform.Utilities;
- using Newtonsoft.Json;
- using RabbitMQ.Client;
- using RabbitMQ.Client.Events;
- using Serilog;
- using System;
- using System.Collections;
- using System.Collections.Concurrent;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Net;
- using System.Net.Mail;
- using System.Reflection;
- using System.Text;
- using System.Text.RegularExpressions;
- using System.Threading;
- using System.Threading.Tasks;
- namespace EP.MailWorker
- {
- class Program
- {
- const string hostName = "x.x.x.x";
- const string userName = "xx";
- const string password = "xx";
- const string queueName = "mail";
- private static ConnectionFactory factory;
- private static IConnection connection;
- private static IModel channel;
- static void Main(string[] args)
- {
- //Log.Logger = new LoggerConfiguration()
- // .ReadFrom.AppSettings()
- // .CreateLogger();
- //Log.Logger = new LoggerConfiguration()
- // .WriteTo.MongoDB("mongodb://x.x.x.x/maillog", collectionName: "log")
- // .CreateLogger();
- factory = new ConnectionFactory()
- {
- HostName = hostName,
- UserName = userName,
- Password = password,
- Port = 5672
- };
- connection = factory.CreateConnection();
- channel = connection.CreateModel();
- var consumer = new EventingBasicConsumer(channel);
- consumer.ConsumerTag = Guid.NewGuid().ToString();
- consumer.Received += (sender, ea) =>
- {
- var m = JsonConvert.DeserializeObject<Mail>(ea.Body.GetString());
- Console.WriteLine($"Fila -> {m.Subject}");
- using (var x = new ParallelEmailSender(2))
- {
- x.Send(m.Subject);
- }
- };
- channel.BasicConsume(queueName, false, consumer);
- Console.Read();
- }
- private static void sendmail(Object item)
- {
- Mail m;
- string info = "";
- try
- {
- var x = (BasicDeliverEventArgs)item;
- m = JsonConvert.DeserializeObject<Mail>(x.Body.GetString());
- //Console.WriteLine($"Início -> {m.Subject}");
- info = $"Template:{(int)m.Template} To:{m.ToAddress} Subject:{m.Subject}";
- var msg = mountMessage(m);
- new Amazon().Send(msg);
- new Sendgrid().Send(msg);
- channel.BasicAck(deliveryTag: x.DeliveryTag, multiple: true);
- //Log.Information("OK " + info);
- Console.WriteLine($"FIM -> {m.Subject}");
- }
- catch (Exception ex)
- {
- Log.Error(ex, info);
- }
- }
- private static MailMessage mountMessage(Mail m)
- {
- MailMessage message = new MailMessage();
- //message.From = new MailAddress(m.FromAddress, m.FromName);
- message.From = new MailAddress("xxxxxx@gmail.com", "Xxxxxx Test");
- message.To.Add(new MailAddress(m.ToAddress, m.ToName));
- message.Subject = m.Subject;
- message.IsBodyHtml = m.IsHtml;
- if ((int)m.Template > 0)
- {
- string template = getTemplate((int)m.Template).bindData(m.Data);
- message.Body = template;
- }
- else
- message.Body = m.Body;
- return message;
- }
- private static string getTemplate(int id)
- {
- string binPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
- string mail = $"{Path.Combine(binPath, "emails")}\\{id}.html";
- return File.ReadAllText(mail);
- }
- }
- public class ParallelEmailSender : IDisposable
- {
- private readonly BlockingCollection<string> blockingCollection;
- public ParallelEmailSender(int threadsCount)
- {
- blockingCollection = new BlockingCollection<string>(new ConcurrentQueue<string>());
- for (int i = 0; i < threadsCount; i++)
- {
- Task.Factory.StartNew(SendInternal);
- }
- }
- public void Send(string message)
- {
- blockingCollection.Add(message);
- }
- private void SendInternal()
- {
- foreach (string message in blockingCollection.GetConsumingEnumerable())
- {
- // send method
- Console.WriteLine(message);
- }
- }
- public void Dispose()
- {
- blockingCollection.CompleteAdding();
- }
- }
- public static class Extensions
- {
- public static string bindData(this string str, Dictionary<string, string> dic)
- {
- if (str.IsNullOrWhiteSpace())
- return null;
- Regex reg = new Regex(@"@\w+@");
- foreach (Match match in reg.Matches(str))
- str = str.Replace(match.Value, dic[match.Value]);
- return str;
- }
- }
- }
Add Comment
Please, Sign In to add comment