locked
Generated Word document as empty document with email attachment? RRS feed

  • Question

  • User238037111 posted

    Hello

    I am using following code for generating word document and attaching with an outgoing email.  I am getting email properly but with an empty document. However If I generate document and saved locally it generate document properly. Please see following code and let me know If what wrong I am doing, I am not getting any error.

            StringBuilder str = new StringBuilder();
            StringBuilder str1 = new StringBuilder();
    
            str.Append("<table border='0' cellpadding='0' cellspacing='0' class='textbold' width='100%' >");
            str.Append("<tr >");
            str.Append("<td style='width:140px'>");
            str.Append("Sl No");
            str.Append("</td>");
            str.Append("<td style='width:20px'>");
            str.Append("Name");
            str.Append("</td>");
            str.Append("<td style='width:200px'>");
            str.Append("Age");
            str.Append("</td>");
            str.Append("<td style='width:500px'>");
            str.Append("</td>");
            str.Append("</tr >");
            str.Append("</table>".ToString());
    
         str1.Append(@"<html xmlns:o='urn:schemas-microsoft-com:office:office' xmlns:w='urn:schemas-microsoft-com:office:word' xmlns='http://www.w3.org/TR/REC-html40'><head><title>Time</title>");
            str1.Append(@"<body lang=EN-US style='mso-element:header' id=h1><span style='mso--code:DATE'></span><div>");
            str1.Append("<DIV  style='font-size:12px;'>");
            str1.Append(str.ToString());
            str1.Append("</div></body></html>");
            string strFile = "Text.doc";
            string strcontentType = "application/word";
            Response.ClearContent();
            Response.ClearHeaders();
            Response.BufferOutput = true;
            Response.ContentType = strcontentType;
            Response.AddHeader("Content-Disposition", "attachment; filename=" + strFile);
    
            string strBody = str1.ToString();
            Response.Write(str1.ToString());
            byte[] data = Encoding.ASCII.GetBytes(strBody);
            
            //save the data to a memory stream
            System.IO.MemoryStream ms = new System.IO.MemoryStream(data);
            
            string smtpServer = SPAdministrationWebApplication.Local.OutboundMailServiceInstance.Server.Address;
            SmtpClient client = new SmtpClient(smtpServer);
            client.Credentials = CredentialCache.DefaultNetworkCredentials;    
         
            MailMessage message = new MailMessage();
        
            message.From = new MailAddress(SPAdministrationWebApplication.Local.OutboundMailSenderAddress.ToString());
            message.To.Add(new MailAddress("akumar@test.com"));
            message.IsBodyHtml = true;
            message.Body = "Hi there, check out the attachment";
            message.Subject = "Sent Attachment";
    
            message.Attachments.Add(new System.Net.Mail.Attachment(ms, strFile, "application/vnd.ms-word"));
            client.Send(message);
            Response.Flush();
            Response.Close();
            Response.End();
    


     

    Sunday, July 11, 2010 11:14 PM

All replies

  • User-1567482218 posted

    Check this code sample that dynamically generates a Word document & emails it as an attachment without explicitly saving the file -

    http://www26.brinkster.com/mvark/dyna/EmailwithDynamicAttachment.html

    Monday, July 12, 2010 5:07 AM
  • User238037111 posted

    Mvark

    I followed same article and modified code. My only concern here why the attached document is attaching as blank document, however If I directly save word document it is showing contents in word documents.

    Please advise 

    Tuesday, July 13, 2010 10:37 AM
  • User-1567482218 posted

    The code sample in the link I mentioned earlier works fine. Having tested it, I did receive a proper document.

    I also tried out your code after removing lines 26-31 and lines 54-56. The sample worked fine and I received a proper Word file as attachment.

    Tuesday, July 13, 2010 3:01 PM
  • User238037111 posted

    Mvark

    Thanks for suggestion. I also tested this, when I send to my office email address, contents doesnt display, somehow contents are emoving but when I sent it to my gmail address, I am able to see content in word documents.

    Please let me know how do I handle this situation, is that something I need to add in following code

     str1.Append(@"<html xmlns:o='urn:schemas-microsoft-com:office:office' xmlns:w='urn:schemas-microsoft-com:office:word' xmlns='http://www.w3.org/TR/REC-html40'><head><title>Time</title>");
            str1.Append(@"<body lang=EN-US style='mso-element:header' id=h1><span style='mso--code:DATE'></span><div>");
            str1.Append("<DIV  style='font-size:12px;'>");
            str1.Append(strHTMLContent.ToString());
            str1.Append("</div></body></html>");


     

    Tuesday, July 13, 2010 9:35 PM