代码之家  ›  专栏  ›  技术社区  ›  Dinav Ahire

如何在asp中检查弹性电子邮件API的API密钥是否正确。net和C#

  •  0
  • Dinav Ahire  · 技术社区  · 9 年前

    我在asp中的web应用程序项目中使用弹性电子邮件API。net c#。 当我付出 USERNAME API_KEY 与其他所需数据一起,电子邮件被发送到目的地(我正在向自己发送电子邮件以进行测试)。在收到邮件时 Lable 在我的页面上 "Mail is sent successfully"

    现在的问题是,如果我提供了错误的 用户名 API_键 ,我会得到 拉布勒 但不是邮件。我知道,因为我提供了错误的 API_键 .

    那么如何检查我是否提供了有效的 用户名 API_键 这样我就可以把Lable改成 "check your data"

    我的代码:

    ElasticEmail.cs

    public static string SaveAndTestElasticEmail(string from, string to, string fromName, string subject, string bodyHtml)
            {
                try
                {
                    setElasticEmailSettings();
                    string channel = System.Configuration.ConfigurationManager.AppSettings["registrationNumber"];
                    WebClient client = new WebClient();
                    NameValueCollection values = new NameValueCollection();
                    values.Add("username", USERNAME);
                    values.Add("api_key", API_KEY);
                    values.Add("from", from);
                    values.Add("from_name", fromName);
                    values.Add("subject", subject);
                    values.Add("body_html", bodyHtml);
                    values.Add("channel", channel);
                    values.Add("to", to);
                    byte[] response = client.UploadValues("https://api.elasticemail.com/mailer/send", values);
                    return Encoding.UTF8.GetString(response);
                }
                catch (Exception ex)
                {
                    return null;
    
                }
    
            }
    

    如果我提供有效 API_键 我调试代码 response{[36]} 。如果我提供了错误的密钥,那么我仍然会收到响应, response{[22]} 我不会收到邮件。

    1 回复  |  直到 9 年前
        1
  •  0
  •   dipak_pusti    7 年前
    byte[] response = client.UploadValues("https://api.elasticemail.com/mailer/send", values);
    
    if (response.Length <= 35)
    {
        return null;
    }
    else
    {
        return Encoding.UTF8.GetString(response);
    }