Sending emails with SendGrid
Works just fine on my local (code below) No source control, just a straight WebDeploy of the same code to AzureWebsites yeilds the error below. SendGrid support have no clue.
Exception Details: System.Net.Sockets.SocketException:
[SocketException (0x271d): An attempt was made to access a socket in a way forbidden by its access permissions]
CodeScales.Http.HttpClient.Navigate(HttpRequest request, HttpBehavior httpBehavior) +772 CodeScales.Http.HttpClient.Execute(HttpRequest request) +45 SendGridMail.Transport.REST.Deliver(ISendGrid message) +115
public bool SendEmail(string from, string to, string subject, string content)
{
var myMessage = SendGrid.GenerateInstance();
// Setup the email properties.
myMessage.From = new MailAddress(from);
myMessage.AddTo(to);
myMessage.Text = content;
myMessage.Subject = subject;
var username = "#####";
var pswd = "#####";
var credentials = new NetworkCredential(username, pswd);
// Get REST instance for sending email.
var transportREST = REST.GetInstance(credentials);
// Send the email.
transportREST.Deliver(myMessage);
return true;
}
You can use the SendGrid APIs and SDKs to start sending emails within minutes. You could also use the SMTP protocol if you already have an existing codebase that relies on SMTP. In this post, you will learn how to send emails using the SendGrid .NET SDK and a .NET 6 console application.
When the SendGrid API responds with a successful HTTP status code, that does not necessarily mean that the email was successfully received by the recipient. It does however mean that the SendGrid API has accepted your request and will send the email. If the recipient does not receive the email, you can use the Email Activity Feed to find out why.
If a from header is not included, SendGrid will block your email because it doesn’t follow RFC 5322 compliance guidelines. Note that the name should be wrapped in quotation marks, and the address should be wrapped in a greater than and less than symbol. Include a subject line and press Enter.
Twilio SendGrid helps you send and receive emails at scale. You verified your email address as a single sender to send emails from. You used the default host builder to set up flexible configuration and extensible dependency injection. Using what you learned here, you can also start integrating SendGrid into your ASP.NET Core applications.
This looks like some sort of Windows Azure bug rather than a SendGrid issue. My two recommendations for fixing this are:
1. Try using the SMTP API instead of the web API to send the email
It could be that there's something wrong with outbound HTTP connections (this would really surprise me, but hey, it happens).
2. Try using the code that Azure provides
Azure has pretty good docs on how to use SendGrid, it might be worth taking a look at those and using some of their sample code.
http://www.windowsazure.com/en-us/develop/net/how-to-guides/sendgrid-email-service/
3. Contact Azure support
They can look under the hood and see what it is that you're doing to cause the error. If you do this, make sure you post back what they say here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With