2

我正在尝试从辅助角色(Azure)发送一封带有附件(来自 blob 存储)的电子邮件(在 c# 中)。我可以发送电子邮件,但附件(word 文档)为空白。从工作角色调用以下函数。

    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("User@hotmail.com", "User@gmail.com");
            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("User@hotmail.com", Password);
            client.Send(Email);
            fs.Flush();
            fs.Close();
            Email.Dispose();                       
    }

请告诉我我做错了什么?

4

2 回答 2

4

fs.Position = 0;在附加创建Attachement对象之前,我会尝试做。

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

于 2011-01-09T20:16:20.760 回答
0

只是一个猜测,但您可能应该在发送电子邮件之前调用 fs.Close() 。

于 2011-01-09T09:45:17.420 回答