C# 在不安装 SMTP 服务器的情况下发送邮件

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

Sending mail without installing an SMTP server

c#.netemailsmtp

提问by Germstorm

I have a .Net application. I want this application to send an email to me. How do I implement this without installing an SMTP server?

我有一个 .Net 应用程序。我希望此应用程序向我发送电子邮件。如何在不安装 SMTP 服务器的情况下实现这一点?

采纳答案by Daniel LeCheminant

Using an SmtpClientto send a MailMessagedoes not require you to have a server on your local machine.

使用 anSmtpClient发送 aMailMessage不需要您在本地机器上有服务器。

Your e-mail service provider is the one with the server (e.g. smtp.gmail.com), and your SmtpClienttalks to it.

您的电子邮件服务提供商是服务器(例如 smtp.gmail.com)的提供商,您SmtpClient可以与之交谈。

回答by AnthonyWJones

You can't send email without the services of a SMTP server, there is of course no need for you to install one, just point your code at your ISPs SMTP server or your companies Exchange server (or what ever they use).

如果没有 SMTP 服务器的服务,您将无法发送电子邮件,当然您无需安装,只需将您的代码指向您的 ISP SMTP 服务器或您公司的 Exchange 服务器(或他们使用的任何服务器)。

回答by splattne

This article by Peter Bromberg on eggheadcafe.com

Peter Bromberg 在 eggheadcafe.com 上的这篇文章

C# SMTP Mail without SMTP Service or CDO

没有 SMTP 服务或 CDO 的 C# SMTP 邮件

explains how to send email without relying on an SMTP client:

解释了如何在不依赖 SMTP 客户端的情况下发送电子邮件:

Sending email via TCP using the native SMTP RFC commands "HELO", "MAIL From", RCPT TO", etc. is no big deal. That's one of the first tricks we learn with Telnet. Finding or writing managed code that will do so reliably is another story. The code in the class that follows is not my original code - I've cobbled it together from three different sample sources, fixing namespaces, error handling, and other minor items, changing console code to class library code, and providing a complete Winforms - based test harness front end that illustrates its correct usage.

I've also included sample code to correctly process and add a mail attachment via an OpenFileDialog here. This code MIME encodes and transmits the attachment(s) according to the specification.

使用本机 SMTP RFC 命令“HELO”、“MAIL From”、RCPT TO”等通过 TCP 发送电子邮件没什么大不了的。这是我们使用 Telnet 学习的第一个技巧。查找或编写可以执行此操作的托管代码可靠的是另一回事。接下来的类中的代码不是我的原始代码 - 我从三个不同的示例源拼凑起来,修复命名空间、错误处理和其他小项目,将控制台代码更改为类库代码,以及提供完整的基于 Winforms 的测试工具前端,说明其正确用法。

我还在此处包含了示例代码,以通过 OpenFileDialog 正确处理和添加邮件附件。此代码 MIME 根据规范对附件进行编码和传输。