通过GMail发送电子邮件:连接尝试失败,因为连接方没有正确响应

时间:2014-07-10 08:12:05

标签: c# email smtp

我正在使用This postThis Post在c#console App上创建简单的电子邮件发送应用程序。但是我在gmail上发送电子邮件时收到错误...

  

{"连接尝试失败,因为连接方没有   在一段时间后正确回应,或建立连接   失败,因为已连接的主机无法响应XXX"}

这是我的代码:

class Program
    {
        private static string to = "XXX@gmail.com";
        private static string from = "XXX@gmail.com";
        private static string subject="07/10/14";
        private static string body;
        private static string address = "XXX@gmail.com";
        static void Main(string[] args)
        {

            MailMessage mail = new MailMessage();
            mail.To.Add(to);
            mail.From = new MailAddress(from);
            mail.Subject = subject;
            mail.Body = body;
            mail.IsBodyHtml = true;
            SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
            smtp.EnableSsl = true;
            smtp.UseDefaultCredentials = false;
            smtp.Credentials =
                 new System.Net.NetworkCredential(address, "YYYYY");
            smtp.Send(mail);
            Console.WriteLine("Sent");
            Console.ReadLine();
        }


}

我的App.Config文件:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
  </appSettings>
  <system.net>
    <mailSettings>
      <smtp from="XXX@gmail.com">
        <network defaultCredentials="false"
        userName="XXX@gmail.com"
           password="YYYYY"
        host="smtp.gmail.com" port="587" enableSsl="true"/>
      </smtp>
    </mailSettings>

  </system.net>
</configuration>

我已经阅读了类似的帖子..有些建议将网址转换为流..我没有得到它..有人说问题可能是在互联网连接..其他说设置smtp服务器到587 ..我已应用所有更改。 .still它显示相同的错误

请建议

1 个答案:

答案 0 :(得分:0)

我解决了这个问题...

只需使用您的托管公司Smtp和Port ..not gmail或其他..如果您从托管公司发送....请咨询您的IT服务台,以便为您的公司提供smtp地址和端口..I这样做......解决了问题

这是我的工作代码.. 它也是通过公司SMTP发送到gmail

class Program
    {
        private static string to = "XXXXr@youHostingComapny.com";
        private static string from = "YYYYY@youHostingComapny.com";
        private static string subject = "test Mail sent By Code";
        private static string body = "Mail sent By Code";

        static void Main(string[] args)
        {
            try
            {

                MailMessage mail = null;

                using (mail = new MailMessage(new MailAddress(from), new MailAddress(to)))
                {

                    mail.Subject = subject;
                    mail.Body = body;
                    mail.To.Add("ZZZZZZZZ@gmail.com");

                    SmtpClient smtpMail = null;
                    using (smtpMail = new SmtpClient("HostingComapny smtp Address"))
                    {
                        smtpMail.Port = Hosting Company Port No.;
                        smtpMail.EnableSsl = false;
                        smtpMail.Credentials = new NetworkCredential("youruserName", "yourPassword");

                        smtpMail.UseDefaultCredentials = false;
                        // and then send the mail
                        ServicePointManager.ServerCertificateValidationCallback =
    delegate(object s, X509Certificate certificate,
             X509Chain chain, SslPolicyErrors sslPolicyErrors)
    { return true; };
                        smtpMail.Send(mail);
                        Console.WriteLine("sent");
                        Console.ReadLine();

                    }

                }

            }
            catch (Exception ex)
            {

                throw ex;
            }

        }


    }