IMAPProvider.cs 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. using InABox.Core;
  2. using InABox.Mail;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace InABox.API
  9. {
  10. public class IMAPProvider : ISMSProvider
  11. {
  12. private IMAPMailer Mailer;
  13. public SMSProviderType ProviderType => SMSProviderType.IMAP;
  14. public TwoFactorAuthenticationType TwoFactorAuthenticationType => TwoFactorAuthenticationType.Email;
  15. public IMAPProvider(string host, int port, string username, string password)
  16. {
  17. Mailer = new IMAPMailer()
  18. {
  19. SMTPHost = host,
  20. SMTPPort = port,
  21. SMTPUserName = username,
  22. SMTPPassword = password
  23. };
  24. }
  25. public bool SendMessage(string recipient, string message)
  26. {
  27. try
  28. {
  29. var imapMessage = Mailer.CreateMessage();
  30. imapMessage.Subject = "Message from PRS";
  31. imapMessage.Body = message;
  32. imapMessage.IsHTML = false;
  33. imapMessage.To = new List<string> { recipient };
  34. Mailer.SendMessage(imapMessage);
  35. }
  36. catch (Exception)
  37. {
  38. return false;
  39. }
  40. return true;
  41. }
  42. }
  43. }