MIME attachment and memory stream

B

bob

Hi,
I want to send a memorystream as an attachment. Avoiding using a
filestream as this code will be part of a Webservice and I figured it
was easier to keep the data in memory rather than writing it to disc
on the webserver.
But if this is not the way to do it then please let me know.

My code 'works' but the attachment is empty when read by the mail
client. The attachment also behaves strangely in that it won't open
from inside the mail client, you have to 'save as' then open the saved
file (which is empty, 0kB)
Would appreciate pointer in the right direction.
code follows.
Thanks
Bob

private bool GenerateEmail(List<Job> list)
{
SmtpClient c = new SmtpClient("mailserver");
ContentType ct = new ContentType("text/plain;
charset=us-ascii");
Stream stream = new MemoryStream();
Attachment a = new
Attachment(GeneratePendingStream(stream,list),ct);

MailMessage m = new
MailMessage("somebodyt@somewhere","mailaddress@mailserver") {Body =
"Test Message"};
m.Attachments.Add(a);
c.Send(m);
stream.Close();
return true;

}
/// <summary>
/// Generate stream of pending jobs
/// </summary>
/// <param name="list"></param>
/// <returns></returns>
private Stream GeneratePendingStream( Stream stream,List<Job>
list)
{


StreamWriter wr = new StreamWriter(stream);
foreach (Job job in list)
{
string sLine = job.CustomerId.ToString() + "," +
job.CustomerName;
wr.WriteLine(sLine);
}
wr.Flush();

return stream;
}
 
B

bob

Hi Pete,
Right on the money.
Also the file open behaviourin the mail client resolved once the
attachment had some length.
Thanks
Bob
 

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

Top