【问题标题】:C# SendGrid - SendAsync Method ErrorC# SendGrid - SendAsync 方法错误
【发布时间】:2017-07-05 12:33:59
【问题描述】:

我已按照教程在我的 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) 都是有效的,但它不适合我。

我错过了什么吗?

谢谢, 约翰

【问题讨论】:

  • 您使用的 .net 和 sendgrid 版本是什么?可能是版本不匹配问题。
  • .Net Framework 4.5.2 和 SendGrid.9.5.0
  • 如果将new Web 替换为new SendGrid.Web 会发生什么?
  • 我收到错误消息
  • 你试过这个不使用Websendgrid.com/docs/Integrate/Code_Examples/v3_Mail/csharp.html的示例代码吗?

标签: c# asp.net sendgrid


【解决方案1】:

您可以使用SmtpClient 为凭据创建一个实例。

var Smtp = new SmtpClient()
{
    Credentials = new NetworkCredential(ConfigurationManager.AppSettings["mailAccount"],
                    ConfigurationManager.AppSettings["mailPassword"])
};
...
await Smtp.SendAsync(myMessage);

【讨论】:

    【解决方案2】:

    如果您使用 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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-04
      • 1970-01-01
      • 1970-01-01
      • 2014-10-09
      • 2013-11-08
      • 1970-01-01
      相关资源
      最近更新 更多