如何为 ASP.NET MVC 设置电子邮件或通知服务
如何创建可以模拟和单元测试的电子邮件发送通知服务类?
我的服务位于另一层,即类库。我试图不导入 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您不想导入 System.Net.Mail 库,则必须使用接口。请注意,这对您的单元测试并没有太大帮助
,然后在您的 EmailNotificationService 类中,您可以添加以下属性或在构造函数中传递 IEmailSender,
您的 Notify 方法将变成
您将创建一个实现 IEmailSender 的具体类界面
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
and then in your EmailNotificationService class you can add the following property or pass in the IEmailSender in your constructor
your Notify method would become
you would then create a concrete class that implements the IEmailSender interface