使用Azure上的Sendgrid发送包含嵌入图像的电子邮件

时间:2017-10-02 13:49:24

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

我有一个发布到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

1 个答案:

答案 0 :(得分:0)

答案是将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