65
loading...
This website collects cookies to deliver better user experience
mkdir SendGridMail
cd SendGridMail
dotnet new console
dotnet run
dotnet add package SendGrid
using SendGrid;
using SendGrid.Helpers.Mail;
var apiKey = Environment.GetEnvironmentVariable("SENDGRID_API_KEY");
var client = new SendGridClient(apiKey);
var msg = new SendGridMessage()
{
From = new EmailAddress("[REPLACE WITH YOUR EMAIL]", "[REPLACE WITH YOUR NAME]"),
Subject = "Sending with Twilio SendGrid is Fun",
PlainTextContent = "and easy to do anywhere, especially with C#"
};
msg.AddTo(new EmailAddress("[REPLACE WITH DESIRED TO EMAIL]", "[REPLACE WITH DESIRED TO NAME]"));
var response = await client.SendEmailAsync(msg);
// A success status code means SendGrid received the email request and will process it.
// Errors can still occur when SendGrid tries to send the email.
// If email is not received, use this URL to debug: https://app.sendgrid.com/email_activity
Console.WriteLine(response.IsSuccessStatusCode ? "Email queued successfully!" : "Something went wrong!");
Main
method of the Program
class if you are using older versions of .NET or older templates of .NET."[REPLACE WITH YOUR EMAIL]"
with the email address you verified with your SendGrid account."[REPLACE WITH YOUR NAME]"
with your name."[REPLACE WITH DESIRED TO EMAIL]"
with the email address you want to send an email towards."[REPLACE WITH DESIRED TO NAME]"
with the recipient's name.apiKey
variable.SendGridClient
with the apiKey
.SendGridMessage
object with your email address as sender, and with a subject, and text content.SendGridMessage
to the SendGrid API.export SENDGRID_API_KEY=[REPLACE WITH YOUR API KEY]
$Env:SENDGRID_API_KEY = "[REPLACE WITH YOUR API KEY]"
set SENDGRID_API_KEY="[REPLACE WITH YOUR API KEY]"
dotnet run
using SendGrid;
using SendGrid.Helpers.Mail;
var apiKey = Environment.GetEnvironmentVariable("SENDGRID_API_KEY");
var client = new SendGridClient(apiKey);
Console.Write("To: ");
var toEmail = Console.ReadLine();
Console.Write("Subject: ");
var subject = Console.ReadLine();
Console.Write("Body: ");
var body = Console.ReadLine();
var msg = new SendGridMessage()
{
From = new EmailAddress("[REPLACE WITH YOUR EMAIL]", "[REPLACE WITH YOUR NAME]"),
Subject = subject,
PlainTextContent = body
};
msg.AddTo(new EmailAddress(toEmail));
var response = await client.SendEmailAsync(msg);
// A success status code means SendGrid received the email request and will process it.
// Errors can still occur when SendGrid tries to send the email.
// If email is not received, use this URL to debug: https://app.sendgrid.com/email_activity
Console.WriteLine(response.IsSuccessStatusCode ? "Email queued successfully!" : "Something went wrong!");
Console.ReadLine
to get the subject, body, and recipient email address. Run the project again and answer the prompts:dotnet run
dotnet add package Microsoft.Extensions.Hosting
dotnet add package Microsoft.Extensions.Configuration.UserSecrets
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using SendGrid;
using SendGrid.Helpers.Mail;
using IHost host = Host.CreateDefaultBuilder(args).Build();
var config = host.Services.GetRequiredService<IConfiguration>();
var apiKey = config.GetValue<string>("SendGridApiKey");
var fromEmail = config.GetValue<string>("FromEmail");
var fromName = config.GetValue<string>("FromName");
if(string.IsNullOrEmpty(apiKey)) throw new Exception("SendGridApiKey should not be null or empty");
if(string.IsNullOrEmpty(fromEmail)) throw new Exception("FromEmail should not be null or empty");
if(string.IsNullOrEmpty(fromName)) throw new Exception("FromName should not be null or empty");
Console.Write("To:");
var toEmail = Console.ReadLine();
Console.Write("Subject:");
var subject = Console.ReadLine();
Console.Write("Body:");
var body = Console.ReadLine();
var client = new SendGridClient(apiKey);
var msg = new SendGridMessage()
{
From = new EmailAddress(fromEmail, fromName),
Subject = subject,
PlainTextContent = body
};
msg.AddTo(new EmailAddress(toEmail));
var response = await client.SendEmailAsync(msg);
// A success status code means SendGrid received the email request and will process it.
// Errors can still occur when SendGrid tries to send the email.
// If email is not received, use this URL to debug: https://app.sendgrid.com/email_activity
Console.WriteLine(response.IsSuccessStatusCode ? "Email queued successfully!" : "Something went wrong!");
Host.CreateDefaultBuilder(args).Build();
will use multiple configuration sources by default, but most importantly:{
"FromEmail": "[ENTER YOUR FROM EMAIL]",
"FromName": "[ENTER YOUR FROM NAME]"
}
dotnet user-secrets init
dotnet user-secrets set SendGridApiKey [PASTE IN YOUR SENDGRID API KEY]
dotnet user-secrets set FromEmail [ENTER YOUR FROM EMAIL]
dotnet run --environment Development
dotnet add package SendGrid.Extensions.DependencyInjection
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using SendGrid;
using SendGrid.Helpers.Mail;
using SendGrid.Extensions.DependencyInjection;
using IHost host = Host.CreateDefaultBuilder(args)
.ConfigureServices((context, services) => services.AddSendGrid(options =>
options.ApiKey = context.Configuration.GetValue<string>("SendGridApiKey")
))
.Build();
var config = host.Services.GetRequiredService<IConfiguration>();
var apiKey = config.GetValue<string>("SendGridApiKey");
var fromEmail = config.GetValue<string>("FromEmail");
var fromName = config.GetValue<string>("FromName");
if(string.IsNullOrEmpty(apiKey)) throw new Exception("SendGridApiKey should not be null or empty");
if(string.IsNullOrEmpty(fromEmail)) throw new Exception("FromEmail should not be null or empty");
if(string.IsNullOrEmpty(fromName)) throw new Exception("FromName should not be null or empty");
Console.Write("To: ");
var toEmail = Console.ReadLine();
Console.Write("Subject: ");
var subject = Console.ReadLine();
Console.Write("Body: ");
var body = Console.ReadLine();
var client = host.Services.GetRequiredService<ISendGridClient>();
var msg = new SendGridMessage()
{
From = new EmailAddress(fromEmail, fromName),
Subject = subject,
PlainTextContent = body
};
msg.AddTo(new EmailAddress(toEmail));
var response = await client.SendEmailAsync(msg);
// A success status code means SendGrid received the email request and will process it.
// Errors can still occur when SendGrid tries to send the email.
// If email is not received, use this URL to debug: https://app.sendgrid.com/email_activity
Console.WriteLine(response.IsSuccessStatusCode ? "Email queued successfully!" : "Something went wrong!");
ConfigureServices
.AddSendGrid
. The lambda-inception can be tricky, so make sure to verify all your parentheses match the code above.SendGridClient
as you did before, the program now requests an instance of ISendGridClient
from the dependency injection container using host.Services.GetRequiredService<ISendGridClient>()
.dotnet run --environment Development