我已经按照教程在C#ASP.NET应用程序中设置了SendGrid。 我按照建议在IdentityConfig.cs中有以下代码:
using SendGrid;
using System.Net;
using System.Configuration;
using System.Diagnostics;
using Microsoft.AspNet.Identity;
using System.Threading.Tasks;
using SendGrid.Helpers.Mail;
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 EmailAddress(
"johnlowry484@gmail.com", "John Lowry");
myMessage.Subject = message.Subject;
myMessage.PlainTextContent = message.Body;
myMessage.HtmlContent = 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);
}
}
行var transportWeb = new Web(credentials);
导致错误:
无法找到类型或命名空间“Web”
我在网上看到的所有地方SendGrid.Web(credentials)
都有效,但不适合我。
我错过了什么吗?
谢谢, 约翰
答案 0 :(得分:0)
您可以使用SmtpClient
为凭据创建实例。
var Smtp = new SmtpClient()
{
Credentials = new NetworkCredential(ConfigurationManager.AppSettings["mailAccount"],
ConfigurationManager.AppSettings["mailPassword"])
};
...
await Smtp.SendAsync(myMessage);
答案 1 :(得分:0)
如果您使用V3,则需要参考文档 https://sendgrid.com/docs/Integrate/Code_Examples/v3_Mail/csharp.html
// using SendGrid's C# Library
// https://github.com/sendgrid/sendgrid-csharp
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("NAME_OF_THE_ENVIRONMENT_VARIABLE_FOR_YOUR_SENDGRID_KEY");
var client = new SendGridClient(apiKey);
var from = new EmailAddress("test@example.com", "Example User");
var subject = "Sending with SendGrid is Fun";
var to = new EmailAddress("test@example.com", "Example User");
var plainTextContent = "and easy to do anywhere, even with C#";
var htmlContent = "<strong>and easy to do anywhere, even with C#</strong>";
var msg = MailHelper.CreateSingleEmail(from, to, subject, plainTextContent, htmlContent);
var response = await client.SendEmailAsync(msg);
}
}
}
已编辑:
You can downgrade to 6.0.1 and it will work fine