Html emails

J

John

Hi

I am using the following code to send html emails via outlook.

objOutlook = CreateObject("Outlook.Application")
objOutlookMsg = objOutlook.CreateItem(Outlook.OlItemType.olMailItem)
fso = CreateObject("Scripting.FileSystemObject")
ts = fso.OpenTextFile(m_objParentForm.txtHtml.Text.ToString, 1)
eMessage = ts.ReadAll
With objOutlookMsg
eto = "(e-mail address removed)"
objOutlookRecip = .Recipients.Add(eto)
objOutlookRecip.Type = Outlook.OlMailRecipientType.olTo
.Subject = "My Subject"
.HTMLBody = eMessage
.Send()
End With
objOutlook = Nothing

The problem is that bodies of emails are coming out blank, where there
should be html read from file. What am I doing wrong?

Thanks

Regards
 
C

Cor

Hi John,
This is not VB.net code, it is VB scripting.
Information about this, you can ask better in a scripting newsgroup.
As far as I can see is the problem in this part of the code
fso = CreateObject("Scripting.FileSystemObject")
ts = fso.OpenTextFile(m_objParentForm.txtHtml.Text.ToString, 1)
I thought that with this code alone, you don't read the file.

I hope this helps a bit.
Cor
 
F

Fergus Cooney

Hi John,

Some questions.

Have you verified that you are opening your file correctly?
Have you verified that you are reading your file successfully?
Does the file contain valid HTML?
If you assign a string of HTML, eg "<html><body>Please work for
me!!</body></html>", to the HTMLBody, does it accept it?

Regards,
Fergus
 
J

John

Yes to all the questions. I placed a breakpoint and emessage value shows at
each step when I bring the mouse over it. The file is read correctly into
emessage, which is the most puzzling thing.

Thanks

Regards
 
J

John

Hi

It seems to compile OK in vb.net. I am not too comfortable with substring so
I still use left/right etc. Is there an example of html mailing from vb.net,
using outlook, somewhere?

Thanks

Regards
 
F

Fergus Cooney

Hi John,

If you post your actual code, ie something that I can run immediately,
then I can try it out.

Regards,
Fergus
 
J

John

Here it is;

objOutlook = CreateObject("Outlook.Application")
objOutlookMsg = objOutlook.CreateItem(Outlook.OlItemType.olMailItem)
fso = CreateObject("Scripting.FileSystemObject")
ts = fso.OpenTextFile(m_objParentForm.txtHtml.Text.ToString, 1)
eMessage = ts.ReadAll
With objOutlookMsg
eto = "(e-mail address removed)"
objOutlookRecip = .Recipients.Add(eto)
objOutlookRecip.Type = Outlook.OlMailRecipientType.olTo
.Subject = "My Subject"
.HTMLBody = eMessage
.Send()
End With
objOutlook = Nothing

Thanks

Regards
 
F

Fergus Cooney

Hi John,

Lol. That's the same code as in your original post!!

I tried it when you first posted it. Unfortunately it doesn't compile. At
the least I would like a complete Sub/Function with all variables declared,
etc. Then I'd have to build a test harness around it.

What I was asking and hoping for is something that I can <run
immediately>. A zip of a working project (complete with the correct
references) that I can simply open and run.

Want to give it another shot?

Regards,
Fergus
 
J

John

Private Sub DoE_Mail(ByVal emailaddr As String) 'emailaddr is the email
address to send html mail to

String, eMessage As String
Dim objOutlook As Outlook.Application
Dim objOutlookMsg As Outlook.MailItem
Dim objOutlookRecip As Outlook.Recipient
Dim fso
Dim ts
fso = CreateObject("Scripting.FileSystemObject")
' Create the Outlook session.
objOutlook = CreateObject("Outlook.Application")
esubject = "My Subject"
eMessage = ""
ts = fso.OpenTextFile("C:\test.htm", 1)
eMessage = ts.ReadAll
MsgBox(eMessage) 'Check the value of emessage to make sure htm file is read
succesfully
objOutlookMsg = objOutlook.CreateItem(Outlook.OlItemType.olMailItem)
With objOutlookMsg
eto = emailaddr
objOutlookRecip = .Recipients.Add(eto)
objOutlookRecip.Type = Outlook.OlMailRecipientType.olTo
..Subject = esubject
..HTMLBody = eMessage
..Send()
End With
objOutlook = Nothing
End Sub
 
C

Cor

John,
I am sorry, I do not quiet understand how you use this code but I will try
to help you.
Private Sub DoE_Mail(ByVal emailaddr As String) 'emailaddr is the email
address to send html mail to
String, eMessage As String

I told you in my first message that the problem was probably with the read
from the Scripting.FileSystemObject

String, eMessage as String is a mix between Vb.net and C code.

I don't know if it works, but I would change that in
\\\
Dim eMessage As String
///

It is not impossible that it works after this change (I thought the only
problem was that HTML)
But see downbelow for probably another error.
Dim objOutlook As Outlook.Application
Dim objOutlookMsg As Outlook.MailItem
Dim objOutlookRecip As Outlook.Recipient
Dim fso
Dim ts
fso = CreateObject("Scripting.FileSystemObject")
' Create the Outlook session.
objOutlook = CreateObject("Outlook.Application")
esubject = "My Subject"
eMessage = ""
ts = fso.OpenTextFile("C:\test.htm", 1)
eMessage = ts.ReadAll ////
MsgBox(eMessage) 'Check the value of emessage to make sure htm file is
read
\\\\
A part of my newsgroup post is gone, was it a webpage?
Then delete this because that gives than a big error.
 
F

Fergus Cooney

Howdy Cor,

Don't bother with the code in these posts. John's reposted - topic: html
emails problem - and has also provided a Solution to run. That's the place to
continue this one. I've run the project and it worked perfectly for me (after
a minor change). I'm using Outlook 2000 and VS.2002. If you've got later
versions, maybe it will successfully fail (!) for you and provide some useful
information.

Anyway, see you in the other thread, eh?

Cheers,
Fergus
 

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