0

I have an ASP.NET website published to Azure from which emails can be sent. Some are plain text but I added a welcome email that is newsletter style with embedded images. The code is VB.NET. I have the system working nicely on our development server displaying a preview before sending the email. On Azure, the code to send the newsletter email is not working. The plain text email goes OK and I have tested the newsletter as an HTML email without the embedded images and that goes through OK. The preview can find the images so I am sure they are there and can be accessed. I do not get any error message, the email just never shows up in the sendgrid account as being processed. The code is as follows:

     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

The array of images is populated with a number of images located in a folder as they don't change, like so:

     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

I have checked the web and can find others who have problems where the code works on the local server but not on Azure but no answers that help with this specific questions. It must be to do with the way I am handling the images but I can't see it. Have reviewed 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

Vijayanath Viswanathan
  • 8,027
  • 3
  • 25
  • 43
  • I'm not familiar with this library, but `transportweb.DeliverAsync(mymessage)` seems awful fishy. Seems like you're making an async call and then not waiting for it to finish. – mason Oct 02 '17 at 14:00
  • From what I have seen in github you only need transportweb.DeliverAsync(mymessage).wait() for a console app and it doesn't work in a web app just causing it to hang – Oscar_Shepherd Oct 03 '17 at 08:42
  • In case anyone else comes across this I have resolved it by setting the path at the time of adding the attachment instead of setting it up in the array. – Oscar_Shepherd Oct 03 '17 at 09:34

1 Answers1

0

The answer is to change the addattachment code to this :

    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