由于在json中使用双引号,使用vb.net中的csharp库通过SendGrid发送电子邮件失败

问题描述 投票:0回答:1

我正在尝试使用c#发送网格库https://www.nuget.org/packages/Sendgrid/9.12.0/通过sendgrid发送电子邮件。我实际上是使用vb.net来运行此代码。我已经联系了SendGrid支持,他们告诉我它生成的json中包含过多的双引号,这就是它失败的原因。

我本质上是在使用this c# example中的代码

但翻译成vb.net。我最终得到的代码是这个...

        psFrom = "[email protected]"
        psTo = "[email protected]"
        psSubject = "Email Test"
        psContent = "This is a test"
        psContent_TextFormat = "This is a test"
        Dim apiKey = "THISKEYISHIDDENINTHISEXAMPLE" 
        Environment.GetEnvironmentVariable("NAME_OF_THE_ENVIRONMENT_VARIABLE_FOR_YOUR_SENDGRID_KEY")
        Dim client = New SendGridClient(apiKey)
        Dim from = New EmailAddress(psFrom)
        Dim subject = psSubject
        Dim [to] = New EmailAddress(psTo)
        Dim plainTextContent = psContent_TextFormat
        Dim htmlContent = psContent
        Dim msg = MailHelper.CreateSingleEmail(from, [to], subject, plainTextContent, htmlContent)
        Dim response = client.SendEmailAsync(msg)

[当我使用此命令查看JSON吗?msg.Serialize时,我得到以下内容...

"{""from"":{""email"":""[email protected]""},""personalizations"":[{""to"":[{""email"":""[email protected]""}],""subject"":""Email Test""}],""content"":[{""type"":""text/plain"",""value"":""This is a test""},{""type"":""text/html"",""value"":""This is a test""}]}"

我不确定要在响应变量中填充什么值,因为它告诉我错误BC30109:'Response'是类类型,不能用作表达式。

为了这篇文章的缘故,我已经更改了电子邮件的to和from。但是,有人告诉我问题出在生成的JSON中的双引号上。有谁知道为什么要添加这些或如何删除它们?

c# vb.net sendgrid sendgrid-api-v3
1个回答
0
投票

好,我终于可以发送了。有2个问题。第一个是,一旦我将其更改为异步函数并正确调用了它,便能够查看response.StatusCode.ToString()。

一旦我能够查看我看到的响应错误

Win32Exception:客户端和服务器无法通信,因为它们不具有通用算法

WebException:基础连接已关闭:接收时发生意外错误。

我将其识别为tls问题,并且添加了该行之后

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12

有效!

这是我的完整代码...也许会帮助某人(我覆盖了用于测试的参数)

    SendEmailWithSendGrid(sMailgunFromAddress, sMailgunToAddress, sMailgunBccAddress, msEmailSubject, msEmailBody, msEmailBody_TextFormat, msEmailAttachment).Wait()

Private Async Function SendEmailWithSendGrid(psFrom As String, psTo As String, psBcc As String, psSubject As String, psContent As String, psContent_TextFormat As String, psEmailAttachment As String) As Task(Of String)
        Try
            'dummy sender for now
            psFrom = "[email protected]"
            psTo = "[email protected]"
            psSubject = "Email Test"
            psContent = "This is a test"
            psContent_TextFormat = "This is a test"
            Dim apiKey = "THISKEYISHIDDENINTHISEXAMPLE" ' Environment.GetEnvironmentVariable("NAME_OF_THE_ENVIRONMENT_VARIABLE_FOR_YOUR_SENDGRID_KEY")
            Dim client = New SendGridClient(apiKey)
            Dim from = New EmailAddress(psFrom)
            Dim subject = psSubject
            Dim [to] = New EmailAddress(psTo)
            Dim plainTextContent = psContent_TextFormat
            Dim htmlContent = psContent
            Dim msg = MailHelper.CreateSingleEmail(from, [to], subject, plainTextContent, htmlContent)

            'JNL 12/18/2018 force 1.2
            'ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12

            Dim response = Await client.SendEmailAsync(msg)
            Debug.Print(response.StatusCode.ToString())
        Catch ex As Exception
            mclSysLog.WriteSysLogEntry("SendEmail", "", ex.Message, "Error Occured using Mailgun:" & "psFrom=" & psFrom & "psTo=" & psTo)
        End Try
    End Function
© www.soinside.com 2019 - 2024. All rights reserved.