如何在不破坏服务层的情况下使用 MVCMailer?
我正在考虑使用 MVcMailer 制作更好的电子邮件。
然而,我不确定的一件事是如何组织代码。我目前有 2 个项目。一份用于 mvc,一份用于我的存储库和服务层。
我的第二个项目不了解 MVC,我想保持这种状态。
我认为我的 smtp 代码将进入服务层或包装器,然后当我需要发送电子邮件时我会从其他服务层调用它。
那么 MVC 邮件程序适合在哪里呢?我是否在控制器中生成正文,然后将其传递到服务层,再将其传递给我的 smtp 类?
I am looking into using MVcMailer to make nicer emails.
However one thing that I am unsure is how to organize the code. I currently have 2 projects. One for mvc and one for my repos and service layers.
my 2nd project has no knowledge of MVC and I would like to keep it that way.
I am thinking that my smtp code would go into a service layer or wrapper and then I would call it up from other service layers when I need to send emails out.
So where does the MVC mailer fit in? Do I generate the body in the controller then pass it to a serivce layer that passes it to my smtp class?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我的解决方案是在服务层中构建接口,然后我的服务可以使用该接口来获取邮件消息,而创建消息的实现继续驻留在 Web 层中。
接口位于服务层:
实现位于 Web (MVC) 项目中:
最后,在服务层中,邮件程序接口是服务的依赖项:
当它在依赖项注入中连接时,Web.UserMailer 对象是用于 Services.IUserMail 参数来构造 UserCreationService 对象。
我试图使示例保持简单,以便易于理解,但是一旦您在服务层中引用了 IMailMessage,您就可以将其发送到您的 SMTP 代码,而不是像我那样只调用 Send()。出于您的目的,您可能需要进一步充实 IMailMessage 接口,以便访问 MvcMailMessage 类的其他部分。
My solution was to build interfaces in the service layer that my services could then use to get a mail message, while the implementation that creates the message continues to reside in the web layer.
The interfaces are in the service layer:
The implementations are then in the web (MVC) project:
Finally in the service layer, the mailer interface is a dependency of the service:
When it's wired up in dependency injection, a Web.UserMailer object is used for the Services.IUserMail parameter to construct the UserCreationService object.
I've tried to keep the example simple so that it is easy to follow, but once you have a reference to an IMailMessage in the service layer, you can send it to your SMTP code instead of just calling Send() as I do. For your purposes, you may need to flesh out the IMailMessage interface more in order to access other parts of the MvcMailMessage class.
MVCMailer 似乎已经支持发送电子邮件。如果您正确设置配置,它应该能够通过电子邮件发送填充的 MailerViews,而无需额外实施。
我不确定您的第二个项目在您的解决方案中的作用,但有两种可能性:
可能不实用......等等
对于带有“电子邮件发送
来自后台进程”
忘记第二个项目中的 Smtp
使用 Http 只需调用 View
这反过来会调用 MVCMailer
MVCMailer seems to already have support for sending email. If you setup the config correctly it should be able to email the populated MailerViews without additional implementation.
I am not sure of the role of your second project in you solution but here are two possibilities:
Probably not practical ... Wait
for the version with "Email Sending
from a Background Process"
Forget about Smtp from your second project
and using Http just call a View
which in turn would call MVCMailer