clsEmail.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. using System;
  2. using System.Net;
  3. using MailKit.Net.Smtp;
  4. using MailKit;
  5. using MimeKit;
  6. using DSharpPlus.EventArgs;
  7. using System.Linq;
  8. using System.Threading.Tasks;
  9. using MailKit.Security;
  10. namespace TFABot
  11. {
  12. public class clsEmail
  13. {
  14. static String EmailFromAddress;
  15. static String SMTPHost;
  16. static int SMTPPort;
  17. static String SMTPUsername;
  18. static String SMTPPassword;
  19. public clsEmail()
  20. {
  21. }
  22. static public void GetSettings()
  23. {
  24. if (Program.SettingsList.TryGetValue("Email-SMTPHost", out SMTPHost))
  25. {
  26. Program.SettingsList.TryGetValue("Email-Username", out SMTPUsername);
  27. Program.SettingsList.TryGetValue("Email-Password", out SMTPPassword);
  28. Program.SettingsList.TryGetValue("Email-FromAddress", out EmailFromAddress);
  29. string port;
  30. if (Program.SettingsList.TryGetValue("Email-SMTPPort", out port))
  31. {
  32. int.TryParse(port, out SMTPPort);
  33. }
  34. }
  35. }
  36. static public Task SendEmail(String To, String Subject, String Message,DSharpPlus.Entities.DiscordChannel ChBotAlert = null)
  37. {
  38. Task task = null;
  39. if (String.IsNullOrEmpty(SMTPHost))
  40. {
  41. if (ChBotAlert!=null) ChBotAlert.SendMessageAsync($"No SMTP host set up");
  42. return task;
  43. }
  44. try
  45. {
  46. task = Task.Run(()=>
  47. {
  48. var message = new MimeMessage ();
  49. message.From.Add (new MailboxAddress (EmailFromAddress));
  50. message.To.Add (new MailboxAddress (To));
  51. message.Subject = Subject;
  52. message.Body = new TextPart ("plain") { Text = Message };
  53. using (var client = new SmtpClient ()) {
  54. try
  55. {
  56. // For demo-purposes, accept all SSL certificates (in case the server supports STARTTLS)
  57. client.ServerCertificateValidationCallback = (s, c, h, e) => true;
  58. client.Connect(SMTPHost, SMTPPort, SecureSocketOptions.Auto);
  59. // Note: only needed if the SMTP server requires authentication
  60. client.Authenticate(SMTPUsername, SMTPPassword);
  61. client.Timeout = 10000;
  62. client.Send(message);
  63. client.Disconnect(true);
  64. if (ChBotAlert != null)
  65. ChBotAlert.SendMessageAsync($"Sent e-mail {To}");
  66. }
  67. catch (Exception ex)
  68. {
  69. if (ChBotAlert != null)
  70. {
  71. ChBotAlert.SendMessageAsync($"FAILED: e-mail {To}");
  72. ChBotAlert.SendMessageAsync($"```{ex.Message}```");
  73. }
  74. Console.WriteLine(ex.Message);
  75. }
  76. }
  77. });
  78. }
  79. catch (Exception ex)
  80. {
  81. if (ChBotAlert!=null)
  82. ChBotAlert.SendMessageAsync($"Send e-mail error {ex.Message}");
  83. }
  84. return task;
  85. }
  86. static public void EmailAlertList(String message = "")
  87. {
  88. if (!String.IsNullOrEmpty(SMTPHost))
  89. {
  90. string alarmMessage = $"{Program.BotName} Alarm {message}";
  91. foreach (var user in Program.UserList.Values.Where(x=>x.OnDuty && !String.IsNullOrEmpty(x.email)))
  92. {
  93. SendEmail(user.email,alarmMessage,alarmMessage);
  94. }
  95. }
  96. }
  97. static public void email(String names, DSharpPlus.Entities.DiscordChannel ChBotAlert = null)
  98. {
  99. if (String.IsNullOrEmpty(SMTPHost))
  100. {
  101. if (ChBotAlert!=null) ChBotAlert.SendMessageAsync($"No SMTP host set up");
  102. return;
  103. }
  104. string alarmMessage = $"{Program.BotName} Alarm (manual Discord request)";
  105. foreach (var nameItem in names.Split(new char []{' '}, StringSplitOptions.RemoveEmptyEntries))
  106. {
  107. var name = nameItem.ToLower();
  108. if (!name.EndsWith("mail"))
  109. {
  110. clsUser user;
  111. if (!Program.UserList.TryGetValue(name,out user))
  112. {
  113. user = Program.UserList.Values.FirstOrDefault(x=>x.DiscordName.ToLower()==name || x.Name.ToLower()==name);
  114. }
  115. if (user!=null)
  116. SendEmail(user.email,alarmMessage,alarmMessage,ChBotAlert);
  117. else if (ChBotAlert!=null)
  118. ChBotAlert.SendMessageAsync("name not found!");
  119. }
  120. }
  121. }
  122. static public void email(MessageCreateEventArgs e)
  123. {
  124. var toRing = e.Message.Content.ToLower();
  125. email(toRing,e.Channel);
  126. }
  127. }
  128. }