EmailStore.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using Comal.Classes;
  4. using InABox.Core;
  5. using InABox.Mail;
  6. namespace Comal.Stores
  7. {
  8. internal class EmailStore : BaseStore<Email>
  9. {
  10. protected override void BeforeSave(Email entity)
  11. {
  12. base.BeforeSave(entity);
  13. var user = FindSubStore<User>().Load(new Filter<User>(x => x.UserID).IsEqualTo(UserID)).FirstOrDefault();
  14. if (user != null)
  15. {
  16. var mailer = new ExchangeMailer
  17. {
  18. MailboxHost = user.EmailHost, MailboxDomain = user.EmailDomain, MailboxUserName = user.EmailUserID,
  19. MailboxPassword = user.EmailPassword
  20. };
  21. if (mailer.Connect())
  22. {
  23. var msg = mailer.CreateMessage();
  24. msg.To = entity.To;
  25. msg.CC = entity.CC;
  26. msg.BCC = entity.BCC;
  27. msg.Subject = entity.Subject;
  28. msg.Body = entity.Message;
  29. var bOK = mailer.SendMessage(msg);
  30. //if (!bOK)
  31. // entity.History.Add(new EntityHistory() { User = UserID, Timestamp = DateTime.Now, Note = "Unable to Send Email" });
  32. //else
  33. // entity.History.Add(new EntityHistory() { User = UserID, Timestamp = DateTime.Now, Note = "Email Sent Successfully" });
  34. }
  35. //entity.History.Add(new EntityHistory() { User = UserID, Timestamp = DateTime.Now, Note = "Unable to Connect to Exchange Mail Server" });
  36. }
  37. }
  38. protected override void OnSave(Email entity, ref string auditnote)
  39. {
  40. // Actually, we don't want save these - (1) there's simply too many of them, (2) we're not using them (3) if we're really keen, we can trawl the logs
  41. //base.OnSave(entity);
  42. }
  43. protected override void OnSave(IEnumerable<Email> entities, ref string auditnote)
  44. {
  45. //base.OnSave(entities);
  46. }
  47. }
  48. }