Azure上缺少电子邮件附件,可在本地使用

时间:2018-04-19 16:15:37

标签: azure azure-web-sites sendgrid mailmessage

我已经实现了类似于以下问题的东西,我可以让它在我的服务器上本地工作,但是当我部署到Azure时它不起作用。我没有收到任何错误:只是没有附件的电子邮件。

Sending attachments using Azure blobs

是否可以使用SendGrid发送哪些类型的文件(文件只有56k)? Azure App服务必须处于特定级别还是可以在Basic上完成? blob URL definitley存在,我将流设置为零,如前一个问题所示。

MailMessage mm = new MailMessage("receiver address", "someone");
            mm.From = new MailAddress("myAddress", "My Name");
            mm.Subject = content.Subject;
            mm.Body = content.Body;
            mm.IsBodyHtml = true;
            mm.BodyEncoding = UTF8Encoding.UTF8;
            mm.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;

            var attachmentAsStream = _storageAccessService.GetAssetAsStreamForEmail("myContainer", "fileThatExists.pdf");

            var attachment = new Attachment(attachmentAsStream, "File.pdf", MediaTypeNames.Application.Pdf);
                mm.Attachments.Add(attachment);


public MemoryStream GetAssetAsStreamForEmail(string containerName, string fileName)
        {
            // Create the blob client.
            CloudBlobClient blobClient = StorageAccountReference().CreateCloudBlobClient();

            // Retrieve reference to a previously created container.
            CloudBlobContainer container = blobClient.GetContainerReference(containerName);

            CloudBlockBlob blob = container.GetBlockBlobReference(fileName);
            var memoryStream = new MemoryStream();

            try
            {
                using (var stream = new MemoryStream())
                {
                    blob.DownloadToStream(memoryStream);
                    memoryStream.Seek(0, SeekOrigin.Begin);
                }

            }
            catch (Exception ex)
            {
                Elmah.ErrorSignal.FromCurrentContext().Raise(new Exception("Failed to download Email Atatchment: "+ ex.Message));

            }
            memoryStream.Position = 0;
            return memoryStream;
        }

using (SmtpClient client = new SmtpClient())
                {
                    client.Port = 587;
                    client.Host = "smtp.sendgrid.net";
                    client.EnableSsl = true;
                    client.Timeout = 10000;
                    client.DeliveryMethod = SmtpDeliveryMethod.Network;
                    client.UseDefaultCredentials = false;
                    client.Credentials = new System.Net.NetworkCredential("hidden", "hidden");

                    await client.SendMailAsync(message);
                }

更新

在Yasir的建议下面更新。从Azure下载Blob作为Stream似乎只在本地工作。但是如果我改为以ByteArray的形式下载,那么它无处不在......但是......

public MemoryStream GetAssetAsStreamForEmail(string containerName, string fileName)
        {
            // Create the blob client.
            CloudBlobClient blobClient = StorageAccountReference().CreateCloudBlobClient();

            // Retrieve reference to a previously created container.
            CloudBlobContainer container = blobClient.GetContainerReference(containerName);

            CloudBlockBlob blob = container.GetBlockBlobReference(fileName);

            try
            {
                blob.FetchAttributes();
                var fileStream = new byte[blob.Properties.Length];
                for (int i = 0; i < blob.Properties.Length; i++)
                {
                    fileStream[i] = 0x20;
                }

                blob.DownloadToByteArray(fileStream, 0) ;
                MemoryStream bufferStream = new MemoryStream(fileStream);

            }
            catch (Exception ex)
            {
                Elmah.ErrorSignal.FromCurrentContext().Raise(new Exception("Failed to download Email Atatchment: " + ex.Message));

            }
            return null;
        }

1 个答案:

答案 0 :(得分:1)

以下代码适用于从Azure功能使用Sendgrid发送电子邮件。我从Blob Storage附加了一个CSV文件。它也适合你。您所要做的就是确保将pdf文件读作byte []。

$ sh -c 'awk -v line="'"$mark"'" '\''$0 == line {print NR}'\'' file'
3
5
8