How to send an email with attachment from worker role using c#.net?

已答覆 How to send an email with attachment from worker role using c#.net?

  • Sunday, January 09, 2011 5:42 AM
     
     

    I am trying to send an email with an attachment (attachment should get downloaded from blob storage) from worker role using c#.net.

    I am able to send an email without attachment, but when I am trying with attachment the I am receiving blank attachment.

    What I am trying here is that I am uploading .doc file in blob storage and in worker role trying to send that .doc file as a attachment in email.

    Please find the below function which I am calling 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@gmail.com", "Password");

                    client.Send(Email);

                    fs.Flush();

                    fs.Close();

                    Email.Dispose();                        

            }

     

    Please tell the solution for above scenario. How should I send an email with an attachment from worker role? 

     

    Thanks in advance

     

All Replies

  • Sunday, January 09, 2011 9:45 AM
     
     
    See my answer on your StackOverflow question.
  • Sunday, January 09, 2011 6:34 PM
     
     
    Could you post the link to that question on SO.
  • Monday, January 10, 2011 5:07 AM
     
     Answered

    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.

    • Marked As Answer by Vaibhav Fegade Tuesday, January 11, 2011 6:38 AM
    •