Skip to content

Sending mail with attachment (Bitmap) from MemoryStream don't work

Ondrej

Asked May 25, 2009, 8:12 AM EDT3 answers
Share
LinkedInX
I don't kown why..., but when I adding attachment (Bitmap) from MemoryStream, it dosn't work, because I receive mail with picture, but it has 0 KB. (sorry, my bad english)

Here is my snippet code:
...
Bitmap bmp;
...
MemoryStream = new MemoryStream();
bmp.Save(memStream, ImageFormat.Jpeg);
...
ContentType contentType = new ContentType();
contentType.MediaType = MediaTypeNames.Image.Jpeg;
contentType.Name = "screen";

MailMessage mail = new MailMessage();
...
mail.Attachments.Add(new Attachment(memStream, contentType));
...
SmtpClient.Send(mail);

3 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    Hi, Where did you define the "memStream" you created new object of MemoryStream but without to declare "memStream" there.

    Bitmap bmp;
    ...
    MemoryStream = new MemoryStream(); //Old one
    MemoryStream memStream = new MemoryStream(); //new one

    bmp.Save(memStream, ImageFormat.Jpeg);
    ...
    ContentType contentType = new ContentType();
    contentType.MediaType = MediaTypeNames.Image.Jpeg;
    contentType.Name = "screen";

    MailMessage mail = new MailMessage();
    ...
    mail.Attachments.Add(new Attachment(memStream, contentType));
    ...
    SmtpClient.Send(mail);
    0
  • 0
    ohh.. mistake..

    I have it like this:
    MemoryStream memStream = new MemoryStream();
    0
  • 0
    MemoryStream memStream = new MemoryStream(); //new one

    bmp.Save(memStream, ImageFormat.Jpeg);
    ...
    ContentType contentType = new ContentType();
    contentType.MediaType = MediaTypeNames.Image.Jpeg;
    contentType.Name = "screen";

    MailMessage mail = new MailMessage();
    ...
    mail.Attachments.Add(new Attachment(memStream, contentType));

     
    -----------------------------------------------------------------------------
    memStream.Position should be set to 0 after bmp.Save(memStream, ImageFormat.Jpeg);
    and then , It works.
    I have met the same issue before.
     
    0