【发布时间】:2019-03-17 21:12:12
【问题描述】:
我正在尝试调整 .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>
网络配置已满:
<?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;
}
}
【问题讨论】:
-
如果有代码特别说明要使用特定的电子邮件服务器,那么这就是正在使用的。该代码需要修复以使用配置文件中的一组
-
除了您的配置之外,请向我们展示 SMTP 代码。
-
谢谢。我添加了一些额外的代码
-
必须更改代码,因为代码中明确设置的设置将始终覆盖 web.config 文件中的邮件设置。
-
那么可能的第 1 步是编辑 cs 文件删除“使用 System.Net.Mail;”并删除上面引用的 SendEmail 类?我正在制作该网站的副本,因此有空间进行试验。谢谢
标签: c# asp.net smtpclient