Operation timed out error

雨燕双飞 提交于 2019-12-06 09:58:01

This is the code I use for mail configuration and Recaptcha proxy for a web site that is hosted on 1and1 :

1- Web.config (only works if put there !)

<system.net>
    <mailSettings>
         <smtp from="mail@domain.com">
            <network host="smtp.1and1.com" port="25" userName="mymail@domain.com" password="mypassword"/>
         </smtp>
    </mailSettings>
    <defaultProxy>
        <proxy usesystemdefault = "false" bypassonlocal="false" proxyaddress="http://ntproxyus.lxa.perfora.net:3128" />
    </defaultProxy>
</system.net>

2- Inside a dedicated action in mycontroller :

// ouside the action I've defined the response
private class gapi {public bool success{get;set;}}

public bool SendMail(string firstname, string lastname, string email, string message, string grecaptcha)
{
    SmtpClient smtp = new SmtpClient("smtp.1and1.com");
    MailMessage mail = new MailMessage();
    mail.From = new MailAddress(email);
    mail.To.Add("mail@domain.com");
    mail.Subject = firstname + " " + lastname;
    mail.Body = message;
    try
    {
        using (var client = new WebClient())
        {
            var values = new NameValueCollection();
            values["secret"] = "6LcEnQYTAAAAAOWzB44-m0Ug9j4yem9XE4ARERUR";
            values["response"] = grecaptcha;
            values["remoteip"] = Request.UserHostAddress;

            var response = client.UploadValues("https://www.google.com/recaptcha/api/siteverify","POST", values);
            bool result = Newtonsoft.Json.JsonConvert.DeserializeObject<gapi>((Encoding.Default.GetString(response) as string)).success;
            if(!result) return "Something is wrong)";
        }
        //... verify that the other fields are ok and send your mail :)
        smtp.Send(mail);
    }
    catch (Exception e) { return "Something is wrong)"; }

    return "Okey :)";
}

Hope this helps.

Kalyan

Finally got the solution, I got the correct proxy server address from 1and1 and used that. reCaptcha works great now.

Also, for some reason, setting the proxy value in the code using the IWebProxy property of the reCaptcha control did not work. I had to add the tag in web.config under .

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