ConfigurationManager.AppSettings[“SMTP”];空异常

发布于 2024-11-06 03:14:36 字数 900 浏览 0 评论 0原文

我用这个代码发送电子邮件。但我不明白该配置管理器的作用,以及为什么它给了我例外。这是完整的代码:

        MailMessage mail = new MailMessage();

    mail.To.Add("[email protected]");

    mail.From = new MailAddress("[email protected]");

    mail.Subject = "Test Email";

    string Body = "Welcome to CodeDigest.Com!!";

    mail.Body = Body;

    SmtpClient smtp = new SmtpClient();

    smtp.Host = ConfigurationManager.AppSettings["SMTP"];

    smtp.Send(mail);

我还将最后几行更改为: smtp.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;

        smtp.Send(mail);

但它仍然会显示一个错误,说它没有找到 IIS 服务器..或类似的东西

i took this code to send an email. But i dont understand what that configurationManager does, and why it gives me the exception. Here is the full code:

        MailMessage mail = new MailMessage();

    mail.To.Add("[email protected]");

    mail.From = new MailAddress("[email protected]");

    mail.Subject = "Test Email";

    string Body = "Welcome to CodeDigest.Com!!";

    mail.Body = Body;

    SmtpClient smtp = new SmtpClient();

    smtp.Host = ConfigurationManager.AppSettings["SMTP"];

    smtp.Send(mail);

i also changed the last lines to this:
smtp.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;

        smtp.Send(mail);

but it would still show a mistake, saying it didnt find IIS server..or something like that

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

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

发布评论

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

评论(3

深居我梦 2024-11-13 03:14:36

您不应使用 AppSettings 和 ConfigurationManager 进行 SMTP 配置。首选方法是通过配置 SMTP > web.config 中的 部分。例如,小型网站的配置可能如下所示:

<system.net>
  <mailSettings>
     <smtp from="[email protected]">
        <network host="localhost" port="25" defaultCredentials="false">
     </smtp>
  </mailSettings>
</system.net>

这将允许您新建一个 SmtpClient 并仅发送消息而无需进一步配置。

You should not use AppSettings an the ConfigurationManager for SMTP configuration. The preferred way is to configure SMTP through the <mailSettings> section in web.config. For example, the configuration for a small website could look like this:

<system.net>
  <mailSettings>
     <smtp from="[email protected]">
        <network host="localhost" port="25" defaultCredentials="false">
     </smtp>
  </mailSettings>
</system.net>

This will allow you to new up an SmtpClient and just send the message without further configuration.

巴黎夜雨 2024-11-13 03:14:36

ConfigurationManager 提供对客户端应用程序配置文件的访问。

我猜错误的原因是您的应用程序的配置文件在应用程序设置部分没有 SMTP 密钥。

<appSettings>
    <add key="SMTP" value="..." />
</appSettings>

ConfigurationManager provides access to configuration files for client applications.

I guess the reason of the error is that the config file of your application does not have a SMTP key in the application settings section.

<appSettings>
    <add key="SMTP" value="..." />
</appSettings>
七颜 2024-11-13 03:14:36

原因是配置文件中没有这样的SMTP配置。我认为你最好检查你的web.config。为了使您的应用程序更强大,您需要添加默认主机,以防配置文件不正确。

string defaultHost = "www.foo.com";
smtp.Host = ConfigurationManager.AppSettings["SMTP"] ?? defaultHost;

The reason is that there is no such SMTP configuration in the config file. I think you'd better check your web.config. And to make your application stronger, you need to add a default host in case of the config file is incorrect.

string defaultHost = "www.foo.com";
smtp.Host = ConfigurationManager.AppSettings["SMTP"] ?? defaultHost;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文