C# 如何为SmtpException创建函数的重载版本?

C# 如何为SmtpException创建函数的重载版本?,c#,C#,我有以下发送电子邮件的方法和包装异常的方法: public void SendEmail(SendEmailRequest emailRequest) { ... catch (SmtpFailedRecipientsException e) { var failedRecipients = e.InnerExceptions.Select(x => x.FailedRecipient); LogAndReThrowWithValid

我有以下发送电子邮件的方法和包装异常的方法:

public void SendEmail(SendEmailRequest emailRequest)
{
    ...
    catch (SmtpFailedRecipientsException e)
    {
        var failedRecipients = e.InnerExceptions.Select(x => x.FailedRecipient);
        LogAndReThrowWithValidMessage(e, EmailsLocalization.EmailDeliveryFailed, emailRequest.Subject, serverSettings, failedRecipients);
    }
    catch (SmtpFailedRecipientException e)
    {
        LogAndReThrowWithValidMessage(e, EmailsLocalization.EmailDeliveryFailed, emailRequest.Subject, serverSettings, new[] { e.FailedRecipient });
    }
    catch (SmtpException e)
    {
        LogAndReThrowWithValidMessage(e, EmailsLocalization.EmailSendFailedInvalidSettings, emailRequest.Subject, serverSettings);
    }
    catch (Exception e)
    {
         LogAndReThrowWithValidMessage(e, EmailsLocalization.EmailSendFailedGeneralIssues, emailRequest.Subject, serverSettings);
    }
    ...
}

private static void LogAndReThrowWithValidMessage(Exception e, string message, string logSubject, SmtpServerSettings logSettings, IEnumerable<string> failedRecipients = null)
{
    _logger.ErrorException(e,
        "Exception while trying to send message with subject '{0}' using SMTP server {1}:{2}",
         logSubject,
         logSettings.SmtpServerName,
         logSettings.SmtpPort);

    var sendingFailedException = new EmailSendingFailedException(message, e);
    var smtpException = e as SmtpException;
    if (smtpException != null)
    {
        var isGmailServerName = logSettings.SmtpServerName.Contains("gmail.com");
        if (isGmailServerName && smtpException.InnerException.Message.Contains("net_io_connectionclosed"))
        {
            sendingFailedException = new EmailSendingFailedException(EmailsLocalization.EmailSendFailedWrongPortNumber, e);
        }

        // Wrapping authentication exceptions
        if (smtpException.StatusCode == SmtpStatusCode.MustIssueStartTlsFirst || 
            smtpException.Message.Contains("AUTH"))
        {
            sendingFailedException = isGmailServerName ? 
                    new EmailSendingFailedException(EmailsLocalization.EmailSendFailedAuthenticationProblemForGmail, e) : 
                    new EmailSendingFailedException(EmailsLocalization.EmailSendFailedAuthenticationProblem, e);
        }

        // According to Yahoo's spam policy
        if (smtpException.StatusCode == SmtpStatusCode.MailboxNameNotAllowed)
        {
            sendingFailedException = new EmailSendingFailedException(EmailsLocalization.EmailSendFailedWrongFromAddress, e);
        }
    }

    if (failedRecipients != null)
    {
        sendingFailedException.FailedRecipients = failedRecipients;
    }

    throw sendingFailedException;
}
public void sendmail(sendmailrequest emailRequest)
{
...
捕获(SMTPFailedRecipientSexcexception)
{
var failedRecipients=e.InnerExceptions.Select(x=>x.FailedRecipient);
LogandRetrowWithValidMessage(e,EmailsLocalization.EmailDeliveryFailed,emailRequest.Subject,serverSettings,failedRecipients);
}
捕获(SmtpFailedRecipientException e)
{
LogandRetrowWithValidMessage(e,EmailsLocalization.EmailDeliveryFailed,emailRequest.Subject,serverSettings,new[]{e.FailedRecipient});
}
捕获(SMTPE异常)
{
LogandRetrowWithValidMessage(e,EmailsLocalization.EmailSendFailedValidSettings,emailRequest.Subject,serverSettings);
}
捕获(例外e)
{
LogandRetrowWithValidMessage(e,EmailsLocalization.EmailSendFailedGeneralises,emailRequest.Subject,serverSettings);
}
...
}
私有静态无效日志和ThrowWithValidMessage(异常e、字符串消息、字符串日志主题、SmtpServerSettings日志设置、IEnumerable failedRecipients=null)
{
_记录器错误异常(e,
“尝试使用SMTP服务器{1}:{2}发送主题为{0}的邮件时出现异常”,
日志主题,
logSettings.SmtpServerName,
logSettings.SmtpPort);
var sendingFailedException=新的EmailSendingFailedException(消息,e);
var smtpException=e作为smtpException;
if(smtpException!=null)
{
var isGmailServerName=logSettings.SmtpServerName.Contains(“gmail.com”);
if(isGmailServerName&&smtpException.InnerException.Message.Contains(“net\u io\u connectionclosed”))
{
sendingFailedException=新的EmailSendingFailedException(EmailsLocalization.EmailSendFailedErrorPortNumber,e);
}
//包装身份验证异常
如果(smtpException.StatusCode==SmtpStatusCode.MustIssueStattlsFirst | |
smtpException.Message.Contains(“AUTH”))
{
sendingFailedException=isGmailServerName?
新的EmailSendingFailedException(EmailsLocalization.EmailSendFailedAuthenticationProblemForMail,e):
新的EmailSendingFailedException(EmailsLocalization.EmailSendFailedAuthenticationProblem,e);
}
//根据雅虎的垃圾邮件政策
如果(smtpException.StatusCode==SmtpStatusCode.MailboxNameNotAllowed)
{
sendingFailedException=新的EmailSendingFailedException(EmailsLocalization.EmailSendFailedErrorFromAddress,e);
}
}
if(failedRecipients!=null)
{
sendingFailedException.FailedRecipients=FailedRecipients;
}
抛出发送失败异常;
}

如何为将实现此逻辑的
SmtpException
创建函数的重载版本,然后调用原始方法记录ant wrap。另外,现在我的优化消息在日志中丢失了,如何修复它?

您能设计一个继承自
SmtpException
的类吗?然后将要发送的代码放在构造函数的末尾,这样在创建它时,它会发送一个副本?它真的需要重载吗?这将是困难的,因为
SmtpException
也是
例外
,因此可以用任何一种方法履行合同。为什么不换个名字呢?i、 e.
LogAndReThrowSmtpException
,然后在完成该方法的工作后调用
LogAndReThrowWithValidMessage
。好主意。但是你所说的“这样就可以用任何一种方法履行合同”是什么意思呢?