3

我在用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# 新手,请在我出错的代码中纠正我..?

4

5 回答 5

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);
}
于 2013-03-17T16:15:01.153 回答
2

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

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

例如,Google您需要 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");
于 2013-03-17T16:46:42.720 回答
1

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

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

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

于 2013-03-17T16:30:20.480 回答
0

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

LocalClient.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;

于 2015-03-21T10:15:17.650 回答
0

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

于 2020-02-13T05:28:07.467 回答