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.
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.