How to integrate company logo with auto send using vb.net

J

jonathan_kha

Hi,

In my VB.NET Windows form application, I'm using the Outlook object to
compose and
automatically send email. The spec also requires that I integrate the
company logo on the top of the email to look like the company
letterhead. I tried creating a html page with the company logo design
on it and set
it as the default stationery. It works when I compose a new email
manually from Outlook. But when the email is composed and sent from
VB.NET code using the Outlook MailItem, for reason beyond my level of
understanding, the default stationery is not used. Instead all I see
is a blank template. I'm fairly new at this game. I greatly
appreciate your help.
 
D

Dave Kane [MVP - Outlook]

Outlook stationery really belongs to the Word editor, and there may be an
easy way to do this through Word automation. I can suggest two different
approaches using Outlook

Option #1: You can set the HTMLBody property of your Outlook.MailItem to
repro the stationery that you created, including whatever formatted text you
want in the email. But if you look at the HTML source for your stationery
you will see that the src property of the IMG tag for the company logo looks
something like src="cid:760521916@20082002-17cb". That cid value corresponds
to the PR_ATTACH_CONTENT_ID (&H3712001E) property of a hidden attachment to
the message that holds the image. So in addition to specifying the HTML
source of the message by setting HTMLBody you would need to add the logo
image as an attachment and set its PR_ATTACH_CONTENT_ID. The Outlook object
model will allow you to add the attachment, but to set any properties on an
attachment you would have to use ExMAPI or a component like Redemption.
That's the most flexible approach since you can easily change the HTML and
the image.

Option #2: Reproduce your stationery template using the Outlook HTML editor,
save it as as an OFT file and then use Outlook's CreateItemFromTemplate
method to create your new MailItem. The HTMLBody property of the new
MailItem will contain the full HTML source for the message, including an IMG
src pointing to the hidden attachment of your logo image. You would need to
read that property, parse it to find where to place your message text and
then reset the value of HTMLBody. The coding is simpler, but you need to
rebuild the OFT file if your logo changes.
 
J

jonathan_kha

Hi Dave,

Firstly, thank you for your help. I googled on this topic for the past
day and a half but came up empty. I'm still green in this area so I
would appreciate it very much if you have some code sample for me to
look at -- for Option 1 and/or 2. At least now I know it can be done.
I was ready to give in and tell me boss otherwise. I really, really
appreciate your help!!! Thanks a million.
 
D

Dave Kane [MVP - Outlook]

Option 2 is less technically challenging, so why not start with that.

Step #1 is creating a template in the Outlook HTML editor:
1. Create a new blank message using your stationery
2. From the main Outlook menu (I'm using Outlook 2003) click Actions > New
Mail Message Using > Microsoft Office Outlook (HTML)
3. Put your cursor in the body of the stationery message, click Ctrl+A to
select all and then Ctrl+C to copy
4. Put your cursor in the body of the Outlook HTML message and click Ctrl+V
to paste. That won't grab the background image if you have spec'd one - to
add that you will need to use Format > Background, etc. from the message
menu
5. Create an Outlook template file (OFT) from the HTML message by clicking
File > Save As... and selecting Outlook template (*.oft) as the type. Save
it to a directory that your WinForms application can access

Step #2 is using the template in your code when you create a new item.
Assuming your Outlook application object is mobjOlApp and the template is at
C:\CompanyLogo.oft
Dim myMail as Outlook.MailItem
myMail =
CType(mobjOlApp.CreateItemFromTemplate("C:\CompanyLogo.oft"),Outlook.MailItem)

myMail.HTMLBody will have the full HTML source for your message, with
<HTML>, <HEAD> and <BODY> tags, etc. You'll need to find the point after the
<IMG> tag for your logo where you want to insert the text of the message
that you want to send, wherever that may be. Modify the value of HTMLBody so
that it includes your text. Then set the values of Subject, add your
recipient(s) and call Send. Does that get you what you need?
 
J

jonathan_kha

Hi Dave,

I followed your instruction but it did not come out right. Here's my
code:

Dim oApp As Outlook._Application
oApp = New Outlook.Application
Dim oMsg As Outlook._MailItem
oMsg = CType(oApp.CreateItemFromTemplate("C:\BOSLH.oft"),
Outlook.MailItem)
oMsg.Subject = "Testing..."
oMsg.HTMLBody = "Testing..." & vbCrLf & vbCrLf & "Line 4"
oMsg.To = "(e-mail address removed)"

The email that came thru did not have the logo as designed. Instead,
it has a blank look to it and the logo is attached with the email. Can
you please give me more instruction on how to carry this out, "You'll
need to find the point after the <IMG> tag for your logo where you want
to insert the text of the message that you want to send". I noticed
that vbCrLf does not work with HTMLBody. A million thanks to you.
 
D

Dave Kane [MVP - Outlook]

Jonathan,

I assumed that you had some familiarity with HTML. Unfortunately you are
going to need to get at least a little bit to make this work.

The HTMLBody property of your message contains ALL the HTML for your
template. If you open a web page in a browser, right-click and select View
Source you will see a bunch of stuff that looks something like this (where
"..." indicates sections that have been left out):

<html xmlns:v="urn:schemas-microsoft-com:vml"
xmlns:eek:="urn:schemas-microsoft-com:eek:ffice:eek:ffice"
xmlns:w="urn:schemas-microsoft-com:eek:ffice:word"
xmlns="http://www.w3.org/TR/REC-html40">
<head>
...
</head>

<body lang=EN-US link=blue vlink=purple style='tab-interval:.5in'>

<p class=MsoNormal align=center style='text-align:center'><font size=3
color=black face=Arial><span style='font-size:12.0pt;font-family:Arial;
color:black'><img width=600 height=60 id="_x0000_i1025"
src="cid:155132615@06042006-0998" align=bottom
u1:shapes="ridImg"></span></font></p>

</body>

</html>

This is the HTML for a simple page with one image, like you are trying to
create. The IMG tag specifies what picture to show, where the image is
positioned in the HTML, etc.:
<img width=600 height=60 id="_x0000_i1025"
src="cid:155132615@06042006-0998" align=bottom u1:shapes="ridImg">
The SRC property (src="cid:155...") says what file to use for the image,
which in this case is a hidden attachment.

The image is inside of a paragraph (defined by the "open paragaraph" <p ...>
and "close paragraph" </p> tags.
If the image is supposed to be at the top and I want to add some text after
that I would add paragraphs after the image paragraph but before the end of
the body, which is denoted by the "close body" tag </body>
Add this:
<p>Testing...</p>
<p></p>
<p></p>
<p>Line 4</p>
Before this: </body>

It won't work to all the HTML with a line of text, which is what your code
does - you have to preserve the existing HTML and add to it. A simple way to
do that is to use the Replace method to add your HTML ahead of "</body>"
message text, including close body tag
Dim strText as String = "<p>Testing...</p><p></p><p></p><p>Line
4</p></body>"
Dim strHTML as String = oMsg.HTMLBody
'use Replace to add message text in the body
oMsg.HTMLBody = Replace(strHTML,"</body>", strText)

You can format your message text anyway you like, but you'll need to learn
some HTML to do that :)

Hi Dave,

I followed your instruction but it did not come out right. Here's my
code:

Dim oApp As Outlook._Application
oApp = New Outlook.Application
Dim oMsg As Outlook._MailItem
oMsg = CType(oApp.CreateItemFromTemplate("C:\BOSLH.oft"),
Outlook.MailItem)
oMsg.Subject = "Testing..."
oMsg.HTMLBody = "Testing..." & vbCrLf & vbCrLf & "Line 4"
oMsg.To = "(e-mail address removed)"

The email that came thru did not have the logo as designed. Instead,
it has a blank look to it and the logo is attached with the email. Can
you please give me more instruction on how to carry this out, "You'll
need to find the point after the <IMG> tag for your logo where you want
to insert the text of the message that you want to send". I noticed
that vbCrLf does not work with HTMLBody. A million thanks to you.
Option 2 is less technically challenging, so why not start with that.

Step #1 is creating a template in the Outlook HTML editor:
1. Create a new blank message using your stationery
2. From the main Outlook menu (I'm using Outlook 2003) click Actions >
New
Mail Message Using > Microsoft Office Outlook (HTML)
3. Put your cursor in the body of the stationery message, click Ctrl+A to
select all and then Ctrl+C to copy
4. Put your cursor in the body of the Outlook HTML message and click
Ctrl+V
to paste. That won't grab the background image if you have spec'd one -
to
add that you will need to use Format > Background, etc. from the message
menu
5. Create an Outlook template file (OFT) from the HTML message by
clicking
File > Save As... and selecting Outlook template (*.oft) as the type.
Save
it to a directory that your WinForms application can access

Step #2 is using the template in your code when you create a new item.
Assuming your Outlook application object is mobjOlApp and the template is
at
C:\CompanyLogo.oft
Dim myMail as Outlook.MailItem
myMail =
CType(mobjOlApp.CreateItemFromTemplate("C:\CompanyLogo.oft"),Outlook.MailItem)

myMail.HTMLBody will have the full HTML source for your message, with
<HTML>, <HEAD> and <BODY> tags, etc. You'll need to find the point after
the
<IMG> tag for your logo where you want to insert the text of the message
that you want to send, wherever that may be. Modify the value of HTMLBody
so
that it includes your text. Then set the values of Subject, add your
recipient(s) and call Send. Does that get you what you need?

Hi Dave,

Firstly, thank you for your help. I googled on this topic for the past
day and a half but came up empty. I'm still green in this area so I
would appreciate it very much if you have some code sample for me to
look at -- for Option 1 and/or 2. At least now I know it can be done.
I was ready to give in and tell me boss otherwise. I really, really
appreciate your help!!! Thanks a million.


Dave Kane [MVP - Outlook] wrote:
Outlook stationery really belongs to the Word editor, and there may be
an
easy way to do this through Word automation. I can suggest two
different
approaches using Outlook

Option #1: You can set the HTMLBody property of your Outlook.MailItem
to
repro the stationery that you created, including whatever formatted
text
you
want in the email. But if you look at the HTML source for your
stationery
you will see that the src property of the IMG tag for the company logo
looks
something like src="cid:760521916@20082002-17cb". That cid value
corresponds
to the PR_ATTACH_CONTENT_ID (&H3712001E) property of a hidden
attachment
to
the message that holds the image. So in addition to specifying the
HTML
source of the message by setting HTMLBody you would need to add the
logo
image as an attachment and set its PR_ATTACH_CONTENT_ID. The Outlook
object
model will allow you to add the attachment, but to set any properties
on
an
attachment you would have to use ExMAPI or a component like
Redemption.
That's the most flexible approach since you can easily change the HTML
and
the image.

Option #2: Reproduce your stationery template using the Outlook HTML
editor,
save it as as an OFT file and then use Outlook's
CreateItemFromTemplate
method to create your new MailItem. The HTMLBody property of the new
MailItem will contain the full HTML source for the message, including
an
IMG
src pointing to the hidden attachment of your logo image. You would
need
to
read that property, parse it to find where to place your message text
and
then reset the value of HTMLBody. The coding is simpler, but you need
to
rebuild the OFT file if your logo changes.


Hi,

In my VB.NET Windows form application, I'm using the Outlook object
to
compose and
automatically send email. The spec also requires that I integrate
the
company logo on the top of the email to look like the company
letterhead. I tried creating a html page with the company logo
design
on it and set
it as the default stationery. It works when I compose a new email
manually from Outlook. But when the email is composed and sent from
VB.NET code using the Outlook MailItem, for reason beyond my level
of
understanding, the default stationery is not used. Instead all I
see
is a blank template. I'm fairly new at this game. I greatly
appreciate your help.
 
J

jonathan_kha

Hi Dave,

Your expertise helped me immensely. Everything you said worked.
Please check your email and let me know.

Thanks,
Jonathan Kha

Hi Dave,

I followed your instruction but it did not come out right. Here's my
code:

Dim oApp As Outlook._Application
oApp = New Outlook.Application
Dim oMsg As Outlook._MailItem
oMsg = CType(oApp.CreateItemFromTemplate("C:\BOSLH.oft"),
Outlook.MailItem)
oMsg.Subject = "Testing..."
oMsg.HTMLBody = "Testing..." & vbCrLf & vbCrLf & "Line 4"
oMsg.To = "(e-mail address removed)"

The email that came thru did not have the logo as designed. Instead,
it has a blank look to it and the logo is attached with the email. Can
you please give me more instruction on how to carry this out, "You'll
need to find the point after the <IMG> tag for your logo where you want
to insert the text of the message that you want to send". I noticed
that vbCrLf does not work with HTMLBody. A million thanks to you.
Option 2 is less technically challenging, so why not start with that.

Step #1 is creating a template in the Outlook HTML editor:
1. Create a new blank message using your stationery
2. From the main Outlook menu (I'm using Outlook 2003) click Actions > New
Mail Message Using > Microsoft Office Outlook (HTML)
3. Put your cursor in the body of the stationery message, click Ctrl+A to
select all and then Ctrl+C to copy
4. Put your cursor in the body of the Outlook HTML message and click Ctrl+V
to paste. That won't grab the background image if you have spec'd one - to
add that you will need to use Format > Background, etc. from the message
menu
5. Create an Outlook template file (OFT) from the HTML message by clicking
File > Save As... and selecting Outlook template (*.oft) as the type. Save
it to a directory that your WinForms application can access

Step #2 is using the template in your code when you create a new item.
Assuming your Outlook application object is mobjOlApp and the template is at
C:\CompanyLogo.oft
Dim myMail as Outlook.MailItem
myMail =
CType(mobjOlApp.CreateItemFromTemplate("C:\CompanyLogo.oft"),Outlook.MailItem)

myMail.HTMLBody will have the full HTML source for your message, with
<HTML>, <HEAD> and <BODY> tags, etc. You'll need to find the point after the
<IMG> tag for your logo where you want to insert the text of the message
that you want to send, wherever that may be. Modify the value of HTMLBody so
that it includes your text. Then set the values of Subject, add your
recipient(s) and call Send. Does that get you what you need?

Hi Dave,

Firstly, thank you for your help. I googled on this topic for the past
day and a half but came up empty. I'm still green in this area so I
would appreciate it very much if you have some code sample for me to
look at -- for Option 1 and/or 2. At least now I know it can be done.
I was ready to give in and tell me boss otherwise. I really, really
appreciate your help!!! Thanks a million.


Dave Kane [MVP - Outlook] wrote:
Outlook stationery really belongs to the Word editor, and there may be an
easy way to do this through Word automation. I can suggest two different
approaches using Outlook

Option #1: You can set the HTMLBody property of your Outlook.MailItem to
repro the stationery that you created, including whatever formatted text
you
want in the email. But if you look at the HTML source for your stationery
you will see that the src property of the IMG tag for the company logo
looks
something like src="cid:760521916@20082002-17cb". That cid value
corresponds
to the PR_ATTACH_CONTENT_ID (&H3712001E) property of a hidden attachment
to
the message that holds the image. So in addition to specifying the HTML
source of the message by setting HTMLBody you would need to add the logo
image as an attachment and set its PR_ATTACH_CONTENT_ID. The Outlook
object
model will allow you to add the attachment, but to set any properties on
an
attachment you would have to use ExMAPI or a component like Redemption.
That's the most flexible approach since you can easily change the HTML
and
the image.

Option #2: Reproduce your stationery template using the Outlook HTML
editor,
save it as as an OFT file and then use Outlook's CreateItemFromTemplate
method to create your new MailItem. The HTMLBody property of the new
MailItem will contain the full HTML source for the message, including an
IMG
src pointing to the hidden attachment of your logo image. You would need
to
read that property, parse it to find where to place your message text and
then reset the value of HTMLBody. The coding is simpler, but you need to
rebuild the OFT file if your logo changes.


Hi,

In my VB.NET Windows form application, I'm using the Outlook object to
compose and
automatically send email. The spec also requires that I integrate the
company logo on the top of the email to look like the company
letterhead. I tried creating a html page with the company logo design
on it and set
it as the default stationery. It works when I compose a new email
manually from Outlook. But when the email is composed and sent from
VB.NET code using the Outlook MailItem, for reason beyond my level of
understanding, the default stationery is not used. Instead all I see
is a blank template. I'm fairly new at this game. I greatly
appreciate your help.
 

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