473,396 Members | 2,099 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

using system.net.mail attachment is empty

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.
Apr 18 '06 #1
1 5854
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

theWizard1 wrote:
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.


--
Message posted via DotNetMonster.com
http://www.dotnetmonster.com/Uwe/For...p-net/200604/1
Apr 18 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: PeterR | last post by:
I've spent weeks trying to write a function that is simple to use for sending emails with attachments, but I my programming skills are not that advanced. I've noticed a few postings in these...
2
by: New User | last post by:
Hi, I have a PDF file that I am trying to send as an attachment through a C# program. Even though the PDF file can be opened by itself, sometimes the same file cannot be opened as an...
0
by: Balakrsihna | last post by:
Hi All, Can anyone tell me how to replace an image in MS Word 2003 using c#.net. I am converting an html file to word doc and sending this as attachment using System.Web.Mail; the...
8
by: Ajit | last post by:
Is there anyway to move attachments from a incoming email into a specified folder using System.Web.Mail ? has anyone done this ? Any help is appreciated.
2
by: Brad | last post by:
I have a web .Net app which sends emails with attachments. After the email is sent I clean up aftermyself and delete the attachments from disk. In upgrading to .Net 2 I changed the email logic...
4
by: Max | last post by:
hi I am using System.Web.Mail.MailMessage with System.Web.Mail.SmtpMail to send mail. now the my question is, Is it compulsory to add fields smtpauthenticate, sendusername and sendpassword...
2
by: Ruud | last post by:
Just before leaving for a holiday my collegue modified this script. Now it won't send any body text (The data filled in on the form) and in an error condition it won't send any attachments either....
11
by: Ed Bitzer | last post by:
I have been able using the namespace System.Web.Mail and its method Smtp.mail.send to mail simple text messages to a small group within our 55 and older community. I need help expanding the...
1
by: rengaraj | last post by:
hi dears! in my webapp i have some users, thats are in 3 Roles. i want to do communication between them via mail system. this means that Role 1 attach one file and this file must send via mail...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.