UFO ET IT

Web.config에서 system.net/mailSettings/smtp를 읽는 방법

ufoet 2020. 12. 6. 22:25
반응형

Web.config에서 system.net/mailSettings/smtp를 읽는 방법


이것은 내 web.config메일 설정입니다.

<system.net>
    <mailSettings>
      <smtp deliveryMethod="Network" from="smthg@smthg.net">
        <network defaultCredentials="true" host="localhost" port="587" userName="smthg@smthg.net" password="123456"/>
      </smtp>
    </mailSettings>
  </system.net>

그리고 여기에서 값을 읽는 방법이 있습니다. web.config

 var smtp = new System.Net.Mail.SmtpClient();
 var credential = new System.Net.Configuration.SmtpSection().Network;

 string strHost = smtp.Host;
 int port = smtp.Port;
 string strUserName = credential.UserName;
 string strFromPass = credential.Password;

그러나 자격 증명은 항상 null입니다. 이러한 값에 어떻게 액세스 할 수 있습니까?


대답이 수락되지 않았고 다른 사람이 나를 위해 일하지 않았기 때문에 :

using System.Configuration;
using System.Net.Configuration;
// snip...
var smtpSection = (SmtpSection)ConfigurationManager.GetSection("system.net/mailSettings/smtp");
string username = smtpSection.Network.UserName;

를 사용하고 ConfigurationManager수동으로 값을 가져올 필요는 없습니다 . 단순히 인스턴스화하는 SmtpClient것으로 충분합니다.

SmtpClient client = new SmtpClient();

이것은 MSDN이 말하는 것입니다.

이 생성자는 응용 프로그램 또는 컴퓨터 구성 파일의 설정을 사용하여 새 SmtpClient에 대한 호스트, 자격 증명 및 포트 속성을 초기화합니다.

Scott Guthrie는 얼마 전에 그것에 대한 작은 썼습니다 .


구성을 사용하면 다음 행이 표시됩니다.

var smtp = new System.Net.Mail.SmtpClient();

구성된 값을 사용하므로 다시 액세스하여 할당 할 필요가 없습니다.


null값에 관해서 는 구성 값에 잘못 액세스하려고합니다. SmtpSection구성에서 읽는 대신 빈 파일을 만드는 것입니다.

var smtpSection = (SmtpSection)ConfigurationManager.GetSection("<the section name>");
var credentials == smtpSection.Network;

            //You can access the network credentials in the following way.
            //Read the SmtpClient section from the config file    
            var smtp = new System.Net.Mail.SmtpClient();
            //Cast the newtwork credentials in to the NetworkCredential class and use it .
            var credential = (System.Net.NetworkCredential)smtp.Credentials;
            string strHost = smtp.Host;
            int port = smtp.Port;
            string strUserName = credential.UserName;
            string strFromPass = credential.Password;

defaultCredentials = "true"가 설정되어 있으면 사용하지 않을 때 자격 증명이 null이 될 것이라고 생각합니다.

.Send 메서드를 호출하면 이메일이 전송됩니까?

그래서

이것은 내 웹 구성 메일 설정입니다.

<system.net>
   <mailSettings>
      <smtp deliveryMethod="Network" from="smthg@smthg.net">
         <network defaultCredentials="false" host="localhost" port="587"
            userName="smthg@smthg.net" password="123456"/>
      </smtp>
   </mailSettings>
</system.net>

그리고 이것은 CS입니다

SmtpClient smtpClient = new SmtpClient();

string smtpDetails =
    @"
    DeliveryMethod = {0},
    Host = {1},
    PickupDirectoryLocation = {2},
    Port = {3},
    TargetName = {4},
    UseDefaultCredentials = {5}";

Console.WriteLine(smtpDetails,
    smtpClient.DeliveryMethod.ToString(),
    smtpClient.Host,
    smtpClient.PickupDirectoryLocation == null
        ? "Not Set"
        : smtpClient.PickupDirectoryLocation.ToString(),
    smtpClient.Port,
    smtpClient.TargetName,
    smtpClient.UseDefaultCredentials.ToString)
);

응용 프로그램에 System.Net에 대한 참조가 있는지 확인하십시오.


true로 설정하면 자격 증명이 사용되지 않으므로 defaultCredentials = "false"를 설정합니다.

참고 URL : https://stackoverflow.com/questions/13452530/how-to-read-system-net-mailsettings-smtp-from-web-config

반응형