Enviar correo electrónico desde el rol de trabajador (Azure) con datos adjuntos en c #

I am trying to send an email(in c#) from worker role(Azure) with an attachment(from blob storage). I am able to send an email but attachment(word document) is blank. The following function is called from worker role.

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

Please tell me where I am doing wrong?

preguntado el 09 de enero de 11 a las 05:01

2 Respuestas

I would try do fs.Position = 0; before attaching creating your Attachement objeto.

What's probably happening is that it's trying to read from current position in the stream and that stream is at the end, so it reads nothing.

Respondido el 09 de enero de 11 a las 23:01

Just a guess, but you should probably be calling fs.Close() antes sending the email.

Respondido el 09 de enero de 11 a las 12:01

Got the solution byte[] file= blob.DownloadByteArray(); Attachment attach = new Attachment(new MemoryStream(file), "Report.doc"); Instead of using FileStream used Byte array and MemoryStream and its working fine. Thanks for your replies. - simplyvaibh

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.