Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 14th, 2007, 01:04 AM
Registered User
 
Join Date: Feb 2007
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Email Formatting

I am trying to make a program that emails the text of a RichTextBox. The problem is that when I send the email, the text is unformatted i.e. no line breaks, no font specifications, etc.

I tried using the Rtf property of the RichTextBox but the email sent contains the actual rtf code and not the formatted text.

...
 aMessage.Body = richTextBox1.Text;
 smtp.Send(aMessage);

Thanks in advance.

Eitan

 
Old February 14th, 2007, 05:11 PM
Friend of Wrox
 
Join Date: Sep 2005
Posts: 166
Thanks: 2
Thanked 33 Times in 33 Posts
Default

Hi Eitan,

When you are building your message, the best way to do this is to use AlternateView. These can be added to a MailMessage to represent different formats of the message, e.g. when you send a HTML email, you often also get a plain text version in case your email client can't read HTML. The RichTextBox should make this fairly simple to achieve.
The simplest way to create an AlternateView is to use the CreateAlternateViewFromString method:

Code:
using System.Text;
using System.Net.Mail;
using System.Net.Mime;
...
MailMessage message = new MailMessage();
...
AlternateView PlainView = CreateAlternateViewFromString(
    richTextBox1.Text, Encoding.ASCII, MediaTypeNames.Text.Plain);

AlternateView RTFView = CreateAlternateViewFromString(
    richTextBox1.Rtf, Encoding.ASCII, MediaTypeNames.Text.RichText);

message.AlternateViews.Add(PlainView);
message.AlternateViews.Add(RTFView);
Hope this helps
Phil





Similar Threads
Thread Thread Starter Forum Replies Last Post
Formatting Body of Email murfinp General .NET 0 August 18th, 2006 05:44 AM
Formatting syrex XSLT 2 November 5th, 2005 08:23 AM
Formatting Ben Access 3 February 28th, 2004 05:52 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.