0

我有以下代码将 jpeg 附加到电子邮件中。

System.Net.Mime.ContentType contentType = new System.Net.Mime.ContentType();
contentType.MediaType = System.Net.Mime.MediaTypeNames.Image.Jpeg;
contentType.Name = "screen";

Attachment screenCapture = new Attachment(imageStream, contentType);

//this next line works, I checked the image on the hard drive so I know the jpeg is ok
File.WriteAllBytes("c:\\somecoolimage.jpeg", imageStream.ToArray());

mail.Attachments.Add(screenCapture);

smtp.Send(mail);

但是,当我收到电子邮件中带有附件的电子邮件时,它有 0 个字节。我在这里做错了什么?

4

1 回答 1

4

也许您已经imageStream定位在数据的末尾而不是开头?(我假设它是一个MemoryStream。)试试这个:

imageStream.Position = 0;
Attachment screenCapture = new Attachment(imageStream, contentType);

MemoryStream.ToArray忽略流的当前位置,但我怀疑Attachment不会。)

于 2011-03-09T20:05:04.297 回答