从c#代码发送电子邮件

时间:2013-06-25 20:00:59

标签: c# email

我正在尝试从我的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

1 个答案:

答案 0 :(得分:2)

你必须打开加密。

将此行放在smtp.Send()

之前的某个位置
smtpClient.EnableSsl = true;