将电子邮件凭据存储在 web.config 文件中
Store email credential in web.config file
我正在尝试将电子邮件凭据从 Startup.cs
文件存储到 web.config
文件。
var value = ConfigurationManager.AppSettings["appSettings"];
SmtpClient mySmtpClient = new SmtpClient("smtp.gmail.com");
mySmtpClient.UseDefaultCredentials = false;
System.Net.NetworkCredential basicAuthenticationInfo = new System.Net.NetworkCredential("service@gmail.com", "123456");
mySmtpClient.Credentials = basicAuthenticationInfo;
mySmtpClient.EnableSsl = true;
mySmtpClient.Port = 587;
MailAddress from = new MailAddress("service@gmail.com", "ActiveDirectoryInformation");
MailAddress to = new MailAddress("test@gmail.com");
//MailAddress to = new MailAddress("it@sarajevoosiguranje.ba");
System.Net.Mail.MailMessage myMail = new System.Net.Mail.MailMessage(from, to);
myMail.Subject = "ActiveDirectory";
myMail.SubjectEncoding = System.Text.Encoding.UTF8;
// set body-message and encoding
myMail.Body = @"Ukupno novih korisnika:" + noviKorisnika + "<br>" +
@"Ukupno izmjenjenih korisnika: " + izmjenjenihKorisnika;
myMail.BodyEncoding = System.Text.Encoding.UTF8;
// text or html
myMail.IsBodyHtml = true;
然后我在 app.config
文件中创建
<appSettings>
<add key="webpages:Version" value="1.0.0.0" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
<add key="smtpServer" value="smtp.net" />
<add key="Username" value="service@gmail.com"/>
<add key="Password" value="test123456abc"/>
<add key="EnableSsl" value = "true"/>
<add key="smtpPort" value="587" />
<add key="from" value="service@gmail.com" />
<add key="to" value ="test@gmail.com"></add>
</appSettings>
我这样做的原因是我想实时发布这个应用程序,也许将来当我更改 sender
或 receiver
电子邮件地址时我不想再次编译应用程序并发布。
将所有内容存储在 app.config 中并从那里读取是一种简单的方法。
有什么建议吗?
我不确定这是否适合您,但我能够将 SMTP 信息存储在我的一个旧 C# 应用程序的 app.config 文件中。
但是,收件人:地址并未存储为凭据的一部分,因此我将这些信息存储在本地 SQLite 文件中。
要检索设置,您只需初始化一个 SmtpClient
对象,该对象将从本地配置文件中读取。
希望对您有所帮助。
public static void WriteDefaultSMTPSettingsToConfigurationFile()
{
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
MailSettingsSectionGroup mailSettingsSectionGroup = (MailSettingsSectionGroup)config.GetSectionGroup("system.net/mailSettings");
SmtpSection smtpSection = mailSettingsSectionGroup.Smtp;
if (!smtpSection.ElementInformation.IsPresent)
{
smtpSection.DeliveryFormat = SmtpDeliveryFormat.International;
smtpSection.DeliveryMethod = SmtpDeliveryMethod.Network;
SmtpNetworkElement smtpNetworkElement = smtpSection.Network;
smtpNetworkElement.Host = "localhost";
smtpNetworkElement.Port = 465;
smtpNetworkElement.EnableSsl = true;
smtpNetworkElement.UserName = "username";
smtpNetworkElement.Password = "password";
config.Save();
ConfigurationManager.RefreshSection("system.net/mailSettings");
}
}
据我了解,您想将信息存储在 App.config 文件中,这很容易。
步骤 1
如果您还没有项目,请添加 System.Configuration.dll 引用。
项目 > myAwsomeProject > 参考 > 添加参考 > 查找 System.Configuration.dll
(或类似的东西)并将其添加到您的项目中。
第 2 步
导入 System.Configuration.dll 使用:
using System.Configuration;
然后在你的main()下创建函数或者写第三步的代码;
步骤 3
创建引用您的 App.config 文件的配置 class 实例
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
第 4 步
使用以下语法从您的 App.config 文件中读取
config.AppSettings.Settings["mykey"].Value
其中您将用相应的键名替换 "mykey" 值。例如,在您的情况下,用户名或密码
第 5 步
使用以下语法写入to/change键的值
config.AppSettings.Settings["mykey"].Value = "value";
"mykey" 是键,"value" 是所需的值
写入配置文件或完成所有操作后不要忘记保存配置文件!!!
config.Save();
第 6 步(可选)
您还可以在 App.Config 文件中添加和删除密钥
config.AppSettings.Settings.Add("newKey", "value");
config.AppSettings.Settings.Remove("mykey");
在每次修改文件的操作之后。保存文件
config.Save();
重要提示
app.config 文件不是您在 visual studio 的解决方案资源管理器中看到的文件。解决方案资源管理器中的那个即使保存也不会被修改
您将要使用和修改的 app.config 位于
C:\Users\yourUsername\source\repos\yourAwsomeProject\yourAwsomeProject\bin\Debug
或者如果它处于发布模式
C:\Users\yourUsername\source\repos\yourAwsomeProject\yourAwsomeProject\bin\Release
或者如果您使用的是自定义项目路径
\Path\To\your\Awsome\Project\yourAwsomeProject\bin\Release
文件将被命名为
yourAwsomeProject.exe.config
(如果我错了或误解了问题,请纠正我)
我正在尝试将电子邮件凭据从 Startup.cs
文件存储到 web.config
文件。
var value = ConfigurationManager.AppSettings["appSettings"];
SmtpClient mySmtpClient = new SmtpClient("smtp.gmail.com");
mySmtpClient.UseDefaultCredentials = false;
System.Net.NetworkCredential basicAuthenticationInfo = new System.Net.NetworkCredential("service@gmail.com", "123456");
mySmtpClient.Credentials = basicAuthenticationInfo;
mySmtpClient.EnableSsl = true;
mySmtpClient.Port = 587;
MailAddress from = new MailAddress("service@gmail.com", "ActiveDirectoryInformation");
MailAddress to = new MailAddress("test@gmail.com");
//MailAddress to = new MailAddress("it@sarajevoosiguranje.ba");
System.Net.Mail.MailMessage myMail = new System.Net.Mail.MailMessage(from, to);
myMail.Subject = "ActiveDirectory";
myMail.SubjectEncoding = System.Text.Encoding.UTF8;
// set body-message and encoding
myMail.Body = @"Ukupno novih korisnika:" + noviKorisnika + "<br>" +
@"Ukupno izmjenjenih korisnika: " + izmjenjenihKorisnika;
myMail.BodyEncoding = System.Text.Encoding.UTF8;
// text or html
myMail.IsBodyHtml = true;
然后我在 app.config
文件中创建
<appSettings>
<add key="webpages:Version" value="1.0.0.0" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
<add key="smtpServer" value="smtp.net" />
<add key="Username" value="service@gmail.com"/>
<add key="Password" value="test123456abc"/>
<add key="EnableSsl" value = "true"/>
<add key="smtpPort" value="587" />
<add key="from" value="service@gmail.com" />
<add key="to" value ="test@gmail.com"></add>
</appSettings>
我这样做的原因是我想实时发布这个应用程序,也许将来当我更改 sender
或 receiver
电子邮件地址时我不想再次编译应用程序并发布。
将所有内容存储在 app.config 中并从那里读取是一种简单的方法。
有什么建议吗?
我不确定这是否适合您,但我能够将 SMTP 信息存储在我的一个旧 C# 应用程序的 app.config 文件中。
但是,收件人:地址并未存储为凭据的一部分,因此我将这些信息存储在本地 SQLite 文件中。
要检索设置,您只需初始化一个 SmtpClient
对象,该对象将从本地配置文件中读取。
希望对您有所帮助。
public static void WriteDefaultSMTPSettingsToConfigurationFile()
{
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
MailSettingsSectionGroup mailSettingsSectionGroup = (MailSettingsSectionGroup)config.GetSectionGroup("system.net/mailSettings");
SmtpSection smtpSection = mailSettingsSectionGroup.Smtp;
if (!smtpSection.ElementInformation.IsPresent)
{
smtpSection.DeliveryFormat = SmtpDeliveryFormat.International;
smtpSection.DeliveryMethod = SmtpDeliveryMethod.Network;
SmtpNetworkElement smtpNetworkElement = smtpSection.Network;
smtpNetworkElement.Host = "localhost";
smtpNetworkElement.Port = 465;
smtpNetworkElement.EnableSsl = true;
smtpNetworkElement.UserName = "username";
smtpNetworkElement.Password = "password";
config.Save();
ConfigurationManager.RefreshSection("system.net/mailSettings");
}
}
据我了解,您想将信息存储在 App.config 文件中,这很容易。
步骤 1
如果您还没有项目,请添加 System.Configuration.dll 引用。
项目 > myAwsomeProject > 参考 > 添加参考 > 查找 System.Configuration.dll
(或类似的东西)并将其添加到您的项目中。
第 2 步
导入 System.Configuration.dll 使用:
using System.Configuration;
然后在你的main()下创建函数或者写第三步的代码;
步骤 3
创建引用您的 App.config 文件的配置 class 实例
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
第 4 步
使用以下语法从您的 App.config 文件中读取
config.AppSettings.Settings["mykey"].Value
其中您将用相应的键名替换 "mykey" 值。例如,在您的情况下,用户名或密码
第 5 步
使用以下语法写入to/change键的值
config.AppSettings.Settings["mykey"].Value = "value";
"mykey" 是键,"value" 是所需的值
写入配置文件或完成所有操作后不要忘记保存配置文件!!!
config.Save();
第 6 步(可选)
您还可以在 App.Config 文件中添加和删除密钥
config.AppSettings.Settings.Add("newKey", "value");
config.AppSettings.Settings.Remove("mykey");
在每次修改文件的操作之后。保存文件
config.Save();
重要提示
app.config 文件不是您在 visual studio 的解决方案资源管理器中看到的文件。解决方案资源管理器中的那个即使保存也不会被修改
您将要使用和修改的 app.config 位于 C:\Users\yourUsername\source\repos\yourAwsomeProject\yourAwsomeProject\bin\Debug
或者如果它处于发布模式 C:\Users\yourUsername\source\repos\yourAwsomeProject\yourAwsomeProject\bin\Release
或者如果您使用的是自定义项目路径
\Path\To\your\Awsome\Project\yourAwsomeProject\bin\Release
文件将被命名为
yourAwsomeProject.exe.config
(如果我错了或误解了问题,请纠正我)