使用 C# 从 Web.Config 文件访问 SMTP 邮件设置

发布于 2024-11-19 20:55:40 字数 555 浏览 3 评论 0原文

需要阅读 web.config 文件中 system.net 部分下定义的 SMTP 电子邮件设置。

以下是 web.config 文件中定义的 SMTP 电子邮件设置示例: (下节)

<system.net>
<mailSettings>
<smtp deliveryMethod="Network" from="[email protected]">
<network defaultCredentials="true" host="localhost" port="25" userName="user” password="testPassword"/>
</smtp>
</mailSettings>
</system.net>

如何使用 c# 访问 SMTP 邮件设置

Need to read my SMTP email settings defined under system.net section in my web.config file.

Below is one example of SMTP email setting defined in web.config file:
(Under Section)

<system.net>
<mailSettings>
<smtp deliveryMethod="Network" from="[email protected]">
<network defaultCredentials="true" host="localhost" port="25" userName="user” password="testPassword"/>
</smtp>
</mailSettings>
</system.net>

How to Access the SMTP Mail Setting by using c#

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

遗失的美好 2024-11-26 20:55:40

只需使用 System.Net.Mail 类即可发送电子邮件。它会自动从您的 web.config 中获取邮件设置。

Just use the System.Net.Mail classes to send your e-mails. It will automagically pick-up the Mail setting from your web.config.

禾厶谷欠 2024-11-26 20:55:40

您可以使用 WebConfigurationManager:

Configuration configurationFile = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
MailSettingsSectionGroup mailSettings = configurationFile.GetSectionGroup("system.net/mailSettings") as MailSettingsSectionGroup;

Response.Write(mailSettings.Smtp.Network.Host);

You can use the WebConfigurationManager:

Configuration configurationFile = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
MailSettingsSectionGroup mailSettings = configurationFile.GetSectionGroup("system.net/mailSettings") as MailSettingsSectionGroup;

Response.Write(mailSettings.Smtp.Network.Host);
请叫√我孤独 2024-11-26 20:55:40

相关...如果您同时从网站和应用程序进行访问,则此代码会派上用场。

Configuration config;

bool isWebApp = HttpRuntime.AppDomainAppId != null;

if (isWebApp)
{
    config = WebConfigurationManager.OpenWebConfiguration(HttpContext.Current.Request.ApplicationPath);
}
else
{
    config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
}

var mailSettings = config.GetSectionGroup("system.net/mailSettings") as MailSettingsSectionGroup;    

Related...If you're accessing from both a website and an application this code can come in handy.

Configuration config;

bool isWebApp = HttpRuntime.AppDomainAppId != null;

if (isWebApp)
{
    config = WebConfigurationManager.OpenWebConfiguration(HttpContext.Current.Request.ApplicationPath);
}
else
{
    config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
}

var mailSettings = config.GetSectionGroup("system.net/mailSettings") as MailSettingsSectionGroup;    
流绪微梦 2024-11-26 20:55:40

您可以使用此代码

Response.Write(ConfigurationManager.GetSection("system.net/mailSettings/smtp").Network.UserName)
Response.Write(ConfigurationManager.GetSection("system.net/mailSettings/smtp").Network.Host)
Response.Write(ConfigurationManager.GetSection("system.net/mailSettings/smtp").Network.Password)

You can use this code

Response.Write(ConfigurationManager.GetSection("system.net/mailSettings/smtp").Network.UserName)
Response.Write(ConfigurationManager.GetSection("system.net/mailSettings/smtp").Network.Host)
Response.Write(ConfigurationManager.GetSection("system.net/mailSettings/smtp").Network.Password)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文