3

I'm trying to create and send an email attachment from an object which is a list of lists. I found a nicely documented answer here, but still have some confusion.

It mentions "get some binary data"

//Get some binary data
byte[] data = GetData();

I have tested my data by:

Console.WriteLine(ieLog.FirstName + "." + ieLog.LastName);

I guess my question is how do I turn that into a stream if it isn't already one and then use:

//save the data to a memory stream
MemoryStream ms = new MemoryStream(data);

and then send the attachment?

Thank you for any help or hints.

I would like to be an excel doc or csv if I can't figure that out. I'm sure there are already classes for this sort of thing, where does a newb look for that sort of information?

Community
  • 1
  • 1
rd42
  • 3,584
  • 15
  • 56
  • 68
  • what is ieLog? A class of your own?... how do you expect that to be attached to the email, as a binary file? – Pauli Østerø Jan 05 '11 at 13:50
  • What is it, if it is not a stream (or `byte[]`)? – Oded Jan 05 '11 at 13:50
  • ieLog is a basic list of login logout times. I would like to attach it as an Excel doc or csv. I'm not stuck on the stream method, it is just all I have found so far. – rd42 Jan 05 '11 at 14:00

1 Answers1

4

I wrote it directly in the browser, but it should be ok:

...

byte[] data = ASCIIEncoding.Default.GetBytes(ieLog.FirstName + "." + ieLog.LastName);

using(MemoryStream ms = new MemoryStream(data))
{
    mail.Attachments.Add(new Attachment(ms, "myFile.csv", "text/csv" ));

    SmtpClient smtp = new SmtpClient("127.0.0.1");
    smtp.Send(mail);   
}
zavaz
  • 745
  • 4
  • 13
  • You should be disposing of mail (which I'm guessing is a Message) and also in .Net 4 onwards dispose of smtp. – Paul Zahra Dec 15 '15 at 09:22