【问题标题】:Can we send mails from localhost using asp.net and c#?我们可以使用 asp.net 和 c# 从 localhost 发送邮件吗?
【发布时间】:2013-03-05 23:18:24
【问题描述】:

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

为此它因错误而坠落

无法建立连接,因为目标机器主动 拒绝它some_ip_address_here

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

我收到以下错误

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

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

【问题讨论】:

  • 您需要一个愿意为您发送邮件的 SMTP 服务器。 (以及登录名、正确的端口和 SSL 设置)
  • 如何从我的机器发送邮件,我没有部署代码。我正在本地主机上工作。当我使用主机“smtp.gmail.com”时;它失败了..我不知道如何更正端口和 SSL 设置。

标签: c# email smtpclient


【解决方案1】:

我只是想补充一点,Gmail 现在需要应用密码才能在其他应用中使用它。检查此link。我不得不以艰难的方式找到它。在我创建了应用密码之后,我更改了 NetworkCredentials 以使用它来发送电子邮件。

【讨论】:

    【解决方案2】:

    使用这条线..不要使用端口和主机名

    LocalClient.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;

    【讨论】:

      【解决方案3】:

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

      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);
      }
      

      【讨论】:

      • 如果你在某处找到了代码,你能链接到原文吗?
      • 是的。问题是我前段时间在这里的某个地方找到了它,然后已经使用了一段时间,现在我不记得确切的来源了。
      • @dontomaso 感谢它对我来说工作得很好.....再次感谢..dontomaso
      【解决方案4】:

      将此代码放在 web.config 文件中的 <configuration> </configuration>

      <system.net>
      <mailSettings>
        <smtp>
          <network host="smtp.gmail.com" enableSsl="true" port="587" userName="youremail@gmail.com" password="yourpassword" />
        </smtp>
      </mailSettings>
      </system.net>
      

      然后是后端代码

      MailMessage message = new MailMessage();
      message.IsBodyHtml = true;
      message.From = new MailAddress("email@gmail.com");
      message.To.Add(new MailAddress(TextBoxEadd.Text));
      message.CC.Add(new MailAddress("email@gmail.com"));
      message.Subject = "New User Registration ! ";
      message.Body =  "HELLO";
      sr.Close();
      SmtpClient client = new SmtpClient();
      client.Send(message);
      

      希望这段代码对你有帮助! :)

      【讨论】:

      • 这对我有用!但是你能告诉我为什么你需要定义 message.From = new MailAddress("email@gmail.com") 当你在网络配置中指定你的凭据时,即 userName="youremail@gmail.com"
      【解决方案5】:

      要从client.Host = "localhost" 发送邮件,您需要设置本地 SMTP 服务器。

      要通过 Google(或通过任何其他 SMTP 服务器,包括您自己的本地 SMTP)发送邮件,您必须设置用户名、密码、ssl 设置 - 所有这些都符合所选 SMTP 服务器的要求,并且您需要阅读他们的帮助。

      例如 Google says,您需要 SSL、端口 465 或 587、服务器 smtp.gmail.com 以及您的用户名和密码。

      您可以在 .config 文件中分配所有这些值。

      <system.net>
        <mailSettings>
          <smtp>
            <network host="smtp.gmail.com" enableSsl="true" port="587" userName="yourname@gmail.com" password="password" />
          </smtp>
        </mailSettings>
      </system.net>
      

      或者每次使用前在代码中设置为SmtpClient:

      client.Host = "smtp.gmail.com";
      client.Port = 587;
      client.EnableSSL = true;
      client.Credentials = new NetworkCredential("yourname@gmail.com", "password");
      

      【讨论】:

        猜你喜欢
        • 2020-08-18
        • 1970-01-01
        • 1970-01-01
        • 2015-12-22
        • 2010-11-04
        • 1970-01-01
        • 1970-01-01
        • 2012-10-19
        • 2011-05-26
        相关资源
        最近更新 更多