我按照SendGrid's site中的例子,将他们在Azure门户中提供的内容作为凭据粘贴。但是,我仍然收到此错误消息。
消息= {“发送邮件失败。"}
InnerException = {“无法连接到远程服务器”}
我不清楚在这里要做什么,甚至不知道如何调试它。我正在从桌面运行代码(在我把它放在网站上之前),这样我就可以突破自己通过它。然而,没有喜悦...
有什么建议?
完整代码如下。
MailMessage mailMsg = new MailMessage();
mailMsg.To.Add(new MailAddress("to@example.com", "To Name"));
mailMsg.From = new MailAddress("from@example.com", "From Name");
mailMsg.Subject = "subject";
string text = "text body";
string html = @"<p>html body</p>";
mailMsg.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(
text, null, MediaTypeNames.Text.Plain));
mailMsg.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(
html, null, MediaTypeNames.Text.Html));
SmtpClient client = new SmtpClient("smtp.sendgrid.net", Convert.ToInt32(587));
System.Net.NetworkCredential credentials = new System.Net.NetworkCredential(
"azure_...@azure.com",
"LubX........pQc");
client.Credentials = credentials;
client.Send(mailMsg);
6条答案
按热度按时间amrnrhlw1#
这是一个控制台应用程序吗?我一直在测试SendGrid,发现当我尝试从控制台应用程序发送电子邮件时,电子邮件从来没有发送过。但是,当我尝试从一个Web应用程序(使用相同的发送代码)发送电子邮件时,电子邮件却发送了。我无法解释为什么控制台应用程序不工作。
nhn9ugyo2#
我能够使用SendGrid v3 C# API从控制台应用程序成功发送SendGrid电子邮件。
我首先需要使用Visual Studio中的包管理器控制台通过NuGet导入SendGrid C#库:
然后,我的代码:
x6492ojm3#
包括 * 使用SendGrid; * 在项目中。
lawou6xi4#
我知道这是一个老帖子,但我发现了为什么电子邮件不是从控制台应用程序发送的。当我添加Console.read();在program.cs类的末尾,它发送电子邮件,但是如果我删除Console.Read();从代码来看,电子邮件不会发送。所以我的假设是,如果我让程序在关闭前等待一段时间,电子邮件应该会发送。
2nbm6dog5#
不确定是否每个人都是这种情况,但我必须创建一个Sender. https://app.sendgrid.com/settings/sender_auth/senders。这将匹配
mailMsg.From
rjee0c156#
好的,我想我有答案了。上面的代码没有问题。在SendGrid上,您必须批准发送电子邮件地址。登录您的SendGrid门户并注意以下事项:
1.设置-〉发件人认证
1.在“Single Sender Verification”(单个发件人验证)下,添加所需的电子邮件地址
1.点击收件箱中的授权链接和你的鲍勃的侄子。
希望以上内容对您有所帮助