123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- using System;
- using System.Net;
- using MailKit.Net.Smtp;
- using MailKit;
- using MimeKit;
- using DSharpPlus.EventArgs;
- using System.Linq;
- using System.Threading.Tasks;
- using MailKit.Security;
- namespace TFABot
- {
- public class clsEmail
- {
- static String EmailFromAddress;
- static String SMTPHost;
- static int SMTPPort;
- static String SMTPUsername;
- static String SMTPPassword;
- public clsEmail()
- {
- }
-
- static public void GetSettings()
- {
- if (Program.SettingsList.TryGetValue("Email-SMTPHost", out SMTPHost))
- {
- Program.SettingsList.TryGetValue("Email-Username", out SMTPUsername);
- Program.SettingsList.TryGetValue("Email-Password", out SMTPPassword);
- Program.SettingsList.TryGetValue("Email-FromAddress", out EmailFromAddress);
-
- string port;
- if (Program.SettingsList.TryGetValue("Email-SMTPPort", out port))
- {
- int.TryParse(port, out SMTPPort);
- }
- }
- }
-
-
- static public Task SendEmail(String To, String Subject, String Message,DSharpPlus.Entities.DiscordChannel ChBotAlert = null)
- {
- Task task = null;
-
- if (String.IsNullOrEmpty(SMTPHost))
- {
- if (ChBotAlert!=null) ChBotAlert.SendMessageAsync($"No SMTP host set up");
- return task;
- }
-
- try
- {
-
- task = Task.Run(()=>
- {
- var message = new MimeMessage ();
- message.From.Add (new MailboxAddress (EmailFromAddress));
- message.To.Add (new MailboxAddress (To));
- message.Subject = Subject;
-
- message.Body = new TextPart ("plain") { Text = Message };
-
-
- using (var client = new SmtpClient ()) {
- try
- {
- // For demo-purposes, accept all SSL certificates (in case the server supports STARTTLS)
- client.ServerCertificateValidationCallback = (s, c, h, e) => true;
- client.Connect(SMTPHost, SMTPPort, SecureSocketOptions.Auto);
- // Note: only needed if the SMTP server requires authentication
- client.Authenticate(SMTPUsername, SMTPPassword);
- client.Timeout = 10000;
- client.Send(message);
- client.Disconnect(true);
- if (ChBotAlert != null)
- ChBotAlert.SendMessageAsync($"Sent e-mail {To}");
- }
- catch (Exception ex)
- {
- if (ChBotAlert != null)
- {
- ChBotAlert.SendMessageAsync($"FAILED: e-mail {To}");
- ChBotAlert.SendMessageAsync($"```{ex.Message}```");
- }
- Console.WriteLine(ex.Message);
- }
-
- }
- });
- }
- catch (Exception ex)
- {
- if (ChBotAlert!=null)
- ChBotAlert.SendMessageAsync($"Send e-mail error {ex.Message}");
- }
-
- return task;
- }
- static public void EmailAlertList(String message = "")
- {
-
- if (!String.IsNullOrEmpty(SMTPHost))
- {
- string alarmMessage = $"{Program.BotName} Alarm {message}";
- foreach (var user in Program.UserList.Values.Where(x=>x.OnDuty && !String.IsNullOrEmpty(x.email)))
- {
- SendEmail(user.email,alarmMessage,alarmMessage);
- }
- }
- }
-
-
- static public void email(String names, DSharpPlus.Entities.DiscordChannel ChBotAlert = null)
- {
-
-
- if (String.IsNullOrEmpty(SMTPHost))
- {
- if (ChBotAlert!=null) ChBotAlert.SendMessageAsync($"No SMTP host set up");
- return;
- }
-
- string alarmMessage = $"{Program.BotName} Alarm (manual Discord request)";
-
- foreach (var nameItem in names.Split(new char []{' '}, StringSplitOptions.RemoveEmptyEntries))
- {
- var name = nameItem.ToLower();
- if (!name.EndsWith("mail"))
- {
- clsUser user;
- if (!Program.UserList.TryGetValue(name,out user))
- {
- user = Program.UserList.Values.FirstOrDefault(x=>x.DiscordName.ToLower()==name || x.Name.ToLower()==name);
- }
-
- if (user!=null)
- SendEmail(user.email,alarmMessage,alarmMessage,ChBotAlert);
- else if (ChBotAlert!=null)
- ChBotAlert.SendMessageAsync("name not found!");
- }
- }
-
- }
-
- static public void email(MessageCreateEventArgs e)
- {
- var toRing = e.Message.Content.ToLower();
- email(toRing,e.Channel);
- }
-
-
- }
- }
|