rahul kadachira
rahul kadachira

Reputation: 43

Sending mail in wpf using richtextbox

The richtextbox gets it's value copied from a word file that has an image and text. The code below only sends the text and not the image. How can I get it to send both?

   using (MailMessage mm = new MailMessage("[email protected]","[email protected]"))
        {
            mm.Subject = "demo";


            TextRange txc = new TextRange(txt_1.Document.ContentStart, txt_1.Document.ContentEnd);

            MemoryStream ms = new MemoryStream();
            txc.Save(ms, DataFormats.Xaml);
           string xamlText = ASCIIEncoding.Default.GetString(ms.ToArray());
         //   string sa = ASCIIEncoding.Default.GetBytes(ms.ToArray()).ToString();

           mm.Body = xamlText;


            mm.IsBodyHtml = true;
            SmtpClient smtp = new SmtpClient();
            smtp.Host = "smtp.gmail.com";
            smtp.EnableSsl = true;
            NetworkCredential NetworkCred = new NetworkCredential("[email protected]", "xxxxxx");
            smtp.UseDefaultCredentials = true;
            smtp.Credentials = NetworkCred;
            smtp.Port = 587;
            smtp.Send(mm);
           // ClientScript.RegisterStartupScript(GetType(), "alert", "alert('Email sent.');", true);
        }

Upvotes: 1

Views: 751

Answers (1)

karfus
karfus

Reputation: 939

The problem is that you're taking binary data and explicitly converting it to ASCII text:

string xamlText = ASCIIEncoding.Default.GetString(ms.ToArray());

In so doing, you're altering the original values of the bytes in the image.

You're either going to want to change the BodyEncoding/TransferEncoding of the message Body and encode the data likewise (e.g. Base64 or MIME encoding), or extract the images from the file stream and add them to the mail as attachments. There's a good primer on email encoding here: http://email.about.com/cs/standards/a/mime.htm

Upvotes: 1

Related Questions