【问题标题】:Send Attachment in Email on Windows Azure with c#使用 c# 在 Windows Azure 上通过电子邮件发送附件
【发布时间】:2015-07-02 17:28:40
【问题描述】:

我正在尝试从 AZURE 发送电子邮件。我成功发送了没有附件的电子邮件。当我发送带有附件的电子邮件时,我在下载附件时遇到以下问题。

  • 当我打开该附件时,它有 0 个字节。我在里面找不到任何内容。当我从我的 azure 门户下载时,我可以完美地看到内容。

对于附件,我在 blob 上上传 PDF 文件并下载它,然后添加附件。我的代码如下。

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;
        }
    }

【问题讨论】:

    标签: c# email azure


    【解决方案1】:

    在将 blob 的内容读入其中后,您需要将内存流的位置重置为 0。所以基本上你的代码是:

    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;
            }
        }
    

    试一试。它应该可以工作。

    【讨论】:

      猜你喜欢
      • 2014-03-03
      • 1970-01-01
      • 2015-03-12
      • 2015-01-11
      • 1970-01-01
      • 1970-01-01
      • 2015-09-30
      • 2015-07-23
      • 2011-12-09
      相关资源
      最近更新 更多