2011-01-05 27 views
22

Web.configのsystem.net/mailSettings/smtpセクションにSMTPサーバー、ユーザー名、パスワードを格納する継承コードがあります。中程度の信頼環境のWeb.configからsystem.net/mailSettings/smtpを読み込みます。

それはそうのようにそれらを読み取るために使用:

Configuration c = WebConfigurationManager.OpenWebConfiguration(HttpContext.Current.Request.ApplicationPath); 
MailSettingsSectionGroup settings = (MailSettingsSectionGroup)c.GetSectionGroup("system.net/mailSettings"); 
return settings.Smtp.Network.Host; 

しかし、私はメディアの信頼環境にデプロイするために持っていたとき、これは失敗でした。

のでthis questionからの回答、以下、私はそうのようなGetSection()を使用するように書き直し:

SmtpSection settings = (SmtpSection)ConfigurationManager.GetSection("system.net/mailSettings/smtp"); 
return settings.Network.Host; 

しかし、それはまだ、次のメッセージを表示して、私の中程度の信頼にSecurityExceptionを与えている:

Request for ConfigurationPermission failed while attempting to access configuration section 'system.net/mailSettings/smtp'. To allow all callers to access the data for this section, set section attribute 'requirePermission' equal 'false' in the configuration file where this section is declared.

だから私はこのrequirePermission属性を試しましたが、どこに置くべきか分かりません。

ノードに<のsmtp >ノードを適用すると、ConfigurationErrorが表示されます。「認識できない属性 'requirePermission'」属性名では大文字と小文字が区別されます。

<mailSettings>ノードに適用すると、引き続きSecurityExceptionが発生します。

このコンフィグレーションセクションでプログラム的に中信頼状態になる方法はありますか?または、私はそれをあきらめて、設定を<appSettings>に移動する必要がありますか?

+4

たびI Iを設定すると、 "それを読む" する必要はありませんでしたということでした。新しいSmtpClient()を作成するだけで、web.configの設定が使用されます。 – turtlepick

答えて

27

requirePemission属性は、セキュリティ上の問題が発生しているweb.configの部分と一致するグループ<configSections>になります。

加えて、あなたが実際にメールを送信するためのコードを使用して設定を読む必要はありません - あなたは、単に空白のSmtpClientを使用することができます。

new SmtpClient.Send(MyMailMessage); 

それがデフォルトで設定セクションからの設定を使用して送信されます。えっコーディングの

+8

Hehは、ホストとポートとユーザー名とパスワードを手動で抽出するのではなく、 'new SmtpClient()'を使うだけで完璧に動作します! – Carson63000

2

喜び...これは私には非常にうまく機能魚を肌にする、常に1000年の方法

System.Net.Configuration.SmtpSection smtp = new System.Net.Configuration.SmtpSection(); 
string from = smtp.From; 
//etc 
System.Net.Configuration.SmtpNetworkElement nt = new System.Net.Configuration.SmtpNetworkElement(); 
string host = nt.Host; 
//etc 
+3

これは単に 'null'を返します - あなたのWeb.configの内容を反映していないようです... –

+0

動作しません。それは単に要素を作成し、要素を読み込まない。 –

4

var smtp = new System.Net.Mail.SmtpClient(); 
var host = smtp.Host; 
var ssl = smtp.EnableSsl; 
var port = smtp.Port; 

var credential = new System.Net.Configuration.SmtpSection().Network; 
var username = credential.UserName; 
var password = credential.Password; 
26

SmtpClientはいくつか提案されているように作成できますが、これはちょっと残酷です。セクションを直接読むだけです。

var section = ConfigurationManager.GetSection("system.net/mailSettings/smtp") as SmtpSection; 
var host=section.Network.Host 
+0

userName属性を取得するには:section.Network.UserName –

+0

このプロセス全体に従いましたが、常にnull値を取得していますか?私は何が欠けていますか? – ruud

2

だけメールオブジェクトを作成し、メールのセクションから設定を取得します。

var client = new SmtpClient(); 
var messageSettings = new MailMessage(); 

var host=client.Host; 
//etc... 

var fromAddress=messageSettings.From.Address; 
//etc.. 

設定:

<system.net> 
    <mailSettings> 
     <smtp from="[email protected]" deliveryMethod="Network" > 
     <network host="smtp.mail.yahoo.com" port="587" enableSsl="true" 
      userName="[email protected]" password="xxxxxxx"/> 
     </smtp>  
    </mailSettings> 
    </system.net> 
関連する問題