Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Fri Jul 01, 2011 9:53 am

I am creating word document as memory stream and I am to send it as email attachment. But when I create attachment from memory stream, document in email is empty. Here is my commented code, pleace for help:

' create mail message
Dim mail As New MailMessage()
mail.From = New MailAddress(sMailFrom)
mail.To.Add(sMailTo)
mail.Subject = "Test email"
mail.Body = "See for attachments"

' create (open) document from template
Dim doc As New Document()
doc.LoadFromFile(sFileName, FileFormat.Docx)
.....
' Test save to file - DOCX is correct
doc.SaveToFile("c:\temp\test.docx", FileFormat.Docx)

' Save file to the stream
Dim ms as New System.IO.MemoryStream
doc.SaveToStream(ms, FileFormat.Docx)

' create attachment from stream
mail.Attachments.Add(New Attachment(ms, "Licence.docx", "text/msword"))

'send the message
Dim smtp As New SmtpClient
smtp.Send(mail)

Attachment in mail message is empty, only 105 B

mzahumensky
 
Posts: 18
Joined: Fri May 20, 2011 11:29 am

Mon Jul 04, 2011 3:01 am

Dear mzahumensky,
Thanks for your inquiry.
I checked the MIME Types.
mail.Attachments.Add(New Attachment(ms, "Licence.docx", "text/msword")) should be replace to
mail.Attachments.Add(New Attachment(ms, "Licence.docx", "application/msword"))
Justin
Technical Support / Developer,
e-iceblue Support Team
User avatar

Justin Weng
 
Posts: 110
Joined: Mon Mar 28, 2011 5:54 am

Mon Jul 04, 2011 7:02 am

Dear Justin
I changed MIME type to mail.Attachments.Add(New Attachment(ms, "Licence.docx", "application/msword")), but still same result - attachment in email is empty

Martin

mzahumensky
 
Posts: 18
Joined: Fri May 20, 2011 11:29 am

Tue Jul 05, 2011 3:01 am

Dear Martin,
Thanks for your inquiry.
Before add the stream to Attachments, we should set the stream position to 0.
Like this.
Code: Select all
                ms.Position = 0;
                mail.Attachments.Add(new Attachment(ms, "test.docx"));
Justin
Technical Support / Developer,
e-iceblue Support Team
User avatar

Justin Weng
 
Posts: 110
Joined: Mon Mar 28, 2011 5:54 am

Tue Jul 05, 2011 10:25 am

Dear Justin, thank you for solution, now it is working perfectly
Martin

mzahumensky
 
Posts: 18
Joined: Fri May 20, 2011 11:29 am

Return to Spire.Doc