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

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