Lino Vlacic
Lino Vlacic

Reputation: 21

sending email with attachment from Azure using SendGrid

How do I send an email with an attachment using SendGrid, from my VM in Azure?

I added myMsg.AddAttachment and supplied the parameters. I get no errors when I execute, but Email never gets sent. If I comment out this line, email gets sent.

What am I doing wrong? This is in ASP.net VB.

Upvotes: 2

Views: 1663

Answers (1)

Bruce Chen
Bruce Chen

Reputation: 18465

I used Sendgrid 9.9.0 to check this issue, here is the code snippet:

'instantiate the SendGridMessage object 
Dim sendGridMessage = New SendGridMessage()
sendGridMessage.From = New EmailAddress("[email protected]", "BruceChen1019")
sendGridMessage.AddTo(New EmailAddress("[email protected]", "Bruce Chen"))
sendGridMessage.PlainTextContent = "Hello World!"
sendGridMessage.Subject = "Hello Email!"

'instantiate the Attachment object
Dim attachment = New Attachment()
attachment.Filename = "helloword.txt"
attachment.Content = Convert.ToBase64String(Encoding.UTF8.GetBytes("hello world!!!"))
attachment.Type = "text/plain"
Dim attachments = New List(Of Attachment)
attachments.Add(attachment)
sendGridMessage.AddAttachments(attachments)

'send the email
Dim Response = client.SendEmailAsync(sendGridMessage).Result
Console.WriteLine(Response.StatusCode) //202 Accepted

Console.ReadLine()

TEST:

enter image description here

Moreover, for your attachment file, you need to set the correct MIME Type for attachment.Type based on the file extension, and Base64 encode your file content.

Also, you need to follow the Attachments limitations. And you may follow evilSnobu's comment about going to SendGrid portal for troubleshooting this issue.

Upvotes: 1

Related Questions