using system.net.mail attachment is empty

G

Guest

The following sends my email, but the attachment is empty. The attachment
should contain the data that is in the string that was created from the
xmlReader.

I have a stored procedure written using For XML explicit, and it returns an
xml reader. Then the following:

xmlRdr.MoveToContent();
string myTemp = xmlRdr.ReadOuterXml();

System.IO.MemoryStream memStream = new System.IO.MemoryStream();
StreamWriter sw = new StreamWriter(memStream);
sw.Write(myTemp);

System.Net.Mail.MailMessage mail = new System.Mail.MailMessage();
mail.From = new System.Net.Mail.MailAddress("i put my email address here");
mail.To.Add("I put my email address here, because it is just a test");
mail.Subject = "This is a test";
mail.Body = "this content is in the body";

System.Net.Mail.Attachment emailAttachment = new
System.Net.Mail.Attachment(memStream, "text/xml");
System.Net.Mime.ContentDisposition contentDispo =
emailAttachment.ContentDisposition;

contentDispo.FileName = "myFirstText.xml";
mail.Attachments.Add(emailAttachment);

System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("smtp.put
myemailservernamehere");
smtp.Send(mail);
memStream.Close();

The above sends the email with an attachment, but the attachment is empty.

Note the xmlReader produces xml with a unique root, so no problem with the
string. I can see the data in the string when I debug, but the attachment
does not have it.
 
P

pradeep via DotNetMonster.com

hi,

Try using the following piece of code for filling the memory stream

MemoryStream memStream = new MemoryStream();
UnicodeEncoding encoding = new UnicodeEncoding();
Byte[] byteArray = encoding.GetBytes(myTemp);
memStream.Write(byteArray, 0, byteArray.GetLength(0));
memStream.Seek(0, SeekOrigin.Begin);


regards,
pradeep
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,154
Members
46,702
Latest member
LukasConde

Latest Threads

Top