有沒有辦法從web.config設置EnableSSL?如何在Web.config中爲SmtpClient啓用SSL
我可以在代碼中設置此屬性,但這不適用於簡單郵件Web事件和其他使用默認Smtp服務器的類。有任何想法嗎?
有沒有辦法從web.config設置EnableSSL?如何在Web.config中爲SmtpClient啓用SSL
我可以在代碼中設置此屬性,但這不適用於簡單郵件Web事件和其他使用默認Smtp服務器的類。有任何想法嗎?
對於.NET 3和更早的:不能。你必須手工管理它。
對於.NET 4:您可以。
見http://theoldsewingfactory.com/2011/01/06/enable-ssl-in-web-config-for-smtpclient/
<configuration>
<system.net>
<mailSettings>
<smtp deliveryMethod=」network」>
<network host="localhost"
port="25"
enableSsl="true"
defaultCredentials="true" />
</smtp>
</mailSettings>
</system.net>
</configuration>
啊,有辦法做到這一點的「忘記密碼」建在.NET中的登錄控件雖然。
瑞安
我有一個骯髒的解決辦法(直到.NET 4.0出來)。而不是changin我的代碼它依賴於使用的端口來確定是否需要SSL。
var client = new SmtpClient();
client.EnableSsl = client.Port == 587 || client.Port == 465;
// This could also work
//client.EnableSsl = client.Port != 25;
我說這是一個骯髒的黑客攻擊,但它適用於我們遇到的不同配置。
只需擴展該類並設置EnableSsl = true並使用該類。
這適用於我在.net 4
E.G.在web.config中
network host="somesmtpserver" userName="[email protected]"
password="whatever" port="25" enableSsl="true"
我認爲在MailSettingsSectionGroup
有一個錯誤。請參見下面的代碼:
Configuration configurationFile = WebConfigurationManager.OpenWebConfiguration("~/web.config");
var mailSettings = configurationFile.GetSectionGroup("system.net/mailSettings") as MailSettingsSectionGroup;
_smtpClient.Host = mailSettings.Smtp.Network.Host;
_smtpClient.Port = mailSettings.Smtp.Network.Port;
_smtpClient.EnableSsl = mailSettings.Smtp.Network.**EnableSsl**;
_smtpClient.Credentials = new System.Net.NetworkCredential(mailSettings.Smtp.Network.UserName, mailSettings.Smtp.Network.Password);
_smtpClient.UseDefaultCredentials = false;
看來,EnableSsl
因爲當我運行和調試這一點,我可以看到的值不存在爲網絡下的屬性,但不能代碼編譯由於缺少ExtensionMethod。
我看來這個班是封閉的,所以我做了一個手動擴展。我以爲我會在這裏爲他人提供。希望它對其他人有用。
/// <summary>
/// OldSchool extension of SmtpNetWorkElement, since it's sealed.
/// </summary>
public class SmtpNetworkElementEx
{
private readonly SmtpNetworkElement m_SmtpNetWorkElement;
/// <summary>
/// Initializes a new instance of the <see cref="SmtpNetworkElementEx"/> class.
/// </summary>
public SmtpNetworkElementEx()
{
Configuration configurationFile = WebConfigurationManager.OpenWebConfiguration("~/web.config");
var mailSettings = configurationFile.GetSectionGroup("system.net/mailSettings") as MailSettingsSectionGroup;
if (mailSettings == null)
return;
m_SmtpNetWorkElement = mailSettings.Smtp.Network;
}
public string Host { get { return m_SmtpNetWorkElement.Host; } }
public bool DefaultCredentials { get { return m_SmtpNetWorkElement.DefaultCredentials; } }
public string ClientDomain { get { return m_SmtpNetWorkElement.ClientDomain; } }
public string TargetName { get { return m_SmtpNetWorkElement.TargetName; } }
public int Port { get { return m_SmtpNetWorkElement.Port; } }
public string UserName { get { return m_SmtpNetWorkElement.UserName; } }
public string Password { get { return m_SmtpNetWorkElement.Password; } }
public bool EnableSsl { get { return Convert.ToBoolean(m_SmtpNetWorkElement.ElementInformation.Properties["enableSsl"].Value); } }
}
用這樣的方式:
var smtpSettings = new SmtpNetworkElementEx();
_smtpClient.Host = smtpSettings.Host;
_smtpClient.Port = smtpSettings.Port;
_smtpClient.EnableSsl = smtpSettings.EnableSsl;
_smtpClient.Credentials = new System.Net.NetworkCredential(smtpSettings.UserName, smtpSettings.Password);
賈爾斯·羅伯茨1月18日在'12 18:01說
這種方式更適合我進去。淨4
E.G.在web.config中
network host="somesmtpserver" userName="[email protected]"
password="whatever" port="25" enableSsl="true"
端口25不是SSL端口。端口25是默認的SMTP端口。此外,web.config代碼部分填寫完畢。代碼應該是
<system.net>
<mailSettings>
<smtp deliveryMethod="Network" from="[email protected]">
<network host="smtp.gmail.com"
userName="[email protected]"
password="********"
port="587"
defaultCredentials="true"
enableSsl="true" />
</smtp>
</mailSettings>
</system.net>
上面的這個設置比原來的web.config代碼更準確。我不知道女巫的方法是更好的。使用web.config或使用代碼隱藏頁面發送電子郵件。無論使用巫婆方法,您都必須修改代碼隱藏文件。我這樣說是因爲你必須連接From,Subject和Body文本框。我認爲理所當然希望通過aspx網頁發送消息的最終結果
從.net 4.0開始,您現在可以在webconfig中設置enableSsl:http://theoldsewingfactory.com/2011/ 01/06/enable-ssl-in-web-config-for-smtpclient/ – ilivewithian 2011-01-06 20:53:38
@ilivewithian不,你不能。至少我不能。 – 2011-04-28 00:29:38
@ eKek0,作爲接受的答案,您能否更新文本以表示您可以在.net 4中使用?請參閱ilivewithian的評論以供參考。 – 2012-01-18 18:05:41