如何为 ASP.NET MVC 设置电子邮件或通知服务

发布于 2024-10-12 02:48:43 字数 1369 浏览 3 评论 0原文

如何创建可以模拟和单元测试的电子邮件发送通知服务类?

我的服务位于另一层,即类库。我试图不导入 smtp 客户端,但如果这是不可避免的,那么就没有问题。这就是我现在所拥有的:

public class EmailNotificationService : INotificationService
{
    private readonly EmailNotification _emailNotification;

    public EmailNotificationService(EmailNotification emailNotification)
    {
        _emailNotification = emailNotification;
    }

    public void Notify()
    {
        using (var mail = new MailMessage())
        {
            //If no replyto was passed in the notification, then make it null.
            mail.ReplyTo = string.IsNullOrEmpty(_emailNotification.ReplyTo) ? null : new MailAddress(_emailNotification.ReplyTo);

            mail.To.Add(_emailNotification.To);
            mail.From = _emailNotification.From;
            mail.Subject = _emailNotification.Subject;
            mail.Body = _emailNotification.Body;
            mail.IsBodyHtml = true;

            //this doesn't seem right.
            SmtpClient client = new SmtpClient();
            client.Send(mail);
        }
    }
}

public class EmailNotification
{
    public EmailNotification()
    {
        To = "";
        ReplyTo = "";
        Subject = "";
        Body = "";
    }
    public string To { get; set; }
    public string ReplyTo { get; set; }
    public string Subject { get; set; }
    public string Body { get; set; }

}

How do I create an email sending notification service class that I can mock and unit test?

My service is in another layer which is a class library. I'm trying not to import the smtp client, but if this is unavoidable, then its no problem. This is what I have now:

public class EmailNotificationService : INotificationService
{
    private readonly EmailNotification _emailNotification;

    public EmailNotificationService(EmailNotification emailNotification)
    {
        _emailNotification = emailNotification;
    }

    public void Notify()
    {
        using (var mail = new MailMessage())
        {
            //If no replyto was passed in the notification, then make it null.
            mail.ReplyTo = string.IsNullOrEmpty(_emailNotification.ReplyTo) ? null : new MailAddress(_emailNotification.ReplyTo);

            mail.To.Add(_emailNotification.To);
            mail.From = _emailNotification.From;
            mail.Subject = _emailNotification.Subject;
            mail.Body = _emailNotification.Body;
            mail.IsBodyHtml = true;

            //this doesn't seem right.
            SmtpClient client = new SmtpClient();
            client.Send(mail);
        }
    }
}

public class EmailNotification
{
    public EmailNotification()
    {
        To = "";
        ReplyTo = "";
        Subject = "";
        Body = "";
    }
    public string To { get; set; }
    public string ReplyTo { get; set; }
    public string Subject { get; set; }
    public string Body { get; set; }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你怎么这么可爱啊 2024-10-19 02:48:43

如果您不想导入 System.Net.Mail 库,则必须使用接口。请注意,这对您的单元测试并没有太大帮助

public interface IEmailSender{
     void Send(EmailNotification emailNotification);
}

,然后在您的 EmailNotificationService 类中,您可以添加以下属性或在构造函数中传递 IEmailSender,

private IEmailSender emailSender;

public IEmailSender EmailSender
{
     get{
          if(this.emailSender == null){
               //Initialize new EmailSender using either
               // a factory pattern or inject using IOC 
          }
          return this.emailSender
     }
     set{
          this.emailSender = value;
     }
}

您的 Notify 方法将变成

public void Notify()
{
    EmailSender.Send(_emailNotification);
}

您将创建一个实现 IEmailSender 的具体类界面

public class MyEmailSender: IEmailSender
{
     public void Send(EmailNotification emailNotification)
     {
        using (var mail = new MailMessage())
        {
            //If no replyto was passed in the notification, then make it null.
            mail.ReplyTo = 
                    string.IsNullOrEmpty(_emailNotification.ReplyTo) ? null : 
                    new MailAddress(_emailNotification.ReplyTo);

            mail.To.Add(emailNotification.To);
            mail.From = emailNotification.From;
            mail.Subject = emailNotification.Subject;
            mail.Body = emailNotification.Body;
            mail.IsBodyHtml = true;

            SmtpClient client = new SmtpClient();
            client.Send(mail);
        }
     }
}

If you don't want to import the System.Net.Mail library, you would have to use an interface. Note that this doesn't really help much for your unit testing though

public interface IEmailSender{
     void Send(EmailNotification emailNotification);
}

and then in your EmailNotificationService class you can add the following property or pass in the IEmailSender in your constructor

private IEmailSender emailSender;

public IEmailSender EmailSender
{
     get{
          if(this.emailSender == null){
               //Initialize new EmailSender using either
               // a factory pattern or inject using IOC 
          }
          return this.emailSender
     }
     set{
          this.emailSender = value;
     }
}

your Notify method would become

public void Notify()
{
    EmailSender.Send(_emailNotification);
}

you would then create a concrete class that implements the IEmailSender interface

public class MyEmailSender: IEmailSender
{
     public void Send(EmailNotification emailNotification)
     {
        using (var mail = new MailMessage())
        {
            //If no replyto was passed in the notification, then make it null.
            mail.ReplyTo = 
                    string.IsNullOrEmpty(_emailNotification.ReplyTo) ? null : 
                    new MailAddress(_emailNotification.ReplyTo);

            mail.To.Add(emailNotification.To);
            mail.From = emailNotification.From;
            mail.Subject = emailNotification.Subject;
            mail.Body = emailNotification.Body;
            mail.IsBodyHtml = true;

            SmtpClient client = new SmtpClient();
            client.Send(mail);
        }
     }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文