Cannot access a closed Stream

JCWEBHOST
JCWEBHOST used Ask the Experts™
on
Hey guys, i am geting this error: Cannot access a closed Stream, while trying to send an attach email.

here my code:

 
public MemoryStream Ms
    {
        get { return ms; }
        set { ms = value; }
    }


 public string SendMail()
    {
        string erorr = "";
        try
        {
            System.Net.Mail.MailMessage MailMass = new System.Net.Mail.MailMessage();

            MailMass.From = new System.Net.Mail.MailAddress("ben@intercity.jcwebhostza.com");
            MailMass.To.Add(new System.Net.Mail.MailAddress(To));
            MailMass.Subject = Subject;
            MailMass.Body = Body;
            MailMass.IsBodyHtml = true;

            MailMass.ReplyTo = new System.Net.Mail.MailAddress("ben@intercity.jcwebhostza.com");
            MailMass.Priority = System.Net.Mail.MailPriority.Normal;
            System.Net.Mail.SmtpClient smtpC = new System.Net.Mail.SmtpClient();
            System.Net.NetworkCredential basicAuthenticationInfo = new System.Net.NetworkCredential("ben@intercity.jcwebhostza.com", "ben01int");

            if (Ms != null && Pdf_name != "")
            {
                MemoryStream open_ms = Ms;
                open_ms.Position = 0;
                using (Attachment att = new Attachment(open_ms, Pdf_name, MediaTypeNames.Application.Pdf))
                {
                    {
                        MailMass.Attachments.Add(att);
                    }
                }
            }

            smtpC.Host = "mail.intercity.jcwebhostza.com";
            smtpC.Port = 25;
            smtpC.UseDefaultCredentials = false;
            smtpC.Credentials = basicAuthenticationInfo;

            smtpC.Send(MailMass);
        }
        catch (Exception E)
        {
            erorr = E.Message.ToString();
        }

        return erorr;

Open in new window


please help
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Hi! Where do you create and close ms?
The error is comming because MemoryStream object (ms) is closed, when you try to read or write it.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial