简体   繁体   English

使用 c# 在 Windows Azure 上通过电子邮件发送附件

[英]Send Attachment in Email on Windows Azure with c#

I am trying to send email from AZURE.我正在尝试从 AZURE 发送电子邮件。 I successfully send without attachment email.我成功发送了没有附件的电子邮件。 When I send email with attachment, Below issue I face while download attachment.当我发送带有附件的电子邮件时,我在下载附件时遇到以下问题。

  • When I open that attachment it has 0 bytes.当我打开该附件时,它有 0 个字节。 I can not find any content in it.我在里面找不到任何内容。 While I download from my azure portal then I see the content perfectly.当我从我的 azure 门户下载时,我可以完美地看到内容。

For attachment I upload PDF file on blob and download it and then add attachment.对于附件,我在 blob 上上传 PDF 文件并下载它,然后添加附件。 My code as below.我的代码如下。

var account = new CloudStorageAccount(new StorageCredentials("accountName", "keyvalue"), true);
CloudBlobClient blobClient = account.CreateCloudBlobClient();
CloudBlobContainer container =blobClient.GetContainerReference("containername");
CloudBlockBlob blobread = container.GetBlockBlobReference(Session["UploadPDFFile"].ToString());
MemoryStream msRead = new MemoryStream();                                
using (msRead)
{
  msRead.Position = 0;
  blobread.DownloadToStream(msRead);

  objMailMessgae.Attachments.Add(new System.Net.Mail.Attachment(msRead,    Session["UploadPDFFile"].ToString(), "pdf/application"));

   try
   {
     objSmtpClient.Send(objMailMessgae);
   }
   catch (Exception ex) {
            string s = ex.Message;
        }
    }

You would need to reset the memory stream's position to 0 after you have read blob's content into it.在将 blob 的内容读入其中后,您需要将内存流的位置重置为0 So essentially your code would be:所以基本上你的代码是:

var account = new CloudStorageAccount(new StorageCredentials("accountName", "keyvalue"), true);
CloudBlobClient blobClient = account.CreateCloudBlobClient();
CloudBlobContainer container =blobClient.GetContainerReference("containername");
CloudBlockBlob blobread = container.GetBlockBlobReference(Session["UploadPDFFile"].ToString());
MemoryStream msRead = new MemoryStream();                                
using (msRead)
{
  blobread.DownloadToStream(msRead);
  msRead.Position = 0;

  objMailMessgae.Attachments.Add(new System.Net.Mail.Attachment(msRead,    Session["UploadPDFFile"].ToString(), "pdf/application"));

   try
   {
     objSmtpClient.Send(objMailMessgae);
   }
   catch (Exception ex) {
            string s = ex.Message;
        }
    }

Give it a try.试一试。 It should work.它应该工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM