I want to add the following smtp client configuration in web.config and use it in ac# code behind.
var smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential("feedback.****", "*****"),
};
How to do it?
You have several options...
1- If using the .NET Framework SmtpClient
class you can set up this information in the web.config file to use it as default settings so you don't have to specify them in code...
<system.net>
<mailSettings>
<smtp deliveryMethod="network" from="ben@mailserver.com">
<network
host="localhost"
port="25"
defaultCredentials="true"
/>
</smtp>
</mailSettings>
</system.net>
more info in the MSDN Smpt documentation ...
2- Or, you can set the settings as in the appSettings
element and then retrieve the settings whenever you need to...
<appSettings>
<add key="EmailHost" value="mail.domain.com"/>
<add key="EmailPort" value="25"/>
</appSettings>
var smtp = new SmtpClient
{
Host = ConfigurationManager.AppSettings["EmailHost"],
Port = int.Parse(ConfigurationManager.AppSettings["EmailPort"])
};
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.