通过GMail发送电子邮件:连接尝试失败,因为被连接方未正确响应 [英] Sending email via GMail: A connection attempt failed because the connected party did not properly respond

查看:226
本文介绍了通过GMail发送电子邮件:连接尝试失败,因为被连接方未正确响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用这篇文章和< a href = https://stackoverflow.com/questions/7102687/a-connection-attempt-failed-because-the-connected-party-did-not-properly-respond>此帖子以创建简单通过c#控制台应用程序发送电子邮件。但是当在gmail上发送电子邮件时,我收到了错误消息。

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


{连接尝试失败,因为连接方没有
在一段时间后正确响应,或者由于连接的主机未能响应XXX}建立的连接
失败

{"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"}

这是我的代码:

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>

我看过类似的帖子。有人建议将url转换为流..我没有得到。有人说问题可能出在互联网上。.别人说将smtp服务器设置为587 ..我已应用所有更改..仍然显示相同的错误

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

请建议

推荐答案

我已经解决了这个问题...

I have Solved this problem ...

只需使用您的托管公司的Smtp和端口..而不是gmail或其他..如果您从托管公司发送....请咨询您的IT Desk以提供您公司的SMTP地址和端口..我做到了。问题

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

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

        }


    }

这篇关于通过GMail发送电子邮件:连接尝试失败,因为被连接方未正确响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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