The next generation of the Teknik Services. Written in ASP.NET. https://www.teknik.io/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

HMailService.cs 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. namespace Teknik.MailService
  5. {
  6. public class HMailService : IMailService
  7. {
  8. private readonly hMailServer.Application _App;
  9. private string _Username { get; set; }
  10. private string _Password { get; set; }
  11. private string _Domain { get; set; }
  12. private string _CounterServer { get; set; }
  13. private string _CounterDatabase { get; set; }
  14. private string _CounterUsername { get; set; }
  15. private string _CounterPassword { get; set; }
  16. private int _CounterPort { get; set; }
  17. public HMailService(string username, string password, string domain, string counterServer, string counterDatabase, string counterUsername, string counterPassword, int counterPort)
  18. {
  19. _Username = username;
  20. _Password = password;
  21. _Domain = domain;
  22. _CounterServer = counterServer;
  23. _CounterDatabase = counterDatabase;
  24. _CounterUsername = counterUsername;
  25. _CounterPassword = counterPassword;
  26. _CounterPort = counterPort;
  27. _App = InitApp();
  28. }
  29. public void CreateAccount(string username, string password, int size)
  30. {
  31. var domain = _App.Domains.ItemByName[_Domain];
  32. var newAccount = domain.Accounts.Add();
  33. newAccount.Address = username;
  34. newAccount.Password = password;
  35. newAccount.Active = true;
  36. newAccount.MaxSize = size;
  37. newAccount.Save();
  38. }
  39. public bool AccountExists(string username)
  40. {
  41. try
  42. {
  43. GetAccount(username);
  44. // We didn't error out, so the email exists
  45. return true;
  46. }
  47. catch { }
  48. return false;
  49. }
  50. public void DeleteAccount(string username)
  51. {
  52. var app = new hMailServer.Application();
  53. app.Connect();
  54. app.Authenticate(_Username, _Password);
  55. var domain = app.Domains.ItemByName[_Domain];
  56. var account = domain.Accounts.ItemByAddress[username];
  57. if (account != null)
  58. {
  59. account.Delete();
  60. }
  61. }
  62. public void EnableAccount(string username)
  63. {
  64. EditActivity(username, true);
  65. }
  66. public void DisableAccount(string username)
  67. {
  68. EditActivity(username, false);
  69. }
  70. public void EditActivity(string username, bool active)
  71. {
  72. var account = GetAccount(username);
  73. account.Active = active;
  74. account.Save();
  75. }
  76. public void EditMaxEmailsPerDay(string username, int maxPerDay)
  77. {
  78. //We need to check the actual git database
  79. MysqlDatabase mySQL = new MysqlDatabase(_CounterServer, _CounterDatabase, _CounterUsername, _CounterPassword, _CounterPort);
  80. string sql = @"INSERT INTO mailcounter.counts (qname, lastdate, qlimit, count) VALUES ({1}, NOW(), {0}, 0)
  81. ON DUPLICATE KEY UPDATE qlimit = {0}";
  82. mySQL.Execute(sql, new object[] { maxPerDay, username });
  83. }
  84. public void EditMaxSize(string username, int size)
  85. {
  86. var account = GetAccount(username);
  87. account.MaxSize = size;
  88. account.Save();
  89. }
  90. public void EditPassword(string username, string password)
  91. {
  92. var account = GetAccount(username);
  93. account.Password = password;
  94. account.Save();
  95. }
  96. public DateTime LastActive(string username)
  97. {
  98. var account = GetAccount(username);
  99. return (DateTime)account.LastLogonTime;
  100. }
  101. private hMailServer.Application InitApp()
  102. {
  103. var app = new hMailServer.Application();
  104. app.Connect();
  105. app.Authenticate(_Username, _Password);
  106. return app;
  107. }
  108. private hMailServer.Account GetAccount(string username)
  109. {
  110. var domain = _App.Domains.ItemByName[_Domain];
  111. return domain.Accounts.ItemByAddress[username];
  112. }
  113. }
  114. }