简体   繁体   English

如何在asp.net和C#中检查Elastic Email API的API密钥是否正确

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

I am using Elastic Email API in my web application project in asp.net c#. 我在asp.net c#的Web应用程序项目中使用Elastic Email API。 when I give USERNAME and API_KEY with other required data, email is sent to destination (I am sending email to myself for testing). 当我将其他必需的数据提供给USERNAMEAPI_KEY时,电子邮件将发送到目的地(我正在向自己发送电子邮件以进行测试)。 And on receiving mail, there is one Lable on my page "Mail is sent successfully" 在接收邮件时,我的页面上有一个Lable "Mail is sent successfully"

Now the problem is, if I provide wrong USERNAME and API_KEY , I will get Lable but not mail. 现在的问题是,如果我提供了错误的USERNAMEAPI_KEY ,我会得到Lable而不是邮件。 I know because I have provided wrong API_KEY . 我知道,因为我提供了错误的API_KEY

So how to check if i have provided valid USERNAME and API_KEY so that I can change Lable to "check your data" 因此,如何检查我是否提供了有效的USERNAMEAPI_KEY以便可以更改Lable以"check your data"

My Code: 我的代码:

ElasticEmail.cs 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;

            }

        }

If I provide valid API_KEY and I debug my code, I am getting response{[36]} . 如果我提供了有效的API_KEY并调试了代码, API_KEY得到response{[36]} and if I provide wrong key, then still I will receive response but it is, response{[22]} and I will not get mail. 如果我提供了错误的密钥,那么我仍然会收到响应,但是它是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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用c#在asp.net中读取json响应(Reseller Club域检查可用性API集成) - how to read json response in asp.net with c# (Reseller Club Domain Check Availability API Integration) 如何使用asp.net c#在azure门户中使用SendGrid Api Key发送邮件 - how to send mail using SendGrid Api Key in azure portal using asp.net c# 如何在c#Asp.NET中处理Linkedin邀请API - How to handle Invitation API of Linkedin in c# Asp.NET 如何在asp.net c#中获取API响应 - how to get API response it in asp.net c# ASP.net C#检查小时数是否正确 - ASP.net c# Check that the hours are correct 检查收件箱中的每封电子邮件是否已回复-ASP.NET C# - Check if each email in inbox is replied to or not - asp.net c# Microsoft Graph Rest API使用c#ASP.NET MVC将附件添加到电子邮件 - Microsoft Graph Rest API Add attachment to email using c# ASP.NET MVC 从 Twitter API 获取用户的电子邮件以进行外部登录身份验证 ASP.NET MVC C# - Get user's email from Twitter API for External Login Authentication ASP.NET MVC C# 在Gridview前端ASP.NET C#中检查关键重复项 - Check for key duplicates in Gridview Frontend ASP.NET C# 如何使用asp.net,c#,adonet在两个文本字段中检查用户输入的数据是否正确 - How to check user entered data is correct or not on two text fields using asp.net, c#, adonet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM