无法连接到远程服务器C# [英] Unable to connect to the remote server C#

查看:343
本文介绍了无法连接到远程服务器C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我的代码如下,



Hi,

my code as below,

public void SendMaiL(string sSubject, string[] arToAddress, string[] arToAddressCC, int? iReqEmpId, int iMailType)
  {
      string sBody = string.Empty;
      bool enableSSL = true;

      if (arToAddress != null && arToAddress.Length > 0)
      {
          try
          {
              string sServer = "smtp.mail.com";
              int iPort =587;
              string sUserName = ConfigurationManager.AppSettings["userName"].ToString();
              string sPassword = ConfigurationManager.AppSettings["password"].ToString();
              string sFrom = ConfigurationManager.AppSettings["from"].ToString();

              //oSmtpClient.UseDefaultCredentials = false;
              //oSmtpClient.Credentials = new NetworkCredential(sUserName, sPassword);

              MailMessage oMailMessage = new MailMessage(); ;
              oMailMessage.From = new MailAddress(sFrom);
              if (arToAddress != null && arToAddress.Length > 0)
              {
                  sBody = GetMailBody(Convert.ToInt32(arToAddress[0]), iMailType, iReqEmpId);
                  for (int t = 0; t <= arToAddress.Length - 1; t++)
                  {
                      oMailMessage.To.Add(new MailAddress(GetEmpMailId(Convert.ToInt32(arToAddress[t].ToString()))));
                  }
              }
              if (arToAddressCC != null && arToAddressCC.Length > 0)
              {
                  for (int i = 0; i <= arToAddressCC.Length - 1; i++)
                  {
                      if (arToAddressCC[i] != null)
                          oMailMessage.CC.Add(new MailAddress(GetEmpMailId(Convert.ToInt32(arToAddressCC[i].ToString()))));
                  }
              }
              oMailMessage.Subject = sSubject;
              oMailMessage.Body = sBody;
              oMailMessage.IsBodyHtml = true;

              using (SmtpClient smtp = new SmtpClient(sServer, iPort))
              {
                  smtp.Credentials = new NetworkCredential(sFrom, sPassword);
                  smtp.EnableSsl = enableSSL;
                  smtp.Send(oMailMessage);
              }

          }
          catch (Exception ex)
          {
              //WriteLog(ex.ToString());
          }
      }
  }





但是获取错误smtp异常被捕获。发送邮件失败。无法连接到远程服务器。



:(



谢谢..



But getting error smtp exception was caught.Failure sending mail. Unable to connect to the remote server.

:(

thanks..

推荐答案

然后显而易见的问题是,这个地址是否正确?。

Then the obvious question has to be, "is that address correct?".
string sServer = "smtp.mail.com";



你真的打算输入:


Did you actually mean to type:

string sServer = "smtp.gmail.com";


是的......常见的异常。我遇到过这个。这可能是由于



1)你的防病毒阻止邮件发送。我使用Mcafee,我必须在发送邮件时禁用访问保护。如果你使用它来获得更多答案,请告诉我。



2)检查你的端口号。 Gmail支持25,587,465。我使用的是25和587.它们都可以为我工作。



3)尝试在目标浏览器中登录gmail。如果您的应用程序以chrome身份运行,请尝试使用chrome登录gmail并重新运行您的应用程序:)当您尝试从未检测到的设备运行gmail并登录之前修复它时,Google会要求进行两步身份验证。



4)验证您的凭据:D



希望这可以解决这个问题。这就是我在服务器中修复它的方法。如果您还有问题,很乐意为您提供帮助。
Yeah..common exception. I have encountered this. This may be caused due to

1) Your antivirus blocking mail sending. I use Mcafee and I had to disable access protection when you send mail. Let me know if you use this for further answers.

2) Check your port number. Gmail supports 25, 587, 465. I am using 25 and 587. Either of them work for me.

3) Try to login to gmail in your target browser. If your app is running in chrome, try to login to gmail in chrome and re-run your app :) Google asks two step authentication while you try to run gmail from an undetected device and logging in prior fixes it.

4) Verify your credentials also :D

Hope this should fix this problem. This is how I have fixed it in my servers. Happy to help you if you have issues still.


这篇关于无法连接到远程服务器C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆