2011-01-09 63 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;

可能发生的事情是它试图从流中的当前位置读取,并且该流处于末尾,因此它什么也不读。