Email conundrum

Eljefegeneo

Still trying to learn
Local time
Today, 01:05
Joined
Jan 10, 2011
Messages
902
I have some code that sends an email with the body of it in HTML format. I have the code item
Code:
    [FONT=&quot]myMailObj.BodyFormat = olFormatHTML[/FONT]
I am using MS Outlook to send the emails.
If I use .Display and then send the email manually, all is well. My Icons at the bottom of the email appear complete with the hyperlink code imbedded.
But if I use .Send, then the icons in the bottom of my email with hyperlinks encoded so not show up. What I get is something like this:
Code:
 [FONT=&quot][URL="http://www.panambc.com"][COLOR=blue]<IMG border=0 hspace=0 alt='' src= C:\Users\Gene\Desktop\EmailBlast\Pab1.jpg ? align=baseline>[/COLOR][/URL][URL="https://www.facebook.com/PanAmBc/"][COLOR=blue]<IMG border=0 hspace=0 alt='' src= C:\Users\Gene\Desktop\EmailBlast\FaceBook.jpg ? align=baseline>[/COLOR][/URL][URL="https://www.youtube.com/user/panambc?ob=0&feature=results_main"][COLOR=blue]<IMG border=0 hspace=0 alt='' src= C:\Users\Gene\Desktop\EmailBlast\YouTube.jpg ? align=baseline>[/COLOR][/URL][URL="https://www.radiopanam.com/listen/"][COLOR=blue]<IMG border=0 hspace=0 alt='' src= C:\Users\Gene\Desktop\EmailBlast\RadioPA.jpg ? align=baseline>[/COLOR][/URL] [/FONT]
I am using a recordset for the email addresses, anywhere from 10 to 500 at a time.
Is there any way to solve this?
 
Never give up! I spent some time thinking about this. Why did a single email that had .Display and was then manually sent worked fine, the icons showed up, but not when .Send is used. So I changed it to:

Code:
.Display
.Send
And volia! All seems to be well. It is a little strange seeing all the emails being sent, they appear and are send in a few nanoseconds, but it does seems to work OK. Going to try it with a large number of names later on today.
 
Glad you got it working

In case it helps, I have 2 functions for sending email with Outlook
1. SendEmailUsingOutlook
2. SendEmailDisplayOutlook

The first one works 'silently' i.e. without opening Outlook
If either are of use to you, please adapt for your own purposes

You will need to define some string values such as strSendTo as I do this at the start of the module rather than in each procedure

Code:
Public Function SendEmailUsingOutlook(strSendTo As String, strCopyTo As String, strSubject As String, strMessage As String, strAttachment As String)

On Error GoTo ErrHandler

'CR v5161 - Uses late binding to send email without displaying Outlook

Dim objOutlook As Object
Dim objOutlookMsg As Object
Dim StartOutlookFlag As Boolean

StartOutlookFlag = False

' Create the Outlook session.
If IsAppRunning("Outlook.Application") = True Then
    'Use existing instance of Outlook
    Set objOutlook = CreateObject("Outlook.Application")
Else
   'Could not get instance of Outlook, so create a new one
        Path = GetAppExePath("outlook.exe")    'determine outlook's installation path
        Shell (Path), vbMinimizedFocus   'start outlook
        Do While Not IsAppRunning("Outlook.Application")
            DoEvents
        Loop
        Set objOutlook = GetObject(, "Outlook.Application") 'Bind to new instance of Outlook
        StartOutlookFlag = True 'needed so Outlook can be closed later
End If

' Create the message.
Set objOutlookMsg = objOutlook.CreateItem(0)

' Add the To/Subject/Body/Attachments to the message then send the message
With objOutlookMsg
    .To = strSendTo
    .CC = strCopyTo
    .Subject = strSubject
    .Body = strMessage
    If Nz(strAttachment, "") <> "" Then
        .Attachments.Add strAttachment
    End If
   ' .Display 'do not display message
    .Save
    .Send
End With

Set objOutlook = Nothing
Set objOutlookMsg = Nothing

'close Outlook if it was opened for this function - time may need modifiying
DoEvents
DoEvents
Wait 5 'allow time to send message
If StartOutlookFlag = True Then CloseOutlook

ErrHandlerExit:
   Exit Function

ErrHandler:
   If Err.Number <> 287 Then 'And err.Number <> 429 Then
        MsgBox "Error " & Err.Number & " in SendEMailUsingOutlook routine: " & Err.Description
   End If
   Resume ErrHandlerExit

End Function


Code:
Public Function SendEmailDisplayOutlook(strSendTo As String, strCopyTo As String, strSubject As String, strMessage As String, strAttachment As String)

On Error GoTo ErrHandler

'Uses late binding to send email(so that a reference to Outlook library is not needed)

Dim objOutlook As Object
Dim objOutlookMsg As Object
Dim sAPPPath        As String

' Create the Outlook session.
Set objOutlook = CreateObject("Outlook.Application")

' Create the message.
Set objOutlookMsg = objOutlook.CreateItem(0)
' Add the To/Subject/Body/Attachments to the message then display the message for editing
With objOutlookMsg
    .To = strSendTo
    .CC = strCopyTo
    .Subject = strSubject
    .Body = strMessage
    If Nz(strAttachment, "") <> "" Then
        .Attachments.Add strAttachment
    End If
    .display
    '.Send
End With

Set objOutlook = Nothing
Set objOutlookMsg = Nothing

ErrHandlerExit:
   Exit Function

ErrHandler:
   MsgBox "Error " & Err.Number & " in SendEMailDisplayOutlook routine: " & Err.Description
   Resume ErrHandlerExit

End Function
 
Last edited:
I think my problem was with the HTML code and apparently it likes to have a moment to be inserted into the body of an Outlook email, I.e. Using the .Display before the .Send. It isn't a big deal in my case and after numbing my brain learning VBA and SQL, I really did not want to learn HItML so I could insert jpegs into the body of the email.

Hopefully in future versions, the gurus of MS Access will figure it out.

I will see if using your "hidden Outlook" will solve my problem.

Thanks for the input.
 
I don't know anything about email but my guess, and that's all it is...

You add an img tag, pointing to your local drive.

That wont work because recipients don't have access to your drive. You need to include the file as some sort of attachment so that the file is sent with the email.

.Display presumably parses and formats the email as if you had written the body into the outlook interface and generates the attachment.

There must be a way of including the files as attachments. The question is then how do you set the source in the html?
 
The code that I now have works fine. I tested it not only on my computer but I sent it to someone else and it seemed to work fine. My guess is as good as yours why the .Send alone does not work properly. Apparently the email that is sent with the jpgs embedded in the HTML needs to be first displayed in a outgoing email and then sent. Thus the inclusion of the first .Display which as it states displays the email and the the .Send which sends it.

I tested it today with 46 individual emails to be sent and it worked fine. Mine is an old computer by today's standards and it took less than 10 seconds to send them.



Thus I am marking this as solved unless someone can come up with a better alternative.
 

Users who are viewing this thread

Back
Top Bottom