使用SendGrid从Azure发送带附件的电子邮件

时间:2018-04-15 21:26:15

标签: asp.net vba azure sendgrid

如何使用Azure中的VM从SendGrid发送包含附件的电子邮件?

我添加了myMsg.AddAttachment并提供了参数。我执行时没有错误,但电子邮件永远不会被发送。如果我注释掉这一行, 电子邮件发送。

我做错了什么?这是在ASP.net VB。

1 个答案:

答案 0 :(得分:1)

我使用Sendgrid 9.9.0来检查此问题,以下是代码段:

'instantiate the SendGridMessage object 
Dim sendGridMessage = New SendGridMessage()
sendGridMessage.From = New EmailAddress("xxxxxx@hotmail.com", "BruceChen1019")
sendGridMessage.AddTo(New EmailAddress("xxxxx@gmail.com", "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

此外,对于附件文件,您需要根据文件扩展名为attachment.Type设置正确的MIME类型,并对文件内容进行Base64编码。

此外,您需要关注Attachments limitations。您可以关注evilSnobu关于转到SendGrid门户网站以解决此问题的评论。