1

I am trying to create a .txt file dynamically and also after creating it with some content I need to send the same in attachment of email. Email sending with attachment working for me but the attached file is empty.

I am trying below code to do the same -

 using (var ms = new System.IO.MemoryStream())
                {
                    using (var writer = new System.IO.StreamWriter(ms))
                    {
                        StringBuilder sb = new StringBuilder();
                        sb.Append("Filte start here");
                        sb.AppendFormat("<test>{0}</test>", test);
                        sb.Append("File end here");
                        writer.Write(sb.ToString());
                        writer.Flush();

                        System.Net.Mime.ContentType ct = new System.Net.Mime.ContentType(System.Net.Mime.MediaTypeNames.Text.Plain);
                        System.Net.Mail.Attachment attach = new System.Net.Mail.Attachment(ms, ct);
                        attach.ContentDisposition.FileName = "temp.txt";
                        mail.Attachments.Add(attach);
                        try
                        {
                            SendEmailUtility.SendMail(mail);
                        }
                        catch (SmtpException e)
                        {
                            Console.WriteLine(e.ToString());
                        }
            }
}

It is not the duplicate of Attach a file from MemoryStream to a MailMessage in C# post because I am also doing the same thing what is suggested there but in my case sending empty attachment. So definitively there is something which I am missing in my code base.

Please let me know what I am doing wrong here.

Any help would be appreciated!

Yogesh Sharma
  • 2,017
  • 1
  • 15
  • 33
  • 1
    The position in the memorystream is probably at the end. Try rewinding it to the beginning (`ms.Position = 0;`) – Thomas Weller Sep 06 '17 at 13:40
  • thanks @ThomasWeller for quick reply let me try to put position here – Yogesh Sharma Sep 06 '17 at 13:42
  • 1
    The comments below the solution say how you should "rewind" the stream for it to work, but the answer itself does NOT say that. – Kevin Anderson Sep 06 '17 at 13:44
  • There are other answers that rewind the stream, e.g. the one with the highest number of votes (unfortunately not the accepted answer) [and this highest voted comment](https://stackoverflow.com/questions/5336239/attach-a-file-from-memorystream-to-a-mailmessage-in-c-sharp#comment30575705_14839872) – Thomas Weller Sep 06 '17 at 13:51
  • Thanks @ThomasWeller I didn;t try with position but I tried with your first comment which you deleted var attachment = Attachment.CreateAttachmentFromString(EmailAttachmentBody.ToString(), ct);' And it works fine for me to without defining anything... I think this is the easiest solution you gave... – Yogesh Sharma Sep 06 '17 at 14:02
  • @ThomasWeller it would be great if you can put that as an answer so that in case anyone else also come across with same thing he can take that solution... will mark it correct – Yogesh Sharma Sep 06 '17 at 14:03
  • do you NEED to actually *create* the file? – Ess Kay Oct 12 '17 at 12:45
  • after *writer.Flush();* add: ***stream.Position = 0;*** – Ess Kay Oct 12 '17 at 12:49

0 Answers0