gpt4 book ai didi

c# - ConfigurationManager.AppSettings ["SMTP"];空异常

转载 作者:太空宇宙 更新时间:2023-11-03 19:29:35 25 4
gpt4 key购买 nike

我用这个代码发送了一封电子邮件。但我不明白那个 configurationManager 做了什么,以及为什么它给我异常(exception)。这是完整的代码:

        MailMessage mail = new MailMessage();

mail.To.Add("makovetskiyd@yahoo.co.uk");

mail.From = new MailAddress("makovetskiyd@yahoo.co.uk");

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 服务器..或类似的东西

最佳答案

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

<system.net>
<mailSettings>
<smtp from="info@example.com">
<network host="localhost" port="25" defaultCredentials="false">
</smtp>
</mailSettings>
</system.net>

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

关于c# - ConfigurationManager.AppSettings ["SMTP"];空异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5946995/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com