Sending email via GMail: A connection attempt failed because the connected party did not properly respond

我的梦境 提交于 2019-12-11 12:07:43

问题


I am using This post and This Post to create simple email sending Application on c# console App. But I am getting error when sent email on gmail ...

{"A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond XXX"}

Here is my Code :

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


}

My App.Config File :

<?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>

I have read similar post ..Some suggest convert url into stream ..I did not get it ..Some says problem might be in internet connection ..other says set smtp server to 587 ..I have applied all changes ..still it shows same error

Please Suggest


回答1:


I have Solved this problem ...

Just use your Hosting Company Smtp and Port ..not gmail or other one..If you are sending from a hosting company ....Consult your IT Desk for providing your company smtp address and port ..I did that..that solved the Issue

This is My Working Code ..it is also sending to gmail by company SMTP

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

        }


    }


来源:https://stackoverflow.com/questions/24671471/sending-email-via-gmail-a-connection-attempt-failed-because-the-connected-party

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!