這是我使用的郵件配置和代理的Recaptcha爲上的1and1託管網站代碼:
1- Web.config文件(僅當把那裏的工作!)
<system.net>
<mailSettings>
<smtp from="[email protected]">
<network host="smtp.1and1.com" port="25" userName="[email protected]" password="mypassword"/>
</smtp>
</mailSettings>
<defaultProxy>
<proxy usesystemdefault = "false" bypassonlocal="false" proxyaddress="http://ntproxyus.lxa.perfora.net:3128" />
</defaultProxy>
</system.net>
2-在我的控制器中進行專門的操作:
// 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("[email protected]");
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 :)";
}
希望這會有所幫助。
您確定可從您部署到的服務器訪問recaptcha網站嗎? – 2011-12-22 00:59:05
@ M.Babcock我如何能夠證實這一點?我看到了reCaptcha控件,所以我認爲它是可訪問的。 – Kalyan 2011-12-22 02:23:00