如何在asp.net和C#中检查Elastic EmailAPI的API键是否正确

How to check if API key of Elastic Email API is correct or incorrect in asp.net and C#

我在 asp.net c# 的 Web 应用程序项目中使用 Elastic Email API。 当我将 USERNAMEAPI_KEY 与其他所需数据一起提供时,电子邮件将发送到目的地(我正在向自己发送电子邮件进行测试)。在收到邮件时,我的页面上有一个 Lable "Mail is sent successfully"

现在的问题是,如果我提供错误的 USERNAMEAPI_KEY,我会收到 Lable 而不是邮件。我知道是因为我提供错了API_KEY.

那么如何检查我是否提供了有效的 USERNAMEAPI_KEY 以便我可以将 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_KEY 并调试我的代码,我将得到 response{[36]} 。如果我提供了错误的密钥,那么我仍然会收到回复,但是 response{[22]} 我不会收到邮件。

byte[] response = client.UploadValues("https://api.elasticemail.com/mailer/send", values);

if (response.Length <= 35)
{
    return null;
}
else
{
    return Encoding.UTF8.GetString(response);
}