c#电子邮件对象流到附件 [英] c# email object to stream to attachment

查看:295
本文介绍了c#电子邮件对象流到附件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从列表中的对象创建和发送电子邮件附件。我发现一个很好的文件回答这里,但仍然有些混乱。

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

然后发送附件?

感谢您的任何帮助或提示。

Thank you for any help or hints.

如果我无法理解,我想成为一个excel文档或csv。我相信有这样的事情已经有类了,新来的这类信息在哪里?

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?

推荐答案

我直接在浏览器中写了它,但它应该是可以的:

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

这篇关于c#电子邮件对象流到附件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆