2017-07-05 13 views
1

私はC#ASP.NETアプリケーションでSendGridをセットアップするチュートリアルに従ってきました。 示唆したように、私は私のIdentityConfig.csに次のコードを持っている:C#SendGrid - SendAsyncメソッドエラー

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(
          "[email protected]", "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);は、エラーの原因となっている。

どこでも

The type or namespace 'Web' could not be found

私がオンラインになり、SendGrid.Web(credentials)が有効であるが、それはありません私のために。

何か不足していますか?

おかげで、 ジョン

+0

マニュアルを参照してくださいする必要があり、あなたが使用していることをsendgrid V3を使用している場合は?バージョンの問題の不一致かもしれません。 – Sujith

+0

.Net Framework 4.5.2とSendGrid.9.5.0 – Johnathan

+0

'new web'を' new SendGrid.Web'に置き換えたらどうなりますか? – mjwills

答えて

0

あなたは資格証明書のインスタンスを作成するためにSmtpClientを使用することができます。

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

あなたは.NETのバージョンは何 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("[email protected]", "Example User"); 
      var subject = "Sending with SendGrid is Fun"; 
      var to = new EmailAddress("[email protected]", "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); 
     } 
    } 
} 

Edited : You can downgrade to 6.0.1 and it will work fine