c# email object to stream to attachment

别等时光非礼了梦想. 提交于 2019-12-11 03:31:39

问题


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?


回答1:


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);   
}


来源:https://stackoverflow.com/questions/4604726/c-sharp-email-object-to-stream-to-attachment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!