Guest User

Untitled

a guest
May 23rd, 2018
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.20 KB | None | 0 0
  1. using EP.MailWorker.Services;
  2. using EP.Platform.ML;
  3. using EP.Platform.Utilities;
  4. using Newtonsoft.Json;
  5. using RabbitMQ.Client;
  6. using RabbitMQ.Client.Events;
  7. using Serilog;
  8. using System;
  9. using System.Collections;
  10. using System.Collections.Concurrent;
  11. using System.Collections.Generic;
  12. using System.IO;
  13. using System.Linq;
  14. using System.Net;
  15. using System.Net.Mail;
  16. using System.Reflection;
  17. using System.Text;
  18. using System.Text.RegularExpressions;
  19. using System.Threading;
  20. using System.Threading.Tasks;
  21.  
  22. namespace EP.MailWorker
  23. {
  24. class Program
  25. {
  26. const string hostName = "x.x.x.x";
  27. const string userName = "xx";
  28. const string password = "xx";
  29. const string queueName = "mail";
  30.  
  31. private static ConnectionFactory factory;
  32. private static IConnection connection;
  33. private static IModel channel;
  34.  
  35. static void Main(string[] args)
  36. {
  37.  
  38. //Log.Logger = new LoggerConfiguration()
  39. // .ReadFrom.AppSettings()
  40. // .CreateLogger();
  41.  
  42. //Log.Logger = new LoggerConfiguration()
  43. // .WriteTo.MongoDB("mongodb://x.x.x.x/maillog", collectionName: "log")
  44. // .CreateLogger();
  45.  
  46. factory = new ConnectionFactory()
  47. {
  48. HostName = hostName,
  49. UserName = userName,
  50. Password = password,
  51. Port = 5672
  52. };
  53.  
  54. connection = factory.CreateConnection();
  55.  
  56. channel = connection.CreateModel();
  57.  
  58. var consumer = new EventingBasicConsumer(channel);
  59.  
  60. consumer.ConsumerTag = Guid.NewGuid().ToString();
  61.  
  62. consumer.Received += (sender, ea) =>
  63. {
  64. var m = JsonConvert.DeserializeObject<Mail>(ea.Body.GetString());
  65.  
  66. Console.WriteLine($"Fila -> {m.Subject}");
  67.  
  68. using (var x = new ParallelEmailSender(2))
  69. {
  70. x.Send(m.Subject);
  71. }
  72. };
  73.  
  74. channel.BasicConsume(queueName, false, consumer);
  75.  
  76.  
  77. Console.Read();
  78.  
  79. }
  80.  
  81.  
  82. private static void sendmail(Object item)
  83. {
  84. Mail m;
  85. string info = "";
  86.  
  87. try
  88. {
  89. var x = (BasicDeliverEventArgs)item;
  90.  
  91. m = JsonConvert.DeserializeObject<Mail>(x.Body.GetString());
  92.  
  93. //Console.WriteLine($"Início -> {m.Subject}");
  94.  
  95. info = $"Template:{(int)m.Template} To:{m.ToAddress} Subject:{m.Subject}";
  96.  
  97. var msg = mountMessage(m);
  98.  
  99. new Amazon().Send(msg);
  100.  
  101. new Sendgrid().Send(msg);
  102.  
  103. channel.BasicAck(deliveryTag: x.DeliveryTag, multiple: true);
  104.  
  105. //Log.Information("OK " + info);
  106.  
  107. Console.WriteLine($"FIM -> {m.Subject}");
  108.  
  109. }
  110. catch (Exception ex)
  111. {
  112. Log.Error(ex, info);
  113. }
  114. }
  115.  
  116. private static MailMessage mountMessage(Mail m)
  117. {
  118.  
  119. MailMessage message = new MailMessage();
  120.  
  121. //message.From = new MailAddress(m.FromAddress, m.FromName);
  122. message.From = new MailAddress("xxxxxx@gmail.com", "Xxxxxx Test");
  123. message.To.Add(new MailAddress(m.ToAddress, m.ToName));
  124.  
  125. message.Subject = m.Subject;
  126.  
  127. message.IsBodyHtml = m.IsHtml;
  128.  
  129.  
  130. if ((int)m.Template > 0)
  131. {
  132. string template = getTemplate((int)m.Template).bindData(m.Data);
  133.  
  134. message.Body = template;
  135. }
  136. else
  137. message.Body = m.Body;
  138.  
  139. return message;
  140.  
  141. }
  142.  
  143. private static string getTemplate(int id)
  144. {
  145. string binPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
  146.  
  147. string mail = $"{Path.Combine(binPath, "emails")}\\{id}.html";
  148.  
  149. return File.ReadAllText(mail);
  150. }
  151.  
  152.  
  153. }
  154.  
  155. public class ParallelEmailSender : IDisposable
  156. {
  157. private readonly BlockingCollection<string> blockingCollection;
  158.  
  159. public ParallelEmailSender(int threadsCount)
  160. {
  161. blockingCollection = new BlockingCollection<string>(new ConcurrentQueue<string>());
  162. for (int i = 0; i < threadsCount; i++)
  163. {
  164. Task.Factory.StartNew(SendInternal);
  165. }
  166. }
  167.  
  168. public void Send(string message)
  169. {
  170. blockingCollection.Add(message);
  171. }
  172.  
  173. private void SendInternal()
  174. {
  175. foreach (string message in blockingCollection.GetConsumingEnumerable())
  176. {
  177. // send method
  178. Console.WriteLine(message);
  179. }
  180. }
  181.  
  182. public void Dispose()
  183. {
  184. blockingCollection.CompleteAdding();
  185. }
  186. }
  187.  
  188. public static class Extensions
  189. {
  190.  
  191. public static string bindData(this string str, Dictionary<string, string> dic)
  192. {
  193. if (str.IsNullOrWhiteSpace())
  194. return null;
  195.  
  196. Regex reg = new Regex(@"@\w+@");
  197.  
  198. foreach (Match match in reg.Matches(str))
  199. str = str.Replace(match.Value, dic[match.Value]);
  200.  
  201. return str;
  202. }
  203.  
  204. }
  205. }
Add Comment
Please, Sign In to add comment