如何在不对NetworkCredentials进行硬编码的情况下使用System.Net.Mail? [英] How to use System.Net.Mail without hard-coding NetworkCredentials?

查看:94
本文介绍了如何在不对NetworkCredentials进行硬编码的情况下使用System.Net.Mail?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个正在尝试的程序找出我该怎么做.目前,我正在使用System.Net.Mail发送smtp消息.我们的smtp服务器(办公室 365)要求进行身份验证才能发送外发邮件.

I have a program I'm working on trying to find out how I could do this. Currently I'm using System.Net.Mail to send smtp messages. Our smtp server (office 365) requires authentication to send outgoing mail.

当前通过

NetworkCredential basicCredential = new NetworkCredential("<<username>>", strPassword);

client.Credentials = basicCredential;
client.EnableSsl = true;
client.Send(message);

它可以工作,但是硬编码用户名和密码是否安全C#代码?什么是最佳实践

非常感谢Deepak

Many Thanks Deepak

推荐答案

在我们的应用程序中,我们根本不对SMTP访问进行编码.相反,我们为此使用配置文件.

in our applications, we do not code the SMTP access at all. Instead we use the configuration file for this.

您可以简单地创建SmtpClient实例,而无需任何此C#配置.在这种情况下,应用程序将从配置文件中读取所需的配置.

You could simply create the SmtpClient instance without any of this C# configuration. In such a case, the application is reading the required configuration from the configuration file.

那样,它完全取决于管理员来配置应用程序.而且也很不错:在测试环境中,我们不需要发送电子邮件-我们只需使用提取目录来检查电子邮件是否正确生成.

That way it completly depends on the administrator to configure the application. And also very nice: Inside the test environment we do not need to send emails - we simply use a pickup directory to check that an email was generated correctly.

以诚挚的问候,

Konrad


这篇关于如何在不对NetworkCredentials进行硬编码的情况下使用System.Net.Mail?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆