最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - Any idea why I'm not getting anything back from Sendgrid when I try to send an email via their api - Stack Overflow

programmeradmin5浏览0评论

I have the following code. I am trying to send an email via their system. I have signed up for an account today. My account is valid. I am just trying to send out something simple to see if everything works, and I get nothing. I am sending to my wallace.b.mcclure+01 at gmail address. My from address is noreply at noreply.biz. I get no data back why an email isn't sent. The call is incredibly quick as if the request to send an email doesn't leave my system. Any idea on what I am doing wrong? I'm wondering if the "from" email address is the problem, since it isn't real. I am using the code below. (Yes, I know I need to take the async keyword out)

TIA

public async Task<bool> SendEmail(string To, string From, string Subject, string Body)
{
    bool ret = true;
    try
    {
        var builder = new ConfigurationBuilder()
        .SetBasePath(Directory.GetCurrentDirectory())
        .AddJsonFile("appsettings.json");

        var Configuration = builder.Build();
        var apiKey = Configuration.GetValue<string>("Communications:SendGridApiKey");

        var client = new SendGridClient(apiKey);
        var output = MailHelper.CreateSingleEmail(new EmailAddress(From), new EmailAddress(To), Subject, Body, Body);
    }
    catch (Exception ex)
    {
        ret = false;
    }
    finally
    {

    }
    return ret;
}

I have the following code. I am trying to send an email via their system. I have signed up for an account today. My account is valid. I am just trying to send out something simple to see if everything works, and I get nothing. I am sending to my wallace.b.mcclure+01 at gmail address. My from address is noreply at noreply.biz. I get no data back why an email isn't sent. The call is incredibly quick as if the request to send an email doesn't leave my system. Any idea on what I am doing wrong? I'm wondering if the "from" email address is the problem, since it isn't real. I am using the code below. (Yes, I know I need to take the async keyword out)

TIA

public async Task<bool> SendEmail(string To, string From, string Subject, string Body)
{
    bool ret = true;
    try
    {
        var builder = new ConfigurationBuilder()
        .SetBasePath(Directory.GetCurrentDirectory())
        .AddJsonFile("appsettings.json");

        var Configuration = builder.Build();
        var apiKey = Configuration.GetValue<string>("Communications:SendGridApiKey");

        var client = new SendGridClient(apiKey);
        var output = MailHelper.CreateSingleEmail(new EmailAddress(From), new EmailAddress(To), Subject, Body, Body);
    }
    catch (Exception ex)
    {
        ret = false;
    }
    finally
    {

    }
    return ret;
}
Share Improve this question edited Mar 24 at 3:25 WindyJMusic 286 bronze badges asked Mar 19 at 21:49 Wallace B. McClureWallace B. McClure 1,5552 gold badges24 silver badges41 bronze badges 3
  • That could have a number of reasons. Did you successfully complete the sender verification (Single Sender vs. Domain Authentication)? Also, what do you see on the dashboard at <app.sendgrid> (Requests, Delivered, Opened, Clicked)? – IObert Commented Mar 20 at 8:29
  • I have setup a single sender and updated my app to send via that email address. I don't see anything in my email and I do not see anything in the stats at app.sendgrid. When I step thru my code, I get the same problem which is that it quickly goes over that line where I would expect it to either take a while as it goes out to the sendgrid server. I don't think the message is leaving my development system. I do not see any messages sent in the app.sendgrid site and I don't get any errors on my end. – Wallace B. McClure Commented Mar 20 at 13:10
  • and I got it working. I'll update my code above to show what I should be doing. Many thanks. Just talking it thru is helpful. – Wallace B. McClure Commented Mar 20 at 13:20
Add a comment  | 

1 Answer 1

Reset to default 0

Here is some code that does work. The problem was that I was never actually sending. Writing code when you are tired never does work out well.

        public async Task<bool> SendEmail(string To, string From, string Subject, string Body)
        {
            bool ret = true;
            try
            {
                var builder = new ConfigurationBuilder()
                .SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile("appsettings.json");

                var Configuration = builder.Build();
                var apiKey = Configuration.GetValue<string>("Communications:SendGridApiKey");

                var client = new SendGridClient(apiKey);
                var msg = new SendGridMessage()
                {
                    From = new EmailAddress(From),
                    Subject = Subject,
                    HtmlContent = Body,
                    PlainTextContent = Body
                };
                var output = MailHelper.CreateSingleEmail(new EmailAddress(From), new EmailAddress(To), Subject, Body, Body);
                await client.SendEmailAsync(output);
            }
            catch (Exception ex)
            {
                ret = false;
            }
            finally
            {

            }
            return ret;
        }
    }

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论