简体   繁体   English

将 email 凭证存储在 web.config 文件中

[英]Store email credential in web.config file

I am trying to store email credential from Startup.cs file to 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;

And I create in my app.config file我在我的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>

The reason why I do that is that I want to publish this application live, and maybe in future when I change sender or receiver email address I don't want to compile application again and publish it.我这样做的原因是我想实时发布这个应用程序,也许将来当我更改senderreceiver email 地址时,我不想再次编译应用程序并发布它。

It is easy way to store everything in app.config and read from there.将所有内容存储在 app.config 中并从那里读取是一种简单的方法。

Any suggestion how to do this?任何建议如何做到这一点?

I'm not sure if this will work for you or not but I was able to store SMTP information inside the app.config file for one of my old C# applications.我不确定这是否适合您,但我能够将 SMTP 信息存储在我的旧 C# 应用程序之一的 app.config 文件中。

However, the To: address was not stored as part of the credentials so I stored those information in a local SQLite file.但是,To: 地址并未作为凭证的一部分存储,因此我将这些信息存储在本地 SQLite 文件中。

To retrieve the settings you just need to initialize a SmtpClient object which will read from the local config file.要检索设置,您只需初始化将从本地配置文件读取的SmtpClient object。

Hope this helps.希望这可以帮助。

        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");
            }
        }

As I have understood, you want to store information in App.config file which is pretty easy.据我了解,您希望将信息存储在 App.config 文件中,这很容易。

Step 1步骤1

Add System.Configuration.dll reference to your project if you don't have it already.如果您还没有项目,请将 System.Configuration.dll 引用添加到您的项目。

Project > myAwsomeProject > References > Add Reference > Find System.Configuration.dll项目 > myAwsomeProject > 参考 > 添加参考 > 查找 System.Configuration.dll

(or something like that) and add it to your project. (或类似的东西)并将其添加到您的项目中。

Step 2第2步

import the System.Configuration.dll using:使用以下命令导入 System.Configuration.dll:

using System.Configuration;

Then create a function or write the code in the third step under your main();然后创建一个 function 或者在你的 main() 下编写第三步的代码;

Step 3第 3 步

Create an instance of the Configuration class that refers to your App.config file创建引用您的 App.config 文件的配置 class 的实例

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

Step 4第4步

Use the following syntax to read from your App.config file使用以下语法从 App.config 文件中读取

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

in which you would replace the "mykey" value with the corresponding key name.您将在其中将“mykey”值替换为相应的键名。 For example in your case Username or Password例如,在您的情况下,用户名或密码

Step 5第 5 步

Use the following syntax to write to/change value of a key使用以下语法写入/更改键的值

config.AppSettings.Settings["mykey"].Value = "value";

"mykey" is the key and "value" is the desired value “mykey”是键,“value”是所需的值

Do NOT forget to save the configuration file after you write to it or after all the operations are done!!!写入配置文件或完成所有操作后不要忘记保存配置文件!!!

config.Save();

Step 6 (Optional)第 6 步(可选)

You can also add and remove keys to your App.Config file您还可以在 App.Config 文件中添加和删除密钥

config.AppSettings.Settings.Add("newKey", "value");         
config.AppSettings.Settings.Remove("mykey");

Again after each operation concerning the modification of the file.再次在有关文件修改的每个操作之后。 Save the file保存文件

config.Save();

Important Note重要的提示

The app.config file is not the one you see in your solution explorer in visual studio. app.config 文件不是您在 Visual Studio 的解决方案资源管理器中看到的文件。 The one in the solution explorer will not be modified even after it is saved.解决方案资源管理器中的那个即使保存后也不会被修改。

The app.config that you will be using and modifying will be located in C:\Users\yourUsername\source\repos\yourAwsomeProject\yourAwsomeProject\bin\Debug您将使用和修改的 app.config 将位于 C:\Users\yourUsername\source\repos\yourAwsomeProject\yourAwsomeProject\bin\Debug

or if it is in release mode C:\Users\yourUsername\source\repos\yourAwsomeProject\yourAwsomeProject\bin\Release或者如果它处于发布模式 C:\Users\yourUsername\source\repos\yourAwsomeProject\yourAwsomeProject\bin\Release

or if you are using a custom project path或者如果您使用的是自定义项目路径

\Path\To\your\Awsome\Project\yourAwsomeProject\bin\Release

and the file will be named as该文件将被命名为

yourAwsomeProject.exe.config

(Correct me if I am wrong or misunderstood the question) (如果我错了或误解了问题,请纠正我)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM