简体   繁体   English

将用户名/密码传递给SmtpClient(.NET)的正确方法

[英]Proper way to pass username/password to SmtpClient (.NET)

The question is too simple, but still appreciate the short answer. 问题太简单了,但仍然很欣赏简短的回答。 I would like the SmtpClient to fetch username/password from the App.config file. 我希望SmtpClientApp.config文件中获取用户名/密码。 From the MSDN/schema I've figured out that the proper file (excerpt) should look like: 从MSDN /架构我发现正确的文件(摘录)应该是这样的:

  <system.net>
    <mailSettings>
      <smtp from="foo@bar.com">
        <network
          host="mail.bar.com"
          port="25"
          userName="foouser"
          password="barpassword"
        />
      </smtp>
    </mailSettings>
  </system.net>

I am trying to find the proper API to call, when initializing SmtpClient state, so that mail and password will be nicely fetched from the XML: 我正在尝试在初始化SmtpClient状态时找到适当的API来调用,以便从XML中很好地获取邮件和密码:

  var client = new SmtpClient( ... ); // how to fetch the servername?
  client.Credentials = new NetworkCredential( ... , ... ); // how to fetch user/pass
  client.Send(message);

Is there a proper/dedicated way to fetch servername , user , password or should I just call the "regular" API like ConfigurationManager.AppSettings["server"] ? 是否有适当/专用的方式来获取servernameuserpassword或者我应该像ConfigurationManager.AppSettings["server"]一样调用“常规”API?

Nothing special is needed, just initialize and send :) 没有什么特别需要,只需初始化并发送:)

SmtpClient client = new SmtpClient();
client.Send(mymessagehere);

That is all, it will pull from the config. 这就是全部,它将从配置中拉出来。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在.NET中设置SmtpClient object的用户名和密码? - How to set username and password for SmtpClient object in .NET? 正确的方法来获取Windows用户名 - Proper way to get the windows UserName 将用户名和密码传递到 json url - Pass username and password into json url SmtpClient-什么是适当的生存期? - SmtpClient - What is proper lifetime? 在ASP.NET WebForms中将服务器变量传递给客户端的正确方法 - Proper way to pass server variables to client in ASP.NET WebForms 在使用SmtpClient发送电子邮件之前,如何验证用户名和密码是否匹配? - How can I verify if the username and password matches before sending email using SmtpClient? 这是传递参数的正确方法吗? - Is this proper way to pass parameters? 我能够将用户名和密码传递给Web服务,但这不是我需要防止未经授权的用户访问的方式 - I was able to pass username and password to webservice, but its not the way i need to secure from unauthorized user 将用户名/密码从Windows窗体应用程序传递到ASP.NET Web应用程序 - Pass username/password from Windows Forms application to an ASP.NET web application 如何运行应用程序并将用户名和密码传递给该应用程序 - how to run an application and pass username and password to that application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM