C# 无法连接到远程服务器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17787767/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 10:28:36  来源:igfitidea点击:

Unable to connect to remote server

c#

提问by thesma

I am new to C# and I am trying to send an email from a desktop program I am developing. Here is the code I am using but I keep getting the error below :

我是 C# 新手,我正在尝试从我正在开发的桌面程序发送电子邮件。这是我正在使用的代码,但我不断收到以下错误:

System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
message.To.Add("[email protected]");
message.Subject = "This is the Subject line";
message.From = new System.Net.Mail.MailAddress("[email protected]");
message.Body = "This is the message body";
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("smtp.gmail.com",578);
smtp.EnableSsl = true;
smtp.Send(message);

I can't seem to find out what the problem is...

我似乎无法找出问题所在......

采纳答案by BigBadOwl

You need to set the credentials for Gmail

您需要为 Gmail 设置凭据

var client = new SmtpClient("smtp.gmail.com", 587)
 {
    Credentials = new NetworkCredential("[email protected]", "mypwd"),
    EnableSsl = true
};
client.Send("[email protected]", "[email protected]", "test", "testbody");

回答by Brad Christie

Missing credentials most likely:

最有可能缺少凭据:

smtp.Credentials = new System.Net.NetworkCredential("[email protected]", "password");

So:

所以:

SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
smtp.Credentials = new System.Net.NetworkCredential("[email protected]", "Password");
smtp.EnableSsl = true;
smtp.Send(message);

Alternatively, you can store (almost all) this stuff in the app.config, though I'm not sure how secure you need it to be since the user/pass would be plainly visible to any user that opens the application's directory (and that file). For completion's sake:

或者,您可以将(几乎所有)这些东西存储在app.config. 为了完成:

<system.net>
  <mailSettings>
    <smtp from="[email protected]">
      <network host="smtp.gmail.com"
               enableSsl="true"
               userName="[email protected]"
               password="password"
               port="587" />
    </smtp>
  </mailSettings>
</system.net>

回答by Jorgesys

Network credentials are very important, but sometimes we need to check if port 587is blocked.

网络凭据非常重要,但有时我们需要检查端口587是否被阻止。

   SmtpClient client = new SmtpClient();
    client.Credentials = new System.Net.NetworkCredential("[email protected]", "whocaresdupe");
    client.Port = 587;
    client.Host = "smtp.gmail.com";
    client.EnableSsl = true;
    try
    {
        client.Send(mail);          
    } catch (Exception ex)
    {
       Page.RegisterStartupScript("UserMsg", "<script>alert('Successfully Send...');if(alert){ window.location='SendEmail.aspx';}</script>");
    }

回答by user6082735

My company blocks desktop/laptops from sending email using a setting in McAfee. Check the Windows Event view to see if there is an entry that an email was blocked.

我的公司使用 McAfee 中的设置阻止台式机/笔记本电脑发送电子邮件。检查 Windows 事件视图以查看是否有电子邮件被阻止的条目。