在控制台应用程序中使用SendGrid [英] Using SendGrid in a console application

查看:45
本文介绍了在控制台应用程序中使用SendGrid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Is it possible to use Send Grid in Console Application in C#? My code doesn't work and I really do not know why. Could you help me?

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

推荐答案

关于代码的点对:

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

等待transportWeb.DeliverAsync(mail).ConfigureAwait(true);

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

因此,请尝试使用此固定代码,让我们知道它的运行方式:

So try this fixed code and let us know how it goes:

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

这篇关于在控制台应用程序中使用SendGrid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆