How do I send RTF formated mail using VB.NET?

J

John Brock

I am currently using a VB.NET program to send out e-mails with
plain text bodies (plus one attachment). The emails are being
received as Rich Text messages (probably just my personal Outlook
default, because I didn't do this in the program), but there is no
actual formatting (italics, color, etc.) in the message body, which
is passed to from VB.NET to Outlook as an unformatted text String.

I want to start applying formatting to the message body. In
particular, my app has a RichTextBox, which has an Rtf property,
which returns a String formatted as RTF, which contains whatever
my users type or paste into the RichTextBox. That is what I want
to send as the body of the message.

Unfortunately all I get is garbage, like this:

{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\froman\fprq2\fcharset0
Times New Roman;}{\f1\fnil\fcharset0 Microsoft Sans Serif;}}
{\colortbl ;\red255\green0\blue0;}
\viewkind4\uc1\pard\f0\fs24 This is \cf1 rich\cf0 \b\i text.\par
\b0\i0\par
\pard\f1\fs17\par
}

This is of course precisely the content of the input String,
formatting and all. Clearly I have failed to clue Outlook in that
the message body should be interpreted as RTF, not plain text. So
how should I do this?

Here is my code:

=== Begin Example ===
Imports Outlook = Microsoft.Office.Interop.Outlook

....

Sub MailerRoutine(ByVal Subject As String, ByVal Recipient As String, _
ByVal Body As String, ByVal Filename As String, ByVal displayName As String)

Dim oApp As Outlook.Application = CreateObject("Outlook.Application")

Dim oItem As Outlook.MailItem = oApp.CreateItem(Outlook.OlItemType.olMailItem)

oItem.Subject = Subject
oItem.To = Recipient
oItem.Body = Body
oItem.BodyFormat = Outlook.OlBodyFormat.olFormatRichText

Dim bodyLength As String = oItem.Body.Length

Dim oAttachments As Outlook.Attachments = oItem.Attachments
Dim oAttachment As Outlook.Attachment
oAttachment = oAttachments.Add(Filename, , bodyLength + 1, displayName)

oItem.Send()

oItem = Nothing
oApp = Nothing
End Sub
=== End Example ===

I got this code from someone else, and I have to admit I don't
really understand how the attachment part work, but that is another
issue. I had thought that adding the line with "olFormatRichText"
would be sufficient to tell Outlook that the message body was RTF,
but clearly it isn't. I'm sure this is really simple, but I just
haven't been able to figure it out, so any help would be appreciated.
 
S

Sue Mosher [MVP-Outlook]

The Outlook object model itself provides technique for creating an RTF message body. You can set BodyFormat, but that just changes the message format. It doesn't give you a way to actually get the RTF content into the message. To do that, the easiest approach is to use the third-party Redemption library. See http://www.outlookcode.com/d/formatmsg.htm

FYI, the general Outlook programming forum is "down the hall" at microsoft.public.outlook.program_vba
--
Sue Mosher, Outlook MVP
Author of Configuring Microsoft Outlook 2003

and Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
M

Mr Newbie

And who said newsgroups should not be used to promote one's business ?

:)


The Outlook object model itself provides technique for creating an RTF
message body. You can set BodyFormat, but that just changes the message
format. It doesn't give you a way to actually get the RTF content into the
message. To do that, the easiest approach is to use the third-party
Redemption library. See http://www.outlookcode.com/d/formatmsg.htm

FYI, the general Outlook programming forum is "down the hall" at
microsoft.public.outlook.program_vba
--
Sue Mosher, Outlook MVP
Author of Configuring Microsoft Outlook 2003

and Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
J

John Brock

The Outlook object model itself provides technique for creating an RTF message body. You can
set BodyFormat, but that just changes the message format. It doesn't give you a way to
actually get the RTF content into the message. To do that, the easiest approach is to use
the third-party Redemption library. See http://www.outlookcode.com/d/formatmsg.htm

I need this tomorrow, and I am afraid that third-party solutions
are out of the question in any case. Since I already have an RTF
formatted text string, are you saying there is no straightforward
way to insert it into an Outlook message? I would have assumed
that Microsoft would make this easy, since it seems like such a
natural thing to want to do, but then you never know with MS.
FYI, the general Outlook programming forum is "down the hall" at
microsoft.public.outlook.program_vba

Thanks, I'll give it a shot.
 
S

Sue Mosher [MVP-Outlook]

Yes, that's exactly what I'm saying. The page I suggested describes your choices:

1) Redemption (easy, but third party)

2) CDO 1.21 and another helper .dll (CDO not being officially. supported in .NET languages, although I've heard of people using it)

3) If Word is the email editor, save the .rtf as a file, then open that file and copy paste text using Word objects. (Outlook exposes the Document for an email message through Inspector.WordEditor.)

--
Sue Mosher, Outlook MVP
Author of Configuring Microsoft Outlook 2003

and Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
R

RgSystems

hi,


did you find any solution to your problem ?

I have the same problem with Word object automation.

Thanks in avance.

Roberto Garcia Martín
 
S

Sue Mosher [MVP-Outlook]

Roberto, I'd suggest that you describe your problem from scratch. It may not be the same as John's. Bottom line is that Outlook itself has no direct way to create RTF formatted messages. See http://www.outlookcode.com/d/formatmsg.htm
--
Sue Mosher, Outlook MVP
Author of Configuring Microsoft Outlook 2003

and Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers



RgSystems said:
hi,


did you find any solution to your problem ?

I have the same problem with Word object automation.

Thanks in avance.

Roberto Garcia Martín
 
R

RgSystems

hi Sue,

all right, thak you very much for your answer.
I've put one message in at the top of this newsgroup.

Ref. "oRange.text = ?? "

thanks a lot.
Roberto Garcia Martin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top