簡體   English   中英

使用 c# 在 Windows Azure 上通過電子郵件發送附件

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

我正在嘗試從 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;
        }
    }

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

試一試。 它應該工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM