【问题标题】:Using SendGrid in a console application在控制台应用程序中使用 SendGrid
【发布时间】:2015-09-29 00:12:31
【问题描述】:

是否可以在 C# 的控制台应用程序中使用发送网格?我的代码不起作用,我真的不知道为什么。你可以帮帮我吗?

using System;
using System.Net;
using System.Net.Mail;
using SendGrid;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Create the email object first, then add the properties.
            var myMessage = new SendGridMessage();
            myMessage.From = new MailAddress("adresfrom@example.com", "It's me");
            myMessage.AddTo("adressj@gmail.com");
            myMessage.Subject = "Testing the SendGrid Library";
            myMessage.Text = "Content of the e-mail";

            var username = "my_sendgrid_username";
            var password = "my_sendgrid_password";

            var credentials = new NetworkCredential(username, password);

            // Create a Web transport for sending email.
            var transportWeb = new Web(credentials);
            transportWeb.DeliverAsync(myMessage);
            Console.ReadLine();
        }
    }
}

【问题讨论】:

  • 您是否遇到任何错误?什么没有发生?你用调试器单步执行了吗?
  • 在没有等待的情况下调用方法时我也有类似的行为。它根本不会在没有任何错误的情况下发送电子邮件。当我等待 DeliveryAsync 方法时,它开始发送电子邮件。
  • 这不是您问题的答案(应该是可能的,其他人已经解决了),但您也可以将常规 .NET SMTP 内容与 Sendgrid 一起使用。

标签: c# azure sendgrid


【解决方案1】:

关于您的代码的几点说明:

  • 您没有等待 DeliverAsync() 调用 - 这可能会导致某些问题。像这样称呼它:

await transportWeb.DeliverAsync(mail).ConfigureAwait(true);

  • 如果我错了,请纠正我,但在 Azure 上,您不能使用 NetworkCredentials,因为它们没有正确发送到 SendGrid API。我们正在使用 API 密钥来配置 transportWeb 对象。

所以试试这个固定的代码,让我们知道它是怎么回事:

var transportWeb = new Web(<your-api-key-here>);
await transportWeb.DeliverAsync(myMessage).ConfigureAwait(true);

【讨论】:

    【解决方案2】:

    实际上试试这个:

        static void Main()
        {
            SendEmail().Wait();
        }
    
        static async Task SendEmail()
        {
            var myMessage = new SendGridMessage();
            myMessage.From = new MailAddress("from_emal");
            myMessage.AddTo("to_email");
            myMessage.Subject = "Testing the SendGrid Library";
            myMessage.Html = "<p>Hello World!</p>";
            myMessage.Text = "Hello World plain text!";
    
            var credentials = new NetworkCredential("sendgrid_user", "sendgrid_pass");
            var transportWeb = new Web(credentials);
            await transportWeb.DeliverAsync(myMessage);            
        }
    

    看起来您应该等待 DeliveryAsync 方法。在 Main 方法中,当您删除 Wait() 时,它不会发送电子邮件。如果你使用 Wait() 它会发送。

    【讨论】:

      【解决方案3】:

      它确实在控制台应用程序中工作,但由于某种原因你需要这样调用它:

      transportWeb.DeliverAsync(msg).Wait()

      更多:https://github.com/sendgrid/sendgrid-csharp/issues/154

      【讨论】:

      • 最好,拯救了我的一天!
      【解决方案4】:

      尝试使用 SendGrid API 并通过 HttpClient 连接到它:

        using (var client = new HttpClient())
              {
                  client.BaseAddress = new Uri("https://api.sendgrid.com");
                  var content = new FormUrlEncodedContent(new[] 
              {
                  new KeyValuePair<string, string>("api_key", "YOUR PASSWORD"),
                  new KeyValuePair<string, string>("api_user", "USER NAME"),
                  new KeyValuePair<string, string>("from", "some@body.com"),
                  new KeyValuePair<string, string>("to", "some@body.com"),
                  new KeyValuePair<string, string>("subject", "Whatever"),
                  new KeyValuePair<string,string>("text", "You can use 'html' :)")
      
              });
                  var result = client.PostAsync("/api/mail.send.json", content).Result;
                  string resultContent = result.Content.ReadAsStringAsync().Result;
                  Debug.WriteLine(resultContent);
              }
      

      将值更改为您的值,这对我有用..

      【讨论】:

      • 附件有什么想法吗?
      猜你喜欢
      • 1970-01-01
      • 2017-07-04
      • 2010-09-06
      • 1970-01-01
      • 1970-01-01
      • 2010-11-04
      • 1970-01-01
      • 1970-01-01
      • 2016-09-28
      相关资源
      最近更新 更多