从c#代码发送电子邮件

本文关键字:电子邮件 代码 | 更新日期: 2023-09-27 18:20:03

我正试图用C#代码发送一封电子邮件。以下是的示例

MailMessage message = new MailMessage();
            message.To.Add("milos90zr@gmail.com");
            message.Subject = "Registration";
            message.From = new System.Net.Mail.MailAddress("milos90zr@hotmail.com");
            message.Body = "OK";
            SmtpClient smtp = new SmtpClient();
            smtp.Host = "smtp.gmail.com";
            smtp.Send(message);

但我的代码坏了,这里是错误:

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

从c#代码发送电子邮件

您必须打开加密。

把这条线放在smtp.Send() 之前的某个地方

smtpClient.EnableSsl = true;