I've just signed up with SendGrid and it looks like a nice product. It works fine for the scenarios I've tried but now I need to add some automated integration tests.
I see that SendGrid has something called Sandbox that can be enabled and it will return OK status when sending emails if the email validations are correct, but the email won't reach any inbox nor it will generate a cost (no credits consumed). In theory, that sounds perfect for what I want.
I've tried it successfully with a full access API key, but I don't want to add to my source code a real API Key. I want to add the most restricted possible one (just for email testing).
I've seen I can configure the scope for the API Keys and there's an option called Testing Email but when I send emails in sandbox mode with that restricted key, I get an Unauthorized error.
The question is: can I configure a very restricted Api Key just for simple email tests with sandbox mode in SendGrid?
I'm using the .NET client and this is my code, but this should be irrelevant I guess..
public async Task SendEmail(
string fromEmail,
string fromName,
string toEmail,
string subject,
string plainTextContent,
string htmlContent)
{
var apiKey = "ThisIsTheApiKeyWithRestrictedPermissionsGeneratedAtMySendGridDashboard"
var client = new SendGridClient(apiKey);
var sandboxMode =
new SandboxMode
{
Enable = true
};
var mailSettings =
new MailSettings
{
SandboxMode = sandboxMode
};
var from = new EmailAddress(fromEmail, fromName);
var to = new EmailAddress(toEmail);
var message = MailHelper.CreateSingleEmail(from, to, subject, plainTextContent, htmlContent);
message.MailSettings = mailSettings;
var result = await client.SendEmailAsync(message);
}