如何解决使用C#的SendGrid V3 API无法从控制台应用程序发送邮件
我无法使用控制台应用程序中的V3 API通过SendGrid发送电子邮件。它在我的Web应用程序中工作正常。我创建了一个测试控制台项目,并使用了SendGrid站点中的示例代码。当调用发送消息的命令时,应用程序直接退出。我从通话中看不到任何回应。
这是在VB.Net中。抱歉,我认为我是地球上唯一用VB编码的人。
如果我在GitHub的示例sendgrid-csharp-main中执行此代码,它将正常工作。在示例代码中,我找不到与实际调用发送消息部分的代码有所不同的任何东西(除了不必转换为VB)。
有人在此代码中看到任何错误吗?我正在使用.Net 4.7.2框架。
这是测试代码的全部。我一直在转动车轮寻求帮助,时间太长了,我现在很绝望。
Imports System.Net.Http
Imports SendGrid
Imports SendGrid.Helpers.Mail
Module Module1
Sub Main()
TryIt().ConfigureAwait(True)
End Sub
Private Async Function TryIt() As Task
Dim HttpClient As HttpClient = New HttpClient()
Dim apiKey = "my private api key"
Dim client = New SendGridClient(HttpClient,New SendGridClientOptions With {
.ApiKey = apiKey,.HttpErrorAsException = True
})
Dim from = New EmailAddress("msie@msinvoiceExchange.com","MSIE")
Dim subject = "Hello World from the Twilio SendGrid CSharp Library Helper!"
Dim toEmail = New EmailAddress("joe@invoicesanywhere.com","Joe")
Dim plainTextContent = "Hello,Email from the helper [SendSingleEmailAsync]!"
Dim htmlContent = "<strong>Hello,Email from the helper! [SendSingleEmailAsync]</strong>"
Dim msg As New SendGridMessage With {
.From = from,.Subject = subject,.PlainTextContent = plainTextContent,.HtmlContent = htmlContent
}
msg.AddTo(toEmail)
Dim myResponse As Response
myResponse = Await client.SendEmailAsync(msg)
Console.WriteLine(msg.Serialize())
Console.WriteLine(myResponse.StatusCode)
Console.WriteLine(myResponse.Headers)
Console.WriteLine(vbLf & vbLf & "Press <Enter> to continue.")
Console.ReadLine()
End Function
End Module
解决方法
数字。一旦我绝望地提出一个问题,我就会发现一些可行的方法。
我只是希望其他人也有同样的问题,并认为此答案会有所帮助。
这是我必须进行的更改才能使其正常工作
Sub Main()
TryIt().Wait()
End Sub
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。