Outlook email -- A program is trying to automatically send e-mail onyour behalf . . .

R

robboll

This VBA sends an outlook email from an application. But requires
human intervention to complete the send.

Note that the code that is commented out is supposed to suppress the
message "A program is trying to automatically send an e-mail on your
behalf. . ." but is failing with a Run-time error '424' Object
required message. I found the code from:
http://www.add-in-express.com/docs/outlook-security-manager-automate.php
Any suggestions on how to deal with the Run-time error?

Private Sub Command0_Click()

Dim db As DAO.Database
Dim retstatus As Long
Dim vTime As Variant
Dim objOutlook As Outlook.Application
Dim objOutlookMsg As Outlook.MailItem
Dim theNamespace As Outlook.Namespace
Dim myRecipient As Outlook.Recipient
Set db = CurrentDb()

'OlSecurityManager.ConnectTo OutlookApp ' <----- Fails with Run-
time error '424': Object required
'OlSecurityManager.DisableOOMWarnings = True
'On Error GoTo Finally

Set objOutlook = New Outlook.Application
Set theNamespace = objOutlook.GetNamespace("MAPI")
Set objOutlookMsg = objOutlook.CreateItem(olMailItem)

objOutlookMsg.To = "(e-mail address removed)"
objOutlookMsg.Subject = "We thought you might want to know!"
objOutlookMsg.Body = "ALARM! ALARM! ALARM! ALARM!"
objOutlookMsg.Importance = olImportanceHigh
objOutlookMsg.Send

Set myRecipient = Nothing
Set objOutlookMsg = Nothing
Set theNamespace = Nothing
Set objOutlook = Nothing

'Finally:
'OlSecurityManager.DisableOOMWarnings = False

End Sub

Thanks for any suggestions!

RBollinger
 
R

robboll

I just realized that the referenced website is a vender advertising
their Add-Ins DUHHHHH

which makes the question more basic - - - how send an email withhout
the warning.

I have an Access application that when a new record is added, it needs
to send an email with info from a few fields from the entry. I don't
want the user to have to acknowledge the email from each new record.
I understand the security aspect of this, so the process should only
allow emails from the MS Access application and turn on the security
feature after each send.

I am seeing that there are a number of venders who are trying to sell
their solutions. I would go this route, but I know how quickly things
change with service packs and OS updates. There has to be a code
solution. Thanks for any suggestions!

RBollinger
 
P

Peter Hibbs

To send an email from Access via MS Outlook and bypass the security
warnings you can use Outlook Redemption. This is a .dll file which is
installed and registered on the PC. Then you can use the code below to
send an email direct without any security warnings being shown. And it
is free.

To use Outlook Redemption first go to this site :-

http://www.dimastr.com/redemption/

and click on the Download Outlook Redemption link at the bottom of the
page. Then click on the Download Developer Version link to download
the zip file to your PC. Unzip the files and install the
Redemption.dll as instructed. The easiest option is to use the Install
program provided. You may find the Redemption.dll file is already
installed on your PC as some virus checkers use it.

Next create a new Module in your database and save it as
modOutlookCode (or whatever you want), copy and paste the code below
into the module and save it again.


'----------Redemption Code ----------------------
Option Compare Database
Option Explicit

Public Function SendMailAuto(vRecipients As String, _
vBCC As Variant, _
vSubject As Variant, _
vBody As Variant, _
vAttachments As Variant) As Boolean

'Send an automatic email via Microsoft Outlook
'Entry (vRecipients) = EMail address of recipient
' (vBCC) = EMail addresses of recipients
' (vSubject) = Subject text
' (vBody) = Email body text
' (vAttachments) = List of files to attach to e-mail
'Exit (SendMailAuto) = Returns True if no error reported
' (SendMailAuto) = Returns False if error reported

Dim vCount As Long, vArray() As String
Dim SafeItem As Object, oItem As Object
Dim objApp As Object, NS As Object

On Error GoTo ErrorCode

DoCmd.Hourglass True

'Create instance of Outlook Application
Set objApp = CreateObject("Outlook.Application")
Set NS = objApp.GetNamespace("MAPI")
NS.Logon

'Create an instance of Redemption.SafeMailItem
Set SafeItem = CreateObject("Redemption.SafeMailItem")
Set oItem = objApp.CreateItem(0)
SafeItem.Item = oItem
SafeItem.To = vRecipients
' If Not IsNull(vCC) Then SafeItem.CC = vCC '(not used here)
If Not IsNull(vBCC) Then SafeItem.BCC = vBCC
If Not IsNull(vSubject) Then SafeItem.Subject = vSubject
If Not IsNull(vBody) Then SafeItem.Body = vBody

'Add attachments (if any)
If Not IsNull(vAttachments) Then
vArray = Split(vAttachments, ",")
For vCount = 0 To UBound(vArray)
SafeItem.Attachments.Add (vArray(vCount))
Next
End If

'Send email and close
SafeItem.Send
Set SafeItem = Nothing
Set oItem = Nothing
Set NS = Nothing
Set objApp = Nothing
DoCmd.Hourglass False
SendMailAuto = True
Exit Function

ErrorCode:
DoCmd.Hourglass False
MsgBox Err.Description
SendMailAuto = False

End Function



To send an email just use the following code :-

If SendMailAuto(Me.txtTo, Me.txtBCC, Me.txtSubject, Me.txtBody,
Me.txtAttachments) = False Then
MsgBox "Some error occurred!", vbCritical + vbOKOnly, "EMail
Error"
End If

where the Text boxes (or variables) hold the recipient's email
address, BCC addresses, Subject and Body text.
If there is more than one email address in the To: and/or BCC: fields
then each address should be separated by a semi-colon. The
txtAttachments field holds the full pathname to any attachments to be
sent, i.e. something like "C:\Temp\Filename.txt". If there are more
than one file to attach then they should be separated by commas.

You do not need to set up a reference to Outlook in the VBA code, this
is particularly useful if the database is likely to be used on a
number of different PCs because, even though it is possible to have
different versions of Word, Excel, Access, etc installed on the same
PC, it is only possible to have one version of Outlook installed and
this could be a different version to the one used by the developer.

The email (and attachments) are temporarily stored in the Outlook
Drafts folder but are deleted automatically when you next use Outlook.
However, there is a 2GB limit on the total size of files stored in an
Outlook folder, if a large number of emails with large attachments
files is sent together, the 2GB limit may be exceeded in which case
Outlook will 'fall over' big time. Make sure this does not happen.

Also, if you are intending to send an email to a large number of
recipients, it is NOT a good idea to have a large number of email
addresses in the BCC: field. According to Outlook MVP, Sue Mosher, it
is likely that the recipient's ISP will flag the email as SPAM and bar
the email. As an alternative, she suggests sending one email at a time
which might take longer but would be more reliable. Also the sender's
ISP will set a limit on the number of email addresses that can be
entered in the BCC: field which probably varies with each ISP.

If you have any queries regarding the Redemption code then you should
post the question to the MS Outlook NG as Dmitry Streblechenko (the
author of Redemption) answers questions there.

HTH

Peter Hibbs.
 
M

MikeB

You can also use Windows API calls to capture the windows security dialog
automatically. I never tried it using VBA but have done it in Delphi.
Also, the security warning (IIRC) only comes up once per session, not once
per email, so if the OP can keep the OL object alive for the session, the
momentary click of the dialog shouldn't be too much of a bother.
 
A

Arvin Meyer [MVP]

The API is part of what Redemption does, although it is far more
sophisticated than Windows message capturing code. Which API(s) are you
using?

Actually, if you are looping and sending emails individually, each email
creates a new Outlook session and therefore does need to capture the message
with each email sent. If one is using CC or BCC to send a string of email
addresses to the server, you are correct, the warning will only come up once
per session.
 
M

MikeB

Arvin Meyer said:
The API is part of what Redemption does, although it is far more
sophisticated than Windows message capturing code. Which API(s) are you
using?

FindWindow, FindWindowEx, SetActiveWindow,GetCursorPos, GetWindowRect,
SetCursorPos, mouse_event, SendMessage.

I just run a procedure with a timer (in Delphi DLL this is in a separate
thread) to check for the security warning and process the click of the OK
button if it finds it. You will see a flash as the dialog appears and
closes, but it is barely noticable. My DLL was used to synchronize DB
contacts with Outlook Contacts and not email but it is the same warning.
Also, besides clicking the button, the combobox selection is made at the
same time. Don't ask me what is in the ComboBox, it was a year ago that I
last looked at this code....

I could give you the Delphi Source for it if you want to try to dupe it in
VBA. I think it's doable. Let me know if you want and I will put it up on
my web server in a text file. email me off list via hotmail: bauerlie at
hotmail.com
 
K

KB

Peter,

I created the module but how do I call it from a Macro. I am using a macro
that uses the SendObject to send the e-mail. I am not familiar with VBA and
not sure how to make this work. Any help is appreciated.

Thanks.
 
D

Douglas J. Steele

I think you may have to bite the bullet and learn sufficient VBA for this
task.

I don't think there's any way to do it using a macro.
 
P

Peter Hibbs

You will need to use VBA code to do this but then you already have. In
your original post you had a button called Command0 which had your
original code. Providing you have copied the function into a module
correctly all you need to do now is replace the code in your button
click event with code shown below. Something like this :-


Private Sub Command0_Click()

If SendMailAuto("(e-mail address removed)", "", "We thought you
might want to know!", "ALARM! ALARM! ALARM! ALARM!",
"") = False Then
MsgBox "Some error occurred!", vbCritical + vbOKOnly, "EMail
Error"
End If

End Sub


Note that the email address should be within double quotes, my
Newsreader has replaced the first double quote character with a single
quote character and it may or may not do the same in yours. In
practice, of course, you would normally use variables in place of the
function parameters but I will leave you to sort that out.

Also, watch out for line wrapping in the newreader text, the above
code should be on three lines only, i.e.-

If SendMailAuto("ALARM....etc ) = False Then
MsgBox "Some error.....etc
EndIf

HTH

Peter Hibbs.
 

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