C# 使用本地主机 SMTP 发送邮件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1557989/
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-06 18:49:36  来源:igfitidea点击:

Send mail using localhost SMTP

c#iis-6smtp

提问by pencilslate

I am trying to setup SMTP server on IIS for sending mails. The SMTP server is intended to be used by the ASP.NET code in C#.

我正在尝试在 IIS 上设置 SMTP 服务器以发送邮件。SMTP 服务器旨在供 C# 中的 ASP.NET 代码使用。

I was previously using gmail smtp wherein i provided the smtp.gmail.com as host with secure port and my gmail uid/pwd. That worked fine. Here is the code used to do that.

我以前使用过 gmail smtp,其中我提供了 smtp.gmail.com 作为具有安全端口和我的 gmail uid/pwd 的主机。那工作得很好。这是用于执行此操作的代码。

SmtpClient smtpClient = new SmtpClient();
smtpClient.UseDefaultCredentials = false;
smtpClient.Host = "smtp.gmail.com";
smtpClient.Port = 587;
smtpClient.Credentials = new NetworkCredential(uname,pwd);
smtpClient.EnableSsl = true;
smtpClient.Send(mailMessage);

Now i am planning to use the localhost SMTP server on IIS, what values should i be giving for the parameters UseDefaultCredentials and Credentials.I will be assigning false to EnableSsl as it's over port 25.

现在我打算在 IIS 上使用 localhost SMTP 服务器,我应该为参数 UseDefaultCredentials 和 Credentials 提供什么值。我将为 EnableSsl 分配 false,因为它通过端口 25。

Also, what could be the most simple SMTP virtual server configuration.

此外,最简单的 SMTP 虚拟服务器配置可能是什么。

采纳答案by Natim

I think in localhost you can use :

我认为在本地主机中你可以使用:

SmtpClient smtpClient = new SmtpClient();
smtpClient.UseDefaultCredentials = true;
smtpClient.Send(mailMessage);

回答by Joel Coehoorn

It depends on how you configure the smtp server. You might not need to use any credentials at all, and just configure the server to only accept local connections.

这取决于您如何配置 smtp 服务器。您可能根本不需要使用任何凭据,只需将服务器配置为仅接受本地连接。

回答by dave wanta

When you are using the local IIS SMTP service, set the DeliveryMethod to PickupDirectoryFromIis. For example:

使用本地 IIS SMTP 服务时,将 DeliveryMethod 设置为 PickupDirectoryFromIis。例如:

  smtpClient.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;

This totally bypasses the network layer, and writes the messages directly to disk. Its much faster than going through the chatty SMTP protocol.

这完全绕过了网络层,并将消息直接写入磁盘。它比通过健谈的 SMTP 协议要快得多。

When you using the above code, it means you can get rid of this part of your code:

当你使用上面的代码时,就意味着你可以去掉这部分代码:

smtpClient.UseDefaultCredentials = false;
smtpClient.Host = "smtp.gmail.com";
smtpClient.Port = 587;
smtpClient.Credentials = new NetworkCredential(uname,pwd);
smtpClient.EnableSsl = true;

回答by mahalie

Tx Natim, what you say worked for me. Have our intranet app using integrated auth with our exchange 2007 server now:

Tx Natim,你说的对我有用。现在让我们的内部网应用程序使用集成身份验证与我们的 Exchange 2007 服务器:

Dim msg As New MailMessage()
Dim smtp As SmtpClient

msg.From = New MailAddress(strFrom)
msg.To.Add(strTo)
msg.Subject = strSubject
msg.Body = strBody

smtp = New SmtpClient("ServerName")
smtp.UseDefaultCredentials = True
smtp.Send(msg) 

回答by Lee Englestone

Have you tried enabling relay?

你试过启用中继吗?

Find IIS6 manager (I have found that searching for IIS may return 2 results) go to the SMTP server properties then 'Access' then press the relay button.

找到 IIS6 管理器(我发现搜索 IIS 可能会返回 2 个结果)转到 SMTP 服务器属性,然后“访问”,然后按中继按钮。

Then you can either select all or only allow certain ip's like 127.0.0.1

然后你可以选择全部或只允许某些 ip,如 127.0.0.1

SMTP Relay

SMTP 中继

回答by DinP

If you want to test emails in localhost, just download install the papercut tool https://papercut.codeplex.com/

如果你想在 localhost 中测试电子邮件,只需下载安装剪纸工具https://papercut.codeplex.com/

and change hostname to localhost as below. Papercut captures all the emails sending using localhost.

并将主机名更改为 localhost,如下所示。Papercut 捕获所有使用 localhost 发送的电子邮件。

  smtpClient.UseDefaultCredentials = false;
    smtpClient.Host = "localhost";
    smtpClient.Port = 587;
    smtpClient.Credentials = new NetworkCredential(uname,pwd);
    smtpClient.EnableSsl = true;