0

I have a window application and i have used the Email Functionality in that and in that the Email settings are in the app.config file.and its like below

<system.net>
<mailSettings>
  <smtp from="email@gmail.com">
    <network host="smtp.gmail.com" port="587" userName="username" password="password" enableSsl="true" />

  </smtp>
</mailSettings>
</system.net>

i want to change using the coding of above section..i have to change through coding in smtp and network section in config file.

Kartik Patel
  • 9,303
  • 12
  • 33
  • 53

2 Answers2

0

i think you are asking how to do this programmatically?

there is an answer here:

Sending email through Gmail SMTP server with C#

also, a good example on the msdn site:

http://msdn.microsoft.com/en-us/library/system.net.mail.smtpclient.aspx

let me know if this is not what you were asking..

Edit: to show answer once question was clear:

How can I read/write app.config settings at runtime without using user settings?

good luck

Community
  • 1
  • 1
felbus
  • 2,639
  • 2
  • 24
  • 30
  • yes this is the way how to send mail..but my question is i want to change above configuration of Email and password in the app.config file using coding..because whatever user want to change they can do.. – Kartik Patel Dec 08 '12 at 07:10
  • 2
    ah ok, I dont thin this is advisable. you should probably keep the email configuration in another data storage (file or database) and let the user change it there as they would any dynamic data or configuration. But, I think it is possible, please see here: http://stackoverflow.com/questions/3638754/how-can-i-read-write-app-config-settings-at-runtime-without-using-user-settings – felbus Dec 08 '12 at 07:26
0

I found the Solution ....

 FileInfo objFileInfo = new FileInfo(Assembly.GetExecutingAssembly().Location + ".config");
            XmlDocument objXmlDoc = new XmlDocument();
            objXmlDoc.Load(objFileInfo.FullName);
            XmlElement objElement = objXmlDoc.DocumentElement;
            XmlNode objAppSettingNode = objElement.SelectSingleNode("appSettings");

            foreach (XmlNode appSetting in objAppSettingNode)
            {
                if (appSetting.Name.Equals("add"))
                {
                    appSetting.Attributes["value"].Value = txtEmailAddress.Text.Trim();
                }

            }

            XmlNode objMailSettingNode = objXmlDoc.DocumentElement.SelectSingleNode("system.net/mailSettings");
            if (objMailSettingNode != null)
            {
                foreach (XmlNode childNode in objMailSettingNode)
                {
                    if (childNode.Name.ToLower().Equals("smtp"))
                    {
                        childNode.Attributes["from"].Value = txtEmailAddress.Text.Trim();
                        foreach (XmlNode networkNode in childNode)
                        {
                            if (networkNode.Name.ToLower().Equals("network"))
                            {
                                networkNode.Attributes["host"].Value = txtSmtp.Text.Trim();
                                networkNode.Attributes["userName"].Value = txtEmailAddress.Text.Split('@')[0].Trim();
                                networkNode.Attributes["password"].Value = txtPassword.Text.Trim();
                                networkNode.Attributes["port"].Value = txtPort.Text.Trim();
                            }
                        }
                    }
                }
            }

            objXmlDoc.Save(objFileInfo.FullName);
            lblErrormsg.Text = string.Empty;
            this.Close();
Kartik Patel
  • 9,303
  • 12
  • 33
  • 53