【问题标题】:Sending email with embedded images using Sendgrid on Azure在 Azure 上使用 Sendgrid 发送带有嵌入图像的电子邮件
【发布时间】:2018-03-13 14:25:39
【问题描述】:

我有一个发布到 Azure 的 ASP.NET 网站,可以从该网站发送电子邮件。有些是纯文本,但我添加了一封欢迎电子邮件,它是带有嵌入图像的时事通讯样式。代码是 VB.NET。我的系统在我们的开发服务器上运行良好,在发送电子邮件之前显示预览。在 Azure 上,发送时事通讯电子邮件的代码不起作用。纯文本电子邮件正常,我已经将时事通讯作为没有嵌入图像的 HTML 电子邮件进行了测试,并且正常。预览可以找到图像,所以我确定它们在那里并且可以访问。我没有收到任何错误消息,电子邮件只是从未在 sendgrid 帐户中显示为正在处理中。代码如下:

     Try
        Dim mymessage = New SendGridMessage
        mymessage.From = New MailAddress("do-not-reply@company.co.uk")

        mymessage.AddTo(txtemail.Text)

        mymessage.Subject = "Welcome Email"

        mymessage.Text = plaintext
        mymessage.Html = htmlBody

        Dim arrct As Integer = arrImages.Count - 1
        For i As Integer = 0 To arrct
            mymessage.AddAttachment(arrImages(i).ipath)
            mymessage.EmbedImage(arrImages(i).fname, arrImages(i).id)
        Next

        Dim username = ConfigurationManager.AppSettings("emailServiceUserName")
        Dim pswd = ConfigurationManager.AppSettings("emailServicePassword")

        Dim credentials = New NetworkCredential(username, pswd)
        Dim transportweb = New Web(credentials)

        transportweb.DeliverAsync(mymessage)

        'code here to display success message

    Catch exc As Exception
        'error code here
    End Try

图像数组由位于文件夹中的许多图像填充,因为它们不会更改,如下所示:

     Dim research As String = Server.MapPath("~\ImageTemp\" + query.ImageName)
                        'Extend the array
                        ReDim Preserve arrImages(i + 1)
                        arrImages(i + 1).ipath = respath
                        arrImages(i + 1).fname = qry.ImageName
                        arrImages(i + 1).id = "img" & i + 1

我已经检查了网络,可以找到其他人在代码在本地服务器上运行但在 Azure 上运行时遇到问题,但没有有助于解决这个特定问题的答案。这一定与我处理图像的方式有关,但我看不到它。 已审核 Unable to send emails on Azure Web App using Sendgrid How to send embedded images with sendgrid emails? Sending an email with attachment using SendGrid

【问题讨论】:

  • 我不熟悉这个库,但transportweb.DeliverAsync(mymessage) 看起来很可疑。似乎您正在进行异步调用,然后没有等待它完成。
  • 从我在 github 中看到的情况来看,控制台应用程序只需要 transportweb.DeliverAsync(mymessage).wait() 并且它在 Web 应用程序中不起作用,只会导致它挂起
  • 如果其他人遇到这个问题,我已经通过在添加附件时设置路径而不是在数组中设置它来解决它。

标签: asp.net vb.net azure email sendgrid


【解决方案1】:

答案是把addattachment代码改成这样:

    Dim arrct As Integer = arrImages.Count - 1
    For i As Integer = 0 To arrct
        mymessage.AddAttachment(Server.Mappath("~\ImageTemp\" & arrImages(i).fname)
        mymessage.EmbedImage(arrImages(i).fname, arrImages(i).id)
    Next

【讨论】:

    猜你喜欢
    • 2013-03-23
    • 1970-01-01
    • 2012-04-07
    • 1970-01-01
    • 2021-10-31
    • 2012-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多