hMailServer C#API
公司企业邮箱需要使用hMailServer邮件服务,因需求需要有用户的添加、删除、密码重置接口,参考网上一些大神们的资料后,自己写了一个C#的api类
参考网上的一些资料:
1 public class hMailServerApi 2 { 3 public string domainStr = "";// 你的域名 4 public string admin = "Administrator"; 5 public string adminPassword = "xxx";// 你的管理员密码 6 private dynamic getobDomain() 7 { 8 dynamic _app; 9 _app = Activator.CreateInstance(Type.GetTypeFromProgID("hMailServer.Application")); 10 _app.Authenticate(admin, adminPassword); 11 return _app.Domains.ItemByName(domainStr); ; 12 } 13 14 private dynamic getObAccount() 15 { 16 dynamic obDomain = getobDomain(); 17 var obAccounts = obDomain.Accounts; 18 return obAccounts; 19 } 20 /// <summary> 21 /// 用户添加 22 /// </summary> 23 /// <param name="Address">邮箱账号</param> 24 /// <param name="Password">密码</param> 25 /// <param name="userName">用户名</param> 26 /// <returns></returns> 27 public Result UserAdd(string Address, string Password, string userName) 28 { 29 Result result = new Result(); 30 try 31 { 32 var obAccounts = getObAccount(); 33 var obNewAccount = obAccounts.Add(); 34 obNewAccount.Address = Address; 35 obNewAccount.Password = Password; 36 obNewAccount.Active = 1; 37 obNewAccount.Maxsize = 500; 38 obNewAccount.Save(); 39 result.tag = true; 40 result.msg = "添加成功!"; 41 } 42 catch (Exception ex) 43 { 44 result.msg = "用户:" + userName + "保存失败!" + ex.Message + "\r\n"; 45 result.tag = false; 46 } 47 return result; 48 } 49 /// <summary> 50 /// 用户删除 51 /// </summary> 52 /// <param name="strUsername"></param> 53 /// <returns></returns> 54 public Result DeleteAccount(string strUsername) 55 { 56 Result result = new Result(); 57 try 58 { 59 dynamic obDomain = getobDomain(); 60 dynamic obAccounts = obDomain.Accounts; 61 dynamic obDelAccount = obAccounts.ItemByAddress(strUsername + "@" + domainStr); 62 obDelAccount.Delete(); 63 result.tag = true; 64 result.msg = "删除成功!"; 65 } 66 catch (Exception ex) 67 { 68 result.msg = "删除失败:" + ex.Message + "\r\n"; 69 result.tag = false; 70 } 71 return result; 72 } 73 /// <summary> 74 /// 密码重置 75 /// </summary> 76 /// <param name="strUsername"></param> 77 /// <param name="oldPassword"></param> 78 /// <param name="newPassword"></param> 79 /// <returns></returns> 80 public Result ResetPassword(string strUsername, string oldPassword, string newPassword) 81 { 82 Result result = new Result(); 83 try 84 { 85 dynamic obDomain = getobDomain(); 86 dynamic obAccounts = obDomain.Accounts; 87 dynamic obAccount = obAccounts.ItemByAddress(strUsername + "@" + domainStr); 88 string strPassword = obAccount.Password; 89 if (strPassword == string.Empty) 90 { 91 result.msg = "帐户不正确或不存在 " + "\r\n"; 92 result.tag = false; 93 } 94 string strSALT = obAccount.Password.Substring(0, 6); 95 if (strSALT + GetSHA256(strSALT + oldPassword.Trim()) == obAccount.Password) 96 { 97 obAccount.Password = newPassword; 98 obAccount.Save(); 99 result.msg = "密码重置成功! " + "\r\n"; 100 result.tag = true; 101 102 } 103 else 104 { 105 result.msg = "密码输入错误"; 106 result.tag = false; 107 } 108 } 109 catch (Exception ex) 110 { 111 result.msg = "密码重置失败:" + ex.Message + "\r\n"; 112 result.tag = false; 113 } 114 return result; 115 } 116 /// <summary> 117 /// 加密 118 /// </summary> 119 /// <param name="text"></param> 120 /// <returns></returns> 121 122 private string GetSHA256(string text) 123 { 124 byte[] hashValue; 125 byte[] message = Encoding.UTF8.GetBytes(text); 126 127 SHA256Managed hashString = new SHA256Managed(); 128 string hex = ""; 129 130 hashValue = hashString.ComputeHash(message); 131 foreach (byte x in hashValue) 132 { 133 hex += String.Format("{0:x2}", x); 134 } 135 return hex; 136 } 137 } 138 public class Result 139 { 140 public bool tag { get; set; } 141 public string msg { get; set; } 142 }