2011-01-09 74 views
2

我想從工作人員角色(Azure)與附件(從BLOB存儲)發送電子郵件(在C#中)。 我可以發送電子郵件,但附件(word文檔)爲空。 從worker角色調用以下函數。發送電子郵件從工作人員角色(Azure)與附件在C#

public void sendMail(string blobName) 
    { 
      InitStorage();//Initialize the storage 
      var storageAccount = CloudStorageAccount.FromConfigurationSetting("DataConnectionString"); 
      container = blobStorage.GetContainerReference("Container Name"); 
      CloudBlockBlob blob = container.GetBlockBlobReference(blobName); 

      if (File.Exists("demo.doc")) 
       File.Delete("demo.doc"); 

      FileStream fs = new FileStream("demo.doc", FileMode.OpenOrCreate); 
      blob.DownloadToStream(fs);     
      Attachment attach = new Attachment(fs,"Report.doc"); 
      System.Net.Mail.MailMessage Email = new System.Net.Mail.MailMessage("[email protected]", "[email protected]"); 
      Email.Subject = "Text fax send via email"; 
      Email.Subject = "Subject Of email"; 
      Email.Attachments.Add(attach); 
      Email.Body = "Body of email"; 
      System.Net.Mail.SmtpClient client = new SmtpClient("smtp.live.com", 25); 
      client.DeliveryMethod = SmtpDeliveryMethod.Network; 
      client.EnableSsl = true; 
      client.Credentials = new NetworkCredential("[email protected]", Password); 
      client.Send(Email); 
      fs.Flush(); 
      fs.Close(); 
      Email.Dispose();      
    } 

請告訴我我在做什麼錯?

回答

0

只是一個猜測,但你應該在發送電子郵件之前調用fs.Close()之前

+1

得到解決方案 byte [] file = blob.DownloadByteArray(); Attachment attach = new Attachment(new MemoryStream(file),「Report.doc」); 而不是使用FileStream中使用的字節數組和MemoryStream和它的工作正常。 感謝您的回覆。 – simplyvaibh 2011-01-10 05:09:50

4

在附加創建Attachement對象之前,我會嘗試做fs.Position = 0;

可能發生的事情是它試圖從流中的當前位置讀取,並且該流處於末尾,因此它什麼也不讀。