I have the following code which attaches a jpeg to an email.
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);
However, when I get the email with the attachment in my email, it has 0 bytes. What am I doing wrong here?
Perhaps you’ve left
imageStreampositioned at the end of the data instead of at the start? (I’m assuming it’s aMemoryStream.) Try this:(
MemoryStream.ToArrayignores the current position of the stream, but I suspect thatAttachmentdoesn’t.)