web.config中的smtp设置被忽略

时间:2018-10-12 15:42:02

标签: c# asp.net smtpclient

我正在尝试调整.Net网站中的邮件设置,但我的web.config中的smtp被忽略了,而倾向于在bin文件夹中的dll文件中设置的smtp设置。 我如何在web.config中实施邮件设置,以及为什么dll文件smtp设置优先于web.config设置。生成此代码的程序员(几年前,现在不见了)还在C#控制器.cs文件中包含了smtp设置,我将其更新为正确的smtp设置,但这些设置也被忽略了。倾向于使用dll中的smtp设置。我是.Net网站的新手。 我还使用IIS建立了到该网站的连接,并在控制面板中设置了正确的SMTP设置,但没有任何乐趣。我收到的系统生成的电子邮件都是来自错误的邮件服务器的。 我感谢您的帮助。 谢谢

Web.config邮件设置:

 <system.net>
        <mailSettings>
            <smtp from="mail@mysite.com">
                <network host="smtp.sendgrid.net" port="587" userName="user1" password="pass1" />
            </smtp>
        </mailSettings>
    </system.net>

Web配置已满:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.webServer>
    <directoryBrowse enabled="true" />
    <defaultDocument>
      <files>
        <clear />
        <add value="Default.htm" />
        <add value="Default.asp" />
        <add value="index.htm" />
        <add value="index.pl" />
        <add value="default.html" />
        <add value="Default.cshtml" />
        <add value="index.php" />
        <add value="index.html" />
        <add value="Default.aspx" />
      </files>
    </defaultDocument>
    <httpProtocol>
      <customHeaders>
        <clear />
      </customHeaders>
    </httpProtocol>
  </system.webServer>
  <!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=152368
  -->
  <connectionStrings>

    <add name="ConnectionString" connectionString="Data Source=whatever.com;Initial Catalog=whateverdb;User ID=fakeuser;password=fakepass;Connection Timeout=30;Min Pool Size=20; Max Pool Size=200;" providerName="System.Data.SqlClient" />
  </connectionStrings>
  <appSettings>
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.0">
      <assemblies>
        <add assembly="System.Web.Abstractions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31434BF3856AD364897979E35" />
        <add assembly="System.Web.Helpers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF38343456AD3647878E35" />
        <add assembly="System.Web.Routing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=3GF655651BF3856AD3648787E35" />
        <add assembly="System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3GHGHGH434425856AD387878764E35" />
        <add assembly="System.Web.WebPages, Version=1.0.0.0, Culture=neutral, PublicKeyToken=GHGHGH66676731BF38567676AD364E35" />
      </assemblies>
    </compilation>
    <authentication mode="Forms">
      <forms loginUrl="~/Account/LogOn" timeout="1200000" />
    </authentication>
    <membership>
      <providers>
        <clear />
        <add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="ApplicationServices" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" applicationName="/" />
      </providers>
    </membership>
    <profile>
      <providers>
        <clear />
        <add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="ApplicationServices" applicationName="/" />
      </providers>
    </profile>
    <roleManager enabled="false">
      <providers>
        <clear />
        <add name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider" connectionStringName="ApplicationServices" applicationName="/" />
        <add name="AspNetWindowsTokenRoleProvider" type="System.Web.Security.WindowsTokenRoleProvider" applicationName="/" />
      </providers>
    </roleManager>
    <pages>
      <namespaces>
        <add namespace="System.Web.Helpers" />
        <add namespace="System.Web.Mvc" />
        <add namespace="System.Web.Mvc.Ajax" />
        <add namespace="System.Web.Mvc.Html" />
        <add namespace="System.Web.Routing" />
        <add namespace="System.Web.WebPages" />
      </namespaces>
    </pages>
    <customErrors mode="Off" />
    <trust level="Full" />
  </system.web>
  <system.webServer>
    <validation validateIntegratedModeConfiguration="false" />
    <modules runAllManagedModulesForAllRequests="true" />
  </system.webServer>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
  <system.net>
    <mailSettings>
    <smtp from="mail@mysite.com">
    <network host="smtp.sendgrid.net" port="587" userName="user1" password="pass1" />
    </smtp>
    </mailSettings>
    </system.net>
</configuration>
<!--ProjectGuid: 80e45822-7574-4225-9e47-31f80GHGHG5567678545ea-->

.cs代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Data.SqlClient;
using System.Security.Cryptography;
using System.IO;
using System.Net.Mail;
using System.Timers;

....

public bool SendEmail(string Semailid, string encrypted, string EmailID, string PersonsName, string EmailSubject, string mycourse, string encryptStudent,string Certificettemplete)
        {
            try
            {
                MailMessage Msg = new MailMessage();
                // Sender e-mail address.
                Msg.From = new MailAddress(EmailID);
                // Recipient e-mail address.
                Msg.To.Add(Semailid);
                Msg.Subject = EmailSubject;
                Msg.IsBodyHtml = true;
                Msg.Body = Certificettemplete;

                SmtpClient smtp = new SmtpClient();
                smtp.Host = "smtp.sendgrid.net"; 
                smtp.Port = 25;
                smtp.Credentials = new System.Net.NetworkCredential("user", "pass");
                smtp.EnableSsl = false;
                smtp.Send(Msg);
                Msg = null;
                return true;
            }
            catch (Exception ex)
            {
                throw ex;
                //return false;
            }
        }

2 个答案:

答案 0 :(得分:1)

您的方法具有硬编码的值,因此当前配置不相关。

我的建议:

  1. 如果包含SendMail的类特定于SMTP,请添加/更新一个接受SMTP地址,端口,用户名和密码的构造函数。将它们保存到类中的私有变量中。如果该类是通用实用程序类,则将功能转移到特定的SMTP类中,然后执行上述操作。
  2. 将您的硬编码引用更改为SendEmail中的四个字段,以使用类中的变量
  3. 更改实例化(“ new SMTP()”)以传入从配置中获取的四个参数。*

*如果您到处都在调用SMTP,则在程序开始时获取该值并根据需要传递它可能会很有用。

答案 1 :(得分:0)

如果省略这些行,它将使用web.config中的设置

smtp.Host = "smtp.sendgrid.net"; 
smtp.Port = 25;
smtp.Credentials = new System.Net.NetworkCredential("user", "pass");
smtp.EnableSsl = false;