forked from project-redbud/FunGame-Core
封装邮件、验证码工具类
This commit is contained in:
parent
7ece72bcc8
commit
756e837165
28
FunGame.Core/Api/Transmittal/MailSender.cs
Normal file
28
FunGame.Core/Api/Transmittal/MailSender.cs
Normal file
@ -0,0 +1,28 @@
|
||||
using Milimoe.FunGame.Core.Interface.Base;
|
||||
using Milimoe.FunGame.Core.Library.Common.Network;
|
||||
using Milimoe.FunGame.Core.Library.Constant;
|
||||
using Milimoe.FunGame.Core.Library.Server;
|
||||
using Milimoe.FunGame.Core.Service;
|
||||
|
||||
namespace Milimoe.FunGame.Core.Api.Transmittal
|
||||
{
|
||||
public class MailSender
|
||||
{
|
||||
public SmtpClientInfo SmtpClientInfo => _SmtpClientInfo;
|
||||
public MailSendResult LastestResult => _LastestResult;
|
||||
|
||||
private SmtpClientInfo _SmtpClientInfo;
|
||||
private MailSendResult _LastestResult = MailSendResult.NotSend;
|
||||
|
||||
public MailSender(string SenderMailAddress, string SenderName, string SenderPassword, string Host, int Port, bool OpenSSL)
|
||||
{
|
||||
_SmtpClientInfo = new SmtpClientInfo(SenderMailAddress, SenderName, SenderPassword, Host, Port, OpenSSL);
|
||||
}
|
||||
|
||||
public MailSendResult Send(MailObject Mail)
|
||||
{
|
||||
_LastestResult = MailManager.Send(this, Mail);
|
||||
return _LastestResult;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -255,4 +255,109 @@ namespace Milimoe.FunGame.Core.Api.Utility
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 验证服务
|
||||
|
||||
public class Verification
|
||||
{
|
||||
/// <summary>
|
||||
/// 生成验证码
|
||||
/// </summary>
|
||||
/// <param name="type">类型</param>
|
||||
/// <param name="length">长度</param>
|
||||
/// <returns></returns>
|
||||
public static string CreateVerifyCode(VerifyCodeType type, int length)
|
||||
{
|
||||
return type switch
|
||||
{
|
||||
VerifyCodeType.MixVerifyCode => MixVerifyCode(length),
|
||||
VerifyCodeType.LetterVerifyCode => LetterVerifyCode(length),
|
||||
_ => NumberVerifyCode(length),
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 数字验证码
|
||||
/// </summary>
|
||||
/// <param name="length"></param>
|
||||
/// <returns></returns>
|
||||
private static string NumberVerifyCode(int length)
|
||||
{
|
||||
int[] RandMembers = new int[length];
|
||||
int[] GetNumbers = new int[length];
|
||||
string VerifyCode = "";
|
||||
//生成起始序列值
|
||||
int seekSeek = unchecked((int)DateTime.Now.Ticks);
|
||||
Random seekRand = new(seekSeek);
|
||||
int beginSeek = seekRand.Next(0, int.MaxValue - length * 10000);
|
||||
int[] seeks = new int[length];
|
||||
for (int i = 0; i < length; i++)
|
||||
{
|
||||
beginSeek += 10000;
|
||||
seeks[i] = beginSeek;
|
||||
}
|
||||
//生成随机数字
|
||||
for (int i = 0; i < length; i++)
|
||||
{
|
||||
Random rand = new(seeks[i]);
|
||||
int pownum = 1 * (int)Math.Pow(10, length);
|
||||
RandMembers[i] = rand.Next(pownum, int.MaxValue);
|
||||
}
|
||||
//抽取随机数字
|
||||
for (int i = 0; i < length; i++)
|
||||
{
|
||||
string numStr = RandMembers[i].ToString();
|
||||
int numLength = numStr.Length;
|
||||
Random rand = new();
|
||||
int numPosition = rand.Next(0, numLength - 1);
|
||||
GetNumbers[i] = int.Parse(numStr.Substring(numPosition, 1));
|
||||
}
|
||||
//生成验证码
|
||||
for (int i = 0; i < length; i++)
|
||||
{
|
||||
VerifyCode += GetNumbers[i].ToString();
|
||||
}
|
||||
return VerifyCode;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 字母验证码
|
||||
/// </summary>
|
||||
/// <param name="length"></param>
|
||||
/// <returns></returns>
|
||||
private static string LetterVerifyCode(int length)
|
||||
{
|
||||
char[] Verification = new char[length];
|
||||
char[] Dictionary = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
|
||||
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };
|
||||
Random random = new();
|
||||
for (int i = 0; i < length; i++)
|
||||
{
|
||||
Verification[i] = Dictionary[random.Next(Dictionary.Length - 1)];
|
||||
}
|
||||
return new string(Verification);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 混合验证码
|
||||
/// </summary>
|
||||
/// <param name="length"></param>
|
||||
/// <returns></returns>
|
||||
private static string MixVerifyCode(int length)
|
||||
{
|
||||
char[] Verification = new char[length];
|
||||
char[] Dictionary = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
|
||||
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
|
||||
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'
|
||||
};
|
||||
Random random = new();
|
||||
for (int i = 0; i < length; i++)
|
||||
{
|
||||
Verification[i] = Dictionary[random.Next(Dictionary.Length - 1)];
|
||||
}
|
||||
return new string(Verification);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
|
||||
11
FunGame.Core/Interface/Base/IMailSender.cs
Normal file
11
FunGame.Core/Interface/Base/IMailSender.cs
Normal file
@ -0,0 +1,11 @@
|
||||
using Milimoe.FunGame.Core.Library.Constant;
|
||||
|
||||
namespace Milimoe.FunGame.Core.Interface.Base
|
||||
{
|
||||
public interface IMailSender
|
||||
{
|
||||
public string SenderMailAddress { get; }
|
||||
public string SenderName { get; }
|
||||
public string SenderPassword { get; }
|
||||
}
|
||||
}
|
||||
28
FunGame.Core/Library/Common/Network/MailObject.cs
Normal file
28
FunGame.Core/Library/Common/Network/MailObject.cs
Normal file
@ -0,0 +1,28 @@
|
||||
using System.Net.Mail;
|
||||
|
||||
namespace Milimoe.FunGame.Core.Library.Common.Network
|
||||
{
|
||||
public class MailObject
|
||||
{
|
||||
public string Sender { get; } = "";
|
||||
public string SenderName { get; } = "";
|
||||
public string Subject { get; } = "";
|
||||
public string Body { get; } = "";
|
||||
public MailPriority Priority { get; } = MailPriority.Normal;
|
||||
public bool HTML { get; } = false;
|
||||
public string[] ToList { get; } = Array.Empty<string>();
|
||||
public string[] CCList { get; } = Array.Empty<string>();
|
||||
|
||||
public MailObject(string Sender, string SenderName, string Subject, string Body, MailPriority Priority, bool HTML, string[] ToList, string[] CcList)
|
||||
{
|
||||
this.Sender = Sender;
|
||||
this.SenderName = SenderName;
|
||||
this.Subject = Subject;
|
||||
this.Body = Body;
|
||||
this.Priority = Priority;
|
||||
this.HTML = HTML;
|
||||
this.ToList = ToList;
|
||||
this.CCList = CcList;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -16,10 +16,10 @@ namespace Milimoe.FunGame.Core.Library.Common.Network
|
||||
public string ServerNotice { get; } = "";
|
||||
public bool Connected => Instance != null && Instance.Connected;
|
||||
public bool Receiving => _Receiving;
|
||||
public List<BaseModel> GetUsersList => UserThreads.GetList();
|
||||
public int UsersCount => UserThreads.Count;
|
||||
public List<BaseModel> GetUsersList => OnlineUsers.GetList();
|
||||
public int UsersCount => OnlineUsers.Count;
|
||||
|
||||
private readonly ThreadManager UserThreads;
|
||||
private readonly ThreadManager OnlineUsers;
|
||||
private bool _Receiving = false;
|
||||
|
||||
private ServerSocket(System.Net.Sockets.Socket Instance, int ServerPort, int MaxConnection = 0)
|
||||
@ -27,9 +27,9 @@ namespace Milimoe.FunGame.Core.Library.Common.Network
|
||||
this.Instance = Instance;
|
||||
this.ServerPort = ServerPort;
|
||||
if (MaxConnection <= 0)
|
||||
UserThreads = new ThreadManager();
|
||||
OnlineUsers = new ThreadManager();
|
||||
else
|
||||
UserThreads = new ThreadManager(MaxConnection);
|
||||
OnlineUsers = new ThreadManager(MaxConnection);
|
||||
}
|
||||
|
||||
public static ServerSocket StartListening(int Port = 22222, int MaxConnection = 0)
|
||||
@ -54,22 +54,22 @@ namespace Milimoe.FunGame.Core.Library.Common.Network
|
||||
|
||||
public bool AddUser(string UserName, BaseModel t)
|
||||
{
|
||||
return UserThreads.Add(UserName, t);
|
||||
return OnlineUsers.Add(UserName, t);
|
||||
}
|
||||
|
||||
public bool RemoveUser(string UserName)
|
||||
{
|
||||
return UserThreads.Remove(UserName);
|
||||
return OnlineUsers.Remove(UserName);
|
||||
}
|
||||
|
||||
public bool ContainsUser(string UserName)
|
||||
{
|
||||
return UserThreads.ContainsKey(UserName);
|
||||
return OnlineUsers.ContainsKey(UserName);
|
||||
}
|
||||
|
||||
public BaseModel GetUser(string UserName)
|
||||
{
|
||||
return UserThreads[UserName];
|
||||
return OnlineUsers[UserName];
|
||||
}
|
||||
|
||||
public SocketResult Send(SocketMessageType type, params object[] objs)
|
||||
|
||||
@ -34,6 +34,7 @@
|
||||
public const string HeartBeat = "HeartBeat";
|
||||
public const string IntoRoom = "IntoRoom";
|
||||
public const string Chat = "Chat";
|
||||
public const string QuitRoom = "QuitRoom";
|
||||
}
|
||||
|
||||
public class ReflectionSet
|
||||
|
||||
@ -40,4 +40,11 @@
|
||||
SQLError,
|
||||
IsExist
|
||||
}
|
||||
|
||||
public enum MailSendResult
|
||||
{
|
||||
Success,
|
||||
Fail,
|
||||
NotSend
|
||||
}
|
||||
}
|
||||
|
||||
@ -157,6 +157,14 @@
|
||||
Particle
|
||||
}
|
||||
|
||||
public enum VerifyCodeType
|
||||
{
|
||||
NumberVerifyCode,
|
||||
LetterVerifyCode,
|
||||
MixVerifyCode,
|
||||
ImageVerifyCode
|
||||
}
|
||||
|
||||
public enum RoleType
|
||||
{
|
||||
Core,
|
||||
|
||||
@ -119,4 +119,9 @@
|
||||
{
|
||||
public override string Message => "无法发送公共信息 (#10024)";
|
||||
}
|
||||
|
||||
public class CanNotSendEmailException : Exception
|
||||
{
|
||||
public override string Message => "无法发送邮件 (#10025)";
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,7 +1,6 @@
|
||||
using Milimoe.FunGame.Core.Entity;
|
||||
using Milimoe.FunGame.Core.Library.Common.Network;
|
||||
using Milimoe.FunGame.Core.Library.Constant;
|
||||
using Milimoe.FunGame.Core.Service;
|
||||
|
||||
namespace Milimoe.FunGame.Core.Library.Server
|
||||
{
|
||||
@ -12,34 +11,9 @@ namespace Milimoe.FunGame.Core.Library.Server
|
||||
public abstract Task? Task { get; }
|
||||
public abstract string ClientName { get; }
|
||||
public abstract User? User { get; }
|
||||
public List<BaseModel> GetClientsList => ClientThreads.GetList();
|
||||
public int ClientsCount => ClientThreads.Count;
|
||||
|
||||
public abstract bool Read(ClientSocket socket);
|
||||
public abstract bool Send(ClientSocket socket, SocketMessageType type, params object[] objs);
|
||||
public abstract void Start();
|
||||
|
||||
private readonly ThreadManager ClientThreads = new();
|
||||
|
||||
public bool AddClient(string ClientName, BaseModel t)
|
||||
{
|
||||
return ClientThreads.Add(ClientName, t);
|
||||
}
|
||||
|
||||
public bool RemoveClient(string ClientName)
|
||||
{
|
||||
return ClientThreads.Remove(ClientName);
|
||||
}
|
||||
|
||||
public bool ContainsClient(string ClientName)
|
||||
{
|
||||
return ClientThreads.ContainsKey(ClientName);
|
||||
}
|
||||
|
||||
public BaseModel GetClient(string ClientName)
|
||||
{
|
||||
return ClientThreads[ClientName];
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
31
FunGame.Core/Library/Server/SmtpClientInfo.cs
Normal file
31
FunGame.Core/Library/Server/SmtpClientInfo.cs
Normal file
@ -0,0 +1,31 @@
|
||||
using Milimoe.FunGame.Core.Interface.Base;
|
||||
|
||||
namespace Milimoe.FunGame.Core.Library.Server
|
||||
{
|
||||
public class SmtpClientInfo : IMailSender
|
||||
{
|
||||
public string Host => _Host;
|
||||
public int Port => _Port;
|
||||
public bool OpenSSL => _OpenSSL;
|
||||
public string SenderMailAddress => _SenderMailAddress;
|
||||
public string SenderName => _SenderName;
|
||||
public string SenderPassword => _SenderPassword;
|
||||
|
||||
private string _Host = "";
|
||||
private int _Port = 587;
|
||||
private bool _OpenSSL = true;
|
||||
private string _SenderMailAddress = "";
|
||||
private string _SenderName = "";
|
||||
private string _SenderPassword = "";
|
||||
|
||||
internal SmtpClientInfo(string SenderMailAddress, string SenderName, string SenderPassword, string Host, int Port, bool OpenSSL)
|
||||
{
|
||||
_Host = Host;
|
||||
_Port = Port;
|
||||
_OpenSSL = OpenSSL;
|
||||
_SenderMailAddress = SenderMailAddress;
|
||||
_SenderName = SenderName;
|
||||
_SenderPassword = SenderPassword;
|
||||
}
|
||||
}
|
||||
}
|
||||
54
FunGame.Core/Service/MailManager.cs
Normal file
54
FunGame.Core/Service/MailManager.cs
Normal file
@ -0,0 +1,54 @@
|
||||
using System.Net;
|
||||
using System.Net.Mail;
|
||||
using Milimoe.FunGame.Core.Api.Transmittal;
|
||||
using Milimoe.FunGame.Core.Library.Common.Network;
|
||||
using Milimoe.FunGame.Core.Library.Constant;
|
||||
using Milimoe.FunGame.Core.Library.Server;
|
||||
|
||||
namespace Milimoe.FunGame.Core.Service
|
||||
{
|
||||
internal class MailManager
|
||||
{
|
||||
internal SmtpClient? SmtpClient { get; }
|
||||
|
||||
internal static MailSendResult Send(MailSender Sender, MailObject Mail)
|
||||
{
|
||||
SmtpClientInfo Info = Sender.SmtpClientInfo;
|
||||
SmtpClient Smtp = new()
|
||||
{
|
||||
Host = Info.Host,
|
||||
Port = Info.Port,
|
||||
EnableSsl = Info.OpenSSL,
|
||||
DeliveryMethod = SmtpDeliveryMethod.Network,
|
||||
Credentials = new NetworkCredential(Info.SenderMailAddress, Info.SenderPassword)
|
||||
};
|
||||
MailMessage Msg = new()
|
||||
{
|
||||
Subject = Mail.Subject,
|
||||
SubjectEncoding = General.DefaultEncoding,
|
||||
Body = Mail.Body,
|
||||
BodyEncoding = General.DefaultEncoding,
|
||||
From = new MailAddress(Mail.Sender, Mail.SenderName, General.DefaultEncoding),
|
||||
IsBodyHtml = Mail.HTML,
|
||||
Priority = Mail.Priority
|
||||
};
|
||||
foreach (string To in Mail.ToList)
|
||||
{
|
||||
Msg.To.Add(To);
|
||||
}
|
||||
foreach (string CC in Mail.CCList)
|
||||
{
|
||||
Msg.CC.Add(CC);
|
||||
}
|
||||
try
|
||||
{
|
||||
Smtp.Send(Msg);
|
||||
return MailSendResult.Success;
|
||||
}
|
||||
catch
|
||||
{
|
||||
return MailSendResult.Fail;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -22,6 +22,7 @@ namespace Milimoe.FunGame.Desktop.Library
|
||||
public const string Close = ".close";
|
||||
public const string IntoRoom = ".intoroom";
|
||||
public const string Chat = ".chat";
|
||||
public const string QuitRoom = ".quitroom";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user