C# 在控制台应用程序中使用SendGrid
是否可以在C#中的控制台应用程序中使用发送网格?我的代码不起作用,我真的不知道为什么。你能帮我吗C# 在控制台应用程序中使用SendGrid,c#,azure,sendgrid,C#,Azure,Sendgrid,是否可以在C#中的控制台应用程序中使用发送网格?我的代码不起作用,我真的不知道为什么。你能帮我吗 using System; using System.Net; using System.Net.Mail; using SendGrid; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { // Create t
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();
}
}
}
尝试使用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);
}
使用(var-client=new-HttpClient())
{
client.BaseAddress=新Uri(“https://api.sendgrid.com");
var content=newformurlencodedcontent(new[]
{
新的KeyValuePair(“api_密钥”、“您的密码”),
新的KeyValuePair(“api_用户”、“用户名”),
新的KeyValuePair(“从”some@body.com"),
新的KeyValuePair(“到”some@body.com"),
新的KeyValuePair(“主题”、“任意”),
新的KeyValuePair(“文本”,“您可以使用'html':)”)
});
var result=client.PostAsync(“/api/mail.send.json”,content.result);
字符串resultContent=result.Content.ReadAsStringAsync().result;
Debug.WriteLine(resultContent);
}
将值更改为您的值,这对我有用。关于代码的几点:
- 您没有等待DeliverAsync()调用-这可能会导致某些问题。可以这样称呼:
await-transportWeb.DeliverAsync(邮件).ConfigureAwait(true)代码>
- 如果Im错误,请更正我,但在Azure上,您无法使用网络凭据,因为它们未正确发送到SendGrid API。我们正在使用API密钥配置transportWeb对象李>
因此,请尝试此固定代码,并让我们知道它是如何运行的:
var transportWeb = new Web(<your-api-key-here>);
await transportWeb.DeliverAsync(myMessage).ConfigureAwait(true);
var transportWeb=newweb();
等待transportWeb.DeliverAsync(myMessage).ConfigureAwait(true);
实际尝试一下:
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);
}
static void Main()
{
sendmail().Wait();
}
静态异步任务sendmail()
{
var myMessage=new SendGridMessage();
myMessage.From=新邮件地址(“发件人”);
myMessage.AddTo(“收件人电子邮件”);
myMessage.Subject=“测试SendGrid库”;
myMessage.Html=“你好,世界!”;
myMessage.Text=“你好,世界纯文本!”;
var凭证=新网络凭证(“sendgrid\u用户”、“sendgrid\u通行证”);
var transportWeb=新站点(凭证);
等待transportWeb.DeliverAsync(myMessage);
}
看起来您应该等待DeliveryAsync方法。在Main方法中,当您删除Wait()时,它将不发送电子邮件。如果您使用Wait(),它将发送。它在控制台应用程序中确实可以工作,但出于某些原因,您需要这样调用它:
transportWeb.DeliverAsync(msg.Wait()
更多信息:您是否有任何错误?什么事没有发生?您使用调试器完成了吗?在不等待的情况下调用方法时,我也有类似的行为。它只是没有发送任何错误的电子邮件。当我等待DeliveryAsync方法时,它会开始发送电子邮件。这不是你问题的答案(这应该是可能的,其他人已经解决了),但你也可以使用Sendgrid中的常规.NET SMTP内容。相关:有没有附件的想法?最好,省去我的一天!