MailSender改进

This commit is contained in:
Mili 2023-03-03 00:31:33 +08:00
parent 98ac6093b2
commit 736cab93b5
3 changed files with 92 additions and 39 deletions

View File

@ -1,4 +1,4 @@
using Milimoe.FunGame.Core.Interface.Base;
using System.Net.Mail;
using Milimoe.FunGame.Core.Library.Common.Network;
using Milimoe.FunGame.Core.Library.Constant;
using Milimoe.FunGame.Core.Library.Server;
@ -6,23 +6,43 @@ using Milimoe.FunGame.Core.Service;
namespace Milimoe.FunGame.Core.Api.Transmittal
{
public class MailSender
public class MailSender : IDisposable
{
public Guid MailSenderID { get; }
public SmtpClientInfo SmtpClientInfo => _SmtpClientInfo;
public MailSendResult LastestResult => _LastestResult;
public string ErrorMsg => _ErrorMsg;
private SmtpClientInfo _SmtpClientInfo;
private readonly SmtpClientInfo _SmtpClientInfo;
private MailSendResult _LastestResult = MailSendResult.NotSend;
private string _ErrorMsg = "";
public MailSender(string SenderMailAddress, string SenderName, string SenderPassword, string Host, int Port, bool OpenSSL)
{
MailSenderID = Guid.NewGuid();
_SmtpClientInfo = new SmtpClientInfo(SenderMailAddress, SenderName, SenderPassword, Host, Port, OpenSSL);
}
public MailObject CreateMail(string Subject, string Body, MailPriority Priority, bool HTML, string[] ToList, string[] CCList, string[] BCCList)
{
return new MailObject(this, Subject, Body, Priority, HTML, ToList, CCList, BCCList);
}
public MailSendResult Send(MailObject Mail)
{
_LastestResult = MailManager.Send(this, Mail);
_LastestResult = MailManager.Send(this, Mail, out _ErrorMsg);
return _LastestResult;
}
public bool Dispose()
{
return MailManager.Dispose(this);
}
void IDisposable.Dispose()
{
MailManager.Dispose(this);
GC.SuppressFinalize(this);
}
}
}

View File

@ -1,4 +1,5 @@
using System.Net.Mail;
using Milimoe.FunGame.Core.Api.Transmittal;
namespace Milimoe.FunGame.Core.Library.Common.Network
{
@ -12,17 +13,19 @@ namespace Milimoe.FunGame.Core.Library.Common.Network
public bool HTML { get; } = false;
public string[] ToList { get; } = Array.Empty<string>();
public string[] CCList { get; } = Array.Empty<string>();
public string[] BCCList { get; } = Array.Empty<string>();
public MailObject(string Sender, string SenderName, string Subject, string Body, MailPriority Priority, bool HTML, string[] ToList, string[] CcList)
public MailObject(MailSender Sender, string Subject, string Body, MailPriority Priority, bool HTML, string[] ToList, string[] CCList, string[] BCCList)
{
this.Sender = Sender;
this.SenderName = SenderName;
this.Sender = Sender.SmtpClientInfo.SenderMailAddress;
this.SenderName = Sender.SmtpClientInfo.SenderName;
this.Subject = Subject;
this.Body = Body;
this.Priority = Priority;
this.HTML = HTML;
this.ToList = ToList;
this.CCList = CcList;
this.CCList = CCList;
this.BCCList = BCCList;
}
}
}

View File

@ -1,20 +1,29 @@
using System.Net;
using System.Collections;
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.Exception;
using Milimoe.FunGame.Core.Library.Server;
namespace Milimoe.FunGame.Core.Service
{
internal class MailManager
{
internal SmtpClient? SmtpClient { get; }
internal static Hashtable HashClient { get; } = new Hashtable();
internal static MailSendResult Send(MailSender Sender, MailObject Mail)
internal static MailSendResult Send(MailSender Sender, MailObject Mail, out string ErrorMsg)
{
ErrorMsg = "";
try
{
SmtpClientInfo Info = Sender.SmtpClientInfo;
SmtpClient Smtp = new()
SmtpClient Smtp;
Guid MailSenderID = Sender.MailSenderID;
if (!HashClient.ContainsKey(MailSenderID))
{
Smtp = new()
{
Host = Info.Host,
Port = Info.Port,
@ -22,6 +31,9 @@ namespace Milimoe.FunGame.Core.Service
DeliveryMethod = SmtpDeliveryMethod.Network,
Credentials = new NetworkCredential(Info.SenderMailAddress, Info.SenderPassword)
};
HashClient.Add(MailSenderID, Smtp);
}
else Smtp = (SmtpClient)HashClient[MailSenderID]!;
MailMessage Msg = new()
{
Subject = Mail.Subject,
@ -40,14 +52,32 @@ namespace Milimoe.FunGame.Core.Service
{
Msg.CC.Add(CC);
}
try
{
Smtp.Send(Msg);
return MailSendResult.Success;
}
catch (Exception e)
{
ErrorMsg = e.GetErrorInfo();
return MailSendResult.Fail;
}
}
internal static bool Dispose(MailSender Sender)
{
try
{
Guid MailSenderID = Sender.MailSenderID;
if (HashClient.ContainsKey(MailSenderID))
{
((SmtpClient)HashClient[MailSenderID]!).Dispose();
HashClient.Remove(MailSenderID);
return true;
}
return false;
}
catch
{
return MailSendResult.Fail;
return false;
}
}
}