从 C# 代码发送电子邮件

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

Sending email from c# code

c#email

提问by daidai

I am trying to send an e-mail from my C# code.Here is an example

我正在尝试从我的 C# 代码发送电子邮件。这是一个示例

MailMessage message = new MailMessage();
            message.To.Add("[email protected]");
            message.Subject = "Registration";
            message.From = new System.Net.Mail.MailAddress("[email protected]");
            message.Body = "OK";
            SmtpClient smtp = new SmtpClient();
            smtp.Host = "smtp.gmail.com";
            smtp.Send(message);

But my code breaks and here is the error:

但是我的代码中断了,这是错误:

The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.0 Must issue a STARTTLS command first. hn4sm3874638bkc.2 - gsmtp

回答by Kamil

You have to turn on encryption.

您必须打开加密。

Put this line somewhere before smtp.Send()

将此行放在之前的某个位置 smtp.Send()

smtpClient.EnableSsl = true;