用于在不了解服务器配置的情况下发送电子邮件的 C# 代码?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/463176/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 04:24:12  来源:igfitidea点击:

C# code for sending an email without knowing much about the server configuration?

c#emailconfigurationsmtp

提问by Jonathan

Is there a way, in C# code, to send an email without having to know the SMTP server configuration, etc on the server, or have any of that stuff set up?

有没有办法在 C# 代码中发送电子邮件而无需知道服务器上的 SMTP 服务器配置等,或者设置任何这些东西?

The code I'm developing will be deployed to a live server, but I know nothing about the configuration, so I can't predict what the SMTP server will be.

我正在开发的代码将部署到实时服务器,但我对配置一无所知,因此我无法预测 SMTP 服务器将是什么。

采纳答案by Ray Booysen

The best answer is if you know nothing until live, can you move all the settings into web.config? This will allow configuration up until the last minute. Below is some code to dump into your web.config file. I would question as to why you don't have access to this information though

最好的答案是,如果您在直播之前一无所知,您能否将所有设置移动到 web.config 中?这将允许配置直到最后一分钟。下面是一些转储到您的 web.config 文件的代码。我会质疑为什么您无法访问此信息

<system.net>
    <mailSettings>
      <smtp from="[email protected]">
        <network host="SMTP SERVER ADDRESS" port="25"
         userName="USERNAME" password="PASSWORD">
      </smtp>
    </mailSettings>
  </system.net>

回答by Rulas

If your SMTP configuration is correct, just do this:

如果您的 SMTP 配置正确,请执行以下操作:

MailMessage mail = new MailMessage(); 

mail.To = "To"; 
mail.From = "From"; 
mail.Subject = "Subject";     
mail.Body = "Body"; 

SmtpMail.SmtpServer = "localhost"; 
SmtpMail.Send(mail); 

回答by John Sheehan

Add this to your web.config (MSDN reference here):

将此添加到您的 web.config(此处为 MSDN 参考):

<system.net>
    <mailSettings>
        <smtp deliveryMethod="Network" from="[email protected]">
            <network host="localhost" port="25" />
        </smtp>
    </mailSettings>
</system.net>

Using SmtpClient without specifying configuration settings will use the values from the web.config:

使用 SmtpClient 而不指定配置设置将使用 web.config 中的值:

MailMessage msg = new MailMessage(...);
// build message contents
SmtpClient client = new SmtpClient();
client.Send(msg);

回答by Dale Ragan

I answered a question similar to this not to long ago. You can view it here. Using papercut, you can test your application without knowing or using the actual production smtp server.

不久前我回答了一个类似的问题。你可以在这里查看。使用papercut,您可以在不知道或使用实际生产 smtp 服务器的情况下测试您的应用程序。

Then during testing you can just set the host to your local machine that is running papercut in the app/web config. Therefore it can be changed once moving to production.

然后在测试期间,您可以将主机设置为在 app/web 配置中运行 papercut 的本地机器。因此,一旦投入生产,就可以对其进行更改。

Papercut will show you the emails that were sent and also the contents.

Papercut 将向您显示已发送的电子邮件以及内容。

回答by Spoike

As an alternative: If you don't want to rely on the server configuration and do it programmatically you could always do this:

作为替代方案:如果您不想依赖服务器配置并以编程方式进行配置,您可以随时执行以下操作:

MailMessage mail = new MailMessage() {
    To = "someone@somewhere",
    From = "someone@somewhere",
    Subject = "My Subject",
    Body = "My message"
};

SmtpClient client = new SmtpClient("SMTP Server Address");
    // Naturally you change the "SMTP Server Address" to the
    // actual SMTP server address
client.Send(mail);

But I suggest you stick it in web.config file (which can be configured through ASP.NET Web Configuration tool as well).

但我建议你把它放在 web.config 文件中(也可以通过 ASP.NET Web 配置工具进行配置)。

回答by Vijay

回答by user1048281

Yeah you can use tools like SMTP4Dev to 'send' emails without having an SMTP server at all. I use this frequently for testing to ensure I'm not actually sending emails to real users by mistake.

是的,您可以使用 SMTP4Dev 之类的工具来“发送”电子邮件,而根本不需要 SMTP 服务器。我经常使用它进行测试,以确保我实际上没有错误地向真实用户发送电子邮件。

More info @ http://netdevtools.com/how-to-test-smtp-emails-without-a-mail-server-no-smtp-configuration-required-in-asp-net-c/

更多信息@ http://netdevtools.com/how-to-test-smtp-emails-without-a-mail-server-no-smtp-configuration-required-in-asp-net-c/