在控制台应用程序中使用SendGrid

4

在C#控制台应用程序中使用Send Grid是否可行?我的代码无法运行,我真的不知道为什么。你能帮我吗?

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();
        }
    }
}

你有收到任何错误吗?哪里出了问题?你用调试器逐步执行过代码吗? - Ben
当我在不使用await的情况下调用方法时,我也遇到了类似的行为。它根本没有发送电子邮件,也没有任何错误。当我使用await DeliveryAsync方法时,它开始发送电子邮件。 - kat1330
这不是你问题的答案(应该是可行的,其他人已经解决了它),但你也可以使用常规的.NET SMTP代码与Sendgrid一起使用。 - Casey
相关链接:https://dev59.com/eYDba4cB1Zd3GeqPEXOF - Jon Schneider
4个回答

4

关于您的代码,有以下几点需要注意:

  • 您没有在DeliverAsync()调用上使用await - 这可能会导致某些问题。请按如下方式调用:

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);

1

1

Actually try this:

    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(),它将会发送。


0
尝试使用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);
        }

将值更改为您自己的值,这对我有效。


有关附件的任何想法? - Steve

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接