public ActionResult Smtp(string host, string from = "", bool defaultCredentials = true, bool enableSsl = false, int port = 25, string userName = "", string password = "", string displayName = "") { var xDoc = XDocument.Load(Server.MapPath("~/web.config")); var mailElement = xDoc.Root.Element("system.net").Element("mailSettings"); var smtpElement = new XElement("smtp", new XAttribute("from", from), new XAttribute("deliveryMethod", "Network")); var networkElement = new XElement("network", new XAttribute("port", port), new XAttribute("enableSsl", enableSsl), new XAttribute("defaultCredentials", defaultCredentials)); if (!string.IsNullOrEmpty(host)) { networkElement.Add(new XAttribute("host", host)); } if (!defaultCredentials) { if (!string.IsNullOrEmpty(userName)) { networkElement.Add(new XAttribute("userName", userName)); } if (!string.IsNullOrEmpty(password)) { networkElement.Add(new XAttribute("password", password)); } } smtpElement.Add(networkElement); mailElement.ReplaceNodes(smtpElement); //Set app settings var appSettings = xDoc.Root.Element("appSettings"); var senderEl = appSettings.Elements().FirstOrDefault(e => e.StrAttr("key").Equals("EmailSender")); if (senderEl != null) { senderEl.Attribute("value").SetValue(displayName); } else { senderEl = new XElement("add", new XAttribute("key", "EmailSender"), new XAttribute("value", displayName)); appSettings.Add(senderEl); } xDoc.Save(Server.MapPath("~/web.config")); return(View(SmtpConfig.Read())); }
public ActionResult Smtp() { return(View(SmtpConfig.Read())); }