asp.net net.mail - 在调试模式下将所有电子邮件路由到不同的电子邮件地址
当 System.Web.HttpContext.Current.IsDebuggingEnabled 为 true 时,是否可以让所有电子邮件发送到不同的地址? 或者我是否需要一个新的类来用于到处发送电子邮件?
Is there anyway to have all emails go to a different address when System.Web.HttpContext.Current.IsDebuggingEnabled
is true? Or do I need a new class which I use to send email everywhere?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我将设置一个 SMTP 服务器并让您的 web 应用程序使用它作为其外发邮件服务器(在某种配置中指定)。 对于生产站点,服务器会正常运行,但为了进行调试,您可以配置 SMTP 服务器将所有邮件路由到您选择的地址。 这样做的优点是根本不更改您的应用程序,如果更改行为可以防止您尝试重现的错误发生等,这会很有用。我的公司使用这种方法进行质量检查,并且效果很好。
I'd set up an SMTP server and have your webapp use that as its outgoing mail server (specified in some kind of config). For production sites, the server would behave normally, but for debugging, you can configure an SMTP server to route all mail to your address of choice. This has the advantage of not changing your app at all, which can be useful if changing the behavior prevents bugs from occuring that you're trying to reproduce, etc. My company uses this approach for QA and it works quite well.
您可以将其放在您使用它的任何地方的代码中:
或者只是构建一个简单的包装器来获取电子邮件地址(甚至收件人地址方法的 getter 中的某些内容也可以工作)或整个 smtpclient 或 mailmessage 的包装器班级。
You could either put this in your code everywhere you use it:
Or just build a simple wrapper for the getting of email addresses (even something in the getter of the method for the recipient addresses would work) or a wrapper for the whole smtpclient or mailmessage class.
我必须诚实地说,我使用了很好的旧方法
来阻止我在生产应用程序中浮动调试代码。 我用于数据库访问的方法是使用 dns 条目,即database1.domain.com,在我的开发计算机上的主机文件中,它指向我的本地系统,在我的生产计算机上,它指向数据库服务器。 您可以使用类似的东西来拥有调试邮件服务器和生产邮件服务器,但与上面的第一个解决方案相比,它似乎太过分了。
I have to be honest I use good old
To stop me having debug code floating around in my production apps. An approach I use for database access is to use a dns entry, i.e. database1.domain.com and in the hosts file on my dev machine it points to my local system and on my production machine it points to the database server. You could use something similar to have a debug mail server and a production mail server but it seems overkill compared to the first solution above.
我们使用与 tvanfosson 详细介绍的 MailProxy 方法类似的解决方案。 然而,我们有一些我认为值得一提的差异。 第一个是我们在开发或测试环境中简单地扩展 SmtpClient 类并重写 Send 方法。 我们使用配置文件来确定环境。 然后,我们可以在任何需要发送电子邮件的时候使用这个类。 此类将确定当前登录应用程序的人员,并将生成的所有电子邮件重定向给他们,如果登录用户因某种原因无法使用,则故障转移到标准收件人列表。 如果您有几个人正在测试或开发,这会很有帮助,因为他们只会收到他们生成的电子邮件。 我们还将消息的原始收件人添加到消息正文中。 在测试工作流类型消息以确保它们发送到正确的个人时,这再次很有用。 最后,我发现向邮件主题添加 [DEV] 或 [QA] 标记很有用,这样我就可以在邮件客户端中设置规则来处理所有测试邮件。
We use a similar solution to the MailProxy method detailed by tvanfosson. However we have a few differences that I feel are worth mentioning. The first is that we simply extend the SmtpClient class and override the Send methods when in a development or testing environment. We use a config file to determine the environment. We then use this class anytime we need to send out emails. This class will determine who is currently logged into the application and redirect all emails generated to them, with a failover to a standard list of recipients if the logged in user is not available for whatever reason. This helps if you have several people who are testing or developing as they will only get the emails they generated. We also prepend the original recipients of the message to the body of the message. This again is useful when testing workflow type messages to ensure that they are going to the correct individuals. Finally, I find it's useful to add a [DEV] or [QA] tag to the subject of the message so I can set up a rule in my mail client to handle all testing messages.
我认为,处理此问题的最佳方法是通过邮件客户端的代理类。 创建代理类时,使用一个参数来指示是否在调试模式下运行。 让代理支持与邮件客户端相同的接口,但在调试模式下运行时以静默方式替换传出电子邮件地址。
The best way to handle this, I think, is via a proxy class for the mail client. When you create the proxy class use a parameter to indicate whether to operate in debug mode or not. Have the proxy support the same interface as the mail client, but silently replace the outgoing email addresses when it is operating in debug mode.