MVC using .NET 4.5.2. Code below taken from: http://www.asp.net/mvc/overview/security/create-an-aspnet-mvc-5-web-app-with-email-confirmation-and-password-reset
using System;
using SendGrid;
using System.Net;
using System.Configuration;
using System.Diagnostics;
public class EmailService : IIdentityMessageService
{
public async Task SendAsync(IdentityMessage message)
{
await configSendGridasync(message);
}
// Use NuGet to install SendGrid (Basic C# client lib)
private async Task configSendGridasync(IdentityMessage message)
{
var myMessage = new SendGridMessage();
myMessage.AddTo(message.Destination);
myMessage.From = new System.Net.Mail.MailAddress(
"[email protected]", "Joe S.");
myMessage.Subject = message.Subject;
myMessage.Text = message.Body;
myMessage.Html = message.Body;
var credentials = new NetworkCredential(
ConfigurationManager.AppSettings["mailAccount"],
ConfigurationManager.AppSettings["mailPassword"]
);
// Create a Web transport for sending email.
var transportWeb = new Web(credentials);
// Send the email.
if (transportWeb != null)
{
await transportWeb.DeliverAsync(myMessage);
}
else
{
Trace.TraceError("Failed to create Web transport.");
await Task.FromResult(0);
}
}
}

SendGridMessage will not work... typing SendGrid. only shows up with CSharp, Helpers, and SendGridAPIClient.
Please advise. Thank you.
Hello @metamet,
That example is using our v2 API, which can be installed like this: https://github.com/sendgrid/sendgrid-csharp/blob/master/TROUBLESHOOTING.md#continue-using-v2
Please let me know if you have further issue.
Otherwise, if you want to use the v3 API, please follow the example here: https://github.com/sendgrid/sendgrid-csharp
Thanks!
very helpful thank you !
@thinkingserious,
im using this same methot on my application, so i have to use the API V2.
The question is, the API V2 will always works or in a future i will have to upgrade to V3?
Thanks
@fabiojansenbr,
We have no plans to drop support for our v2 API; however, we are not supporting the v2 libraries any more. Additionally, new features will only come to the v3 API moving forward.
I hope that helps. Thanks!
@thinkingserious Thanks for reply. I will continue using V2.
:-)
@thinkingserious,
Are there any examples of how to make the latest version work with the MVC example posted above?
The free product does not provide API key, so those examples are not helpful.
Hello @RoyCross,
You can generate an API key here: https://app.sendgrid.com/settings/api_keys
I hope that helps!
Thanks! That does help. What changed? Previously, when I used that link, I got a panel that stated that option was not available with my account.
Edit:
I Just received this from SendGrid Support. Thanks for the VERY speedy response!
Thank you for reaching out to SendGrid Support. Our Free accounts do have access to API keys, there was an issue with the scopes of your account in our backend, I have reset them. You should now see the option under Settings to create an API key.
Please let us know if you have any additional questions in regards to this.
Kindly,
Emily R.
SendGrid Support
So, now is there a simple example like the one referenced above that utilises v3?
Hello @RoyCross,
Thanks for the follow up!
You may find the v3 "Hello Email" here: https://github.com/sendgrid/sendgrid-csharp#quick_start. Please let me know if you run into any issues.
Hi Elmer,
I am trying to attach an excel from a folder in an email. Is there a way in v3 to pick the file from a path which I can specify like it is present in v2 ? Could you please share a link for some reference in this regard ?
Hello @robin-cg,
The following code worked for me using v9 of this SDK:
using SendGrid;
using SendGrid.Helpers.Mail;
using System;
using System.Threading.Tasks;
namespace Example
{
internal class Example
{
private static void Main()
{
Execute().Wait();
}
static async Task Execute()
{
var apiKey = Environment.GetEnvironmentVariable("SENDGRID_API_KEY");
var client = new SendGridClient(apiKey);
var msg = new SendGridMessage()
{
From = new EmailAddress("[email protected]", "DX Team"),
Subject = "Hello World from the SendGrid CSharp SDK!",
PlainTextContent = "Hello, Email!",
HtmlContent = "<strong>Hello, Email!</strong>"
};
msg.AddTo(new EmailAddress("[email protected]", "Test User"));
byte[] excelArray = System.IO.File.ReadAllBytes(@"/Users/ethomas/Downloads/Book 2.xlsx");
string base64ExcelRepresentation = Convert.ToBase64String(excelArray);
msg.AddAttachment("Sheet.xlsx", base64ExcelRepresentation, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
var response = await client.SendEmailAsync(msg);
}
}
}
Most helpful comment
very helpful thank you !