我正在使用 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);

对于主机,我提供 client.Host = "localhost";
为此它错误地下降



当我使用 client.Host = "smtp.gmail.com";
我收到以下错误



我无法通过本地主机发送邮件。
请帮帮我,我是 C# 新手,请在我出错的代码中纠正我..?

最佳答案

这是一些用于通过 gmail 发送邮件的代码(来自 stackoverflow 上某处的代码。它类似于此处的代码: Gmail: How to send an email programmatically ):

using (var client = new SmtpClient("smtp.gmail.com", 587)
{
  Credentials = new NetworkCredential("[email protected]", "yourpassword"),
  EnableSsl = true
})
{
  client.Send("[email protected]", "[email protected]", "subject", message);
}

关于c# - 我们可以使用 asp.net 和 c# 从本地主机发送邮件吗?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15462960/

10-13 06:49