MIME attachment and memory stream

  • Thread starter Thread starter bob
  • Start date Start date
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;
}
 
Hi Pete,
Right on the money.
Also the file open behaviourin the mail client resolved once the
attachment had some length.
Thanks
Bob
 
Back
Top