簡體   English   中英

將 email 憑證存儲在 web.config 文件中

[英]Store email credential in web.config file

我正在嘗試將 email 憑據從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>

我這樣做的原因是我想實時發布這個應用程序,也許將來當我更改senderreceiver email 地址時,我不想再次編譯應用程序並發布它。

將所有內容存儲在 app.config 中並從那里讀取是一種簡單的方法。

任何建議如何做到這一點?

我不確定這是否適合您,但我能夠將 SMTP 信息存儲在我的舊 C# 應用程序之一的 app.config 文件中。

但是,To: 地址並未作為憑證的一部分存儲,因此我將這些信息存儲在本地 SQLite 文件中。

要檢索設置,您只需初始化將從本地配置文件讀取的SmtpClient object。

希望這可以幫助。

        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;

然后創建一個 function 或者在你的 main() 下編寫第三步的代碼;

第 3 步

創建引用您的 App.config 文件的配置 class 的實例

Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

第4步

使用以下語法從 App.config 文件中讀取

config.AppSettings.Settings["mykey"].Value

您將在其中將“mykey”值替換為相應的鍵名。 例如,在您的情況下,用戶名或密碼

第 5 步

使用以下語法寫入/更改鍵的值

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

(如果我錯了或誤解了問題,請糾正我)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM