I\'m following the example from SendGrid\'s site and as credentials I\'m pasting it what they gave me in the Azure portal. Still, I get this error message.
Ok, I think I have the answer. Above code is fine. On SendGrid you must approve the sending email address. Log into your SendGrid portal and attend to the below:
Hope the above helps
I was able to get a SendGrid email send to happen successfully from a Console application using the SendGrid v3 C# API.
I first needed to import the SendGrid C# library via NuGet using the Package Manager Console in Visual Studio:
PM> Install-Package SendGrid
Then, my code:
using SendGrid;
using SendGrid.Helpers.Mail;
using System.Threading.Tasks;
// ...
public static async Task SendEmailViaSendGrid(string to, string from, string subject, string body)
{
// Generated from https://app.sendgrid.com/settings/api_keys
// TODO: Store this somewhere secure -- not in version control.
string apiKey = "YOUR_PRIVATE_SENDGRID_API_KEY_GOES_HERE";
dynamic sendGridClient = new SendGridAPIClient(apiKey);
Email fromEmail = new Email(from);
Email toEmail = new Email(to);
Content content = new Content("text/plain", body);
Mail mail = new Mail(fromEmail, subject, toEmail, content);
dynamic response = await sendGridClient.client.mail.send.post(requestBody: mail.Get());
}
I know this is an old post, but I found out why emails are not sent from a console application. When I add Console.Read(); at the end of the program.cs class, it sends the emails, but if I remove the Console.Read(); from the code, the emails do not send. So my assumption is, if I let the program wait a bit before it closes, the emails should send.
Not sure if this is the case for everyone, but I had to create a Sender. https://app.sendgrid.com/settings/sender_auth/senders. That would match mailMsg.From
Does this happen to be a console application? I've been testing SendGrid and found that when I try to send an email from a Console application the emails are never sent. However, when I tried it from a web application (using the same exact sending code) the emails are sent. I have no explanation for why the console application does not work.
public static bool SendMailBySendGrid(string offMail, string mailFormatDetails,string subject,string ccMail=null,string bccMail=null)
{
string mailFromId = System.Configuration.ConfigurationManager.AppSettings["Sender"];
SendGridMessage sndmsg = new SendGridMessage();
sndmsg.From = new MailAddress(mailFromId);
sndmsg.To = new MailAddress[] { new MailAddress(offMail) };
if (!string.IsNullOrEmpty(ccMail))
sndmsg.Cc = new MailAddress[] { new MailAddress(ccMail) };
if(!string.IsNullOrEmpty(bccMail))
sndmsg.Bcc = new MailAddress[] { new MailAddress(bccMail) };
sndmsg.Subject = subject;
sndmsg.Html = "<div>" + mailFormatDetails + "</div>";
bool result= SendEmail(sndmsg);
return result;
}
Include using SendGrid; in your project.