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 に答える