0

I'm trying to send an emil with Attachment using Postal framework for MVC.

I can send e-mails with Postal without problem, however, when I try send an e-mail with attachment, the files goes empty.

Here is my code:

dynamic emailNovoOrcamento = new Email("OrcamentosServicos/NotificaNovoOrcamento");
emailNovoOrcamento.To = usuario.UsuEmail;
emailNovoOrcamento.From = "no-reply@servicili.com";
emailNovoOrcamento.Subject = "Test...";

ContentType conteudo = new ContentType();
conteudo.MediaType = MediaTypeNames.Image.Jpeg;
conteudo.Name = "userPhoto.jpg";

Attachment imagemDestinatario = new Attachment(new getUserPhotoStream(usuario), conteudo);
emailNovoOrcamento.Attachments.Add(imagemDestinatario);
emailNovoOrcamento.Send();

getUserPhotoStream is a method that converts a byte[] to a Stream:

public MemoryStream getUserPhotoStream(byte[] photo)
{
    MemoryStream imagemStream = new MemoryStream();
    Image imagem;
    MemoryStream imagemUsuarioStream = new MemoryStream(photo);

        imagem = Image.FromStream(imagemUsuarioStream);
        imagem.Save(imagemStream, ImageFormat.Jpeg);
        return imagemStream;
    }
}

When I send it via Email, the file size is 0kb.

What am I doing wrong?

Thomas Ayoub
  • 27,208
  • 15
  • 85
  • 130
Dan
  • 1,082
  • 3
  • 14
  • 40

2 Answers2

2

Set the position of the stream to 0 before using it:

// ...
imagem.Save(imagemStream, ImageFormat.Jpeg);
imagemStream.Position = 0;
return imagemStream;
Kvam
  • 2,016
  • 1
  • 21
  • 29
0

Sending email with attachments from C#, attachments arrive as Part 1.2 in Thunderbird

See this, It may give some insight. I did something like you but from here.

Community
  • 1
  • 1