MailGun jQuery の例は非同期要求であり、送信プロセスは mailgun API からの応答を待たずに機能します。
Web メソッドに対して同期要求を行う必要があります。次のコードを使用できます
function validateEmailByMailGun(Email){
$.ajax({
type: "POST",
url: "alpha.aspx/CheckEmail",
data: "{UserName:'" + Email + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
async: false,
success: function Success(msg) {
isMailGunValidEmail = msg.d; //can have true, false or error
},
error: function (xhr, ajaxOptions, thrownError) {
isMailGunValidEmail = false;
isMailGunError = true;
}
});
}
alpha.aspx で、webmethod を作成します
[System.Web.Services.WebMethod]
public static object CheckEmail(string UserName)
{
var http = (HttpWebRequest)WebRequest.Create("https://api.mailgun.net/v2/address/validate?address=" + UserName);
http.Credentials = new NetworkCredential("api","put your public key here");
http.Timeout = 5000;
try
{
var response = http.GetResponse();
var stream = response.GetResponseStream();
var sr = new StreamReader(stream);
var content = sr.ReadToEnd();
JSON.JsonObject js = new JSON.JsonObject(content);
return Convert.ToBoolean(js["is_valid"]);
}
catch (Exception ex)
{
}
}