Search code examples
c#sendgridsendgrid-api-v3

Why C# library for SengGrid API-v3 is on fictional email to SetFrom method get bad requests?


I am using Senddrid C# library provided by SendGrid to send messages. I have dynamic template. On user click it sends emails with dynamic fields.

So problem is what by some means I cannot use fictional email for that I must use real/activated email. Just in case inputs are valid or got from appseting.js and are also valid. Its problem with "SetFrom" method. No problems with template id or app_key

! Real email - I mean email what I could log-in and send messages for example ! Fake email - to use as stub. for example no-reply@gmail.com

    private async Task mySendEmail(string toAddress, string templateId, JObject dynamicTemplateData, string? toWhome = null, string? setFromEmail = null, string? setFromName = null)
    {
        if (setFromEmail is null)
            setFromEmail = sendGridConfig.SendGridApiFrom;

        if (setFromName is null)
            setFromName = sendGridConfig.SendGridApiFromDisplayName;

       // var foo = sendGridConfig.SendGridApiKey;
        var client = new SendGridClient(sendGridConfig.SendGridApiKey);
        var msg = new SendGridMessage();

        
        msg.AddTo(new EmailAddress(toAddress, toWhome));
        //msg.SetFrom("my-real-email@gmail.com", setFromName); // if using real email it works
        msg.SetFrom(new EmailAddress("test@example.com", "Example User 0")); // fictional email have bad requests
        msg.SetTemplateData(dynamicTemplateData);
        msg.SetTemplateId(templateId);

       

        var response = await client.SendEmailAsync(msg); // Bad request


    }

The output of the result

{"from":{"name":"Example User 0","email":"test@example.com"},"personalizations":[{"to":[{"name":"Stone Ocean","email":"count_zero@inbox.lv"}],"dynamic_template_data":{"url":"CUCUMBER@INBOX.LV","password":"TEREMOK"}}],"template_id":"d-2f393a3c5ca7451ea856fc1acadf0bd7"}
Forbidden
{"errors":[{"message":"The from address does not match a verified Sender Identity. Mail cannot be sent until this error is resolved. Visit https://sendgrid.com/docs/for-developers/sending-email/sender-identity/ to see the Sender Identity requirements","field":"from","help":null}]}
Connection: keep-alive
Access-Control-Allow-Origin: https://sendgrid.api-docs.io
Access-Control-Allow-Methods: POST
Access-Control-Allow-Headers: Authorization, Content-Type, On-behalf-of, x-sg-elas-acl
Access-Control-Max-Age: 600
X-No-CORS-Reason: https://sendgrid.com/docs/Classroom/Basics/API/cors.html
Strict-Transport-Security: max-age=600; includeSubDomains
Date: Thu, 24 Nov 2022 07:37:40 GMT
Server: nginx

Any ideas why SetFrom need real email not fictional and how to avoid it?


Solution

  • According SendGrid the sender email(email what is sent FROM) must be verified by SendGrid to send email from it. Otherwise request throws error. Why this click