我们可以使用asp.net和c#从localhost发送邮件吗?

Shr*_*rry 3 c# email smtpclient

我在用 using System.Net.Mail;

并按照以下代码发送邮件

MailMessage message = new MailMessage();
        SmtpClient client = new SmtpClient();

        // Set the sender's address
        message.From = new MailAddress("fromAddress");

     // Allow multiple "To" addresses to be separated by a semi-colon
        if (toAddress.Trim().Length > 0)
        {
            foreach (string addr in toAddress.Split(';'))
            {
                message.To.Add(new MailAddress(addr));
            }
        }
      // Allow multiple "Cc" addresses to be separated by a semi-colon
        if (ccAddress.Trim().Length > 0)
        {
            foreach (string addr in ccAddress.Split(';'))
            {
                message.CC.Add(new MailAddress(addr));
            }
        }
        // Set the subject and message body text
        message.Subject = subject;
        message.Body = messageBody;

        // Set the SMTP server to be used to send the message
        client.Host = "YourMailServer";

        // Send the e-mail message
        client.Send(message);
Run Code Online (Sandbox Code Playgroud)

为我提供的主机 client.Host = "localhost";

为此它的错误下降

无法建立连接,因为目标计算机主动拒绝了some_ip_address_here

当我使用 client.Host = "smtp.gmail.com";

我得到以下错误

连接尝试失败,因为连接方在一段时间后没有正确响应,或者建立的连接失败,因为连接的主机无法响应

我无法通过localhost发送邮件.请帮助我,我是c#的新手请在我错的代码中纠正我..?

tom*_*msv 5

这里有一些代码适用于通过gmail发送邮件(代码来自stackoverflow上的某处.它类似于此处的代码:Gmail:如何以编程方式发送电子邮件):

using (var client = new SmtpClient("smtp.gmail.com", 587)
{
  Credentials = new NetworkCredential("yourmail@gmail.com", "yourpassword"),
  EnableSsl = true
})
{
  client.Send("frommail@gmail.com", "tomail@gmail.com", "subject", message);
}
Run Code Online (Sandbox Code Playgroud)