Encrypting Outlook E-Mail (1 Viewer)

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 12:07
Joined
Feb 28, 2001
Messages
27,163
I'm trying to encrypt selected outbound E-mail, built by Access 2003 and sent through Outlook 2003.

The catch is, I can't quite get this to work. I've looked all over the net to get this far. Does anyone have insights that might help?

Here are the significant parts. I have removed some of the code that does not appear to be relevant, just some logging to an event table. An external routine builds the To, CC, Subject, and Mail Body as strings. They get passed in to my mail send routine correctly. The UID is a code number for the user internal ID, which is the PK of a lookup table of authorized users. It is used only in the logging routine that I don't show in the code. Formal parameter bEnc is TRUE if I need to encrypt the mail. If traps occur, I return a status code other than zero for the function value.

Code:
Public Function SendAMsg(UID As Long, OLSbj As String, OLTo As String, _
    OLCC As String, OLBdy As String, bEnc As Boolean) As Long

Dim OLApp As Outlook.Application        'ACCESS construct to allow us to bit-twiddle Outlook
Dim OLMsg As Outlook.MailItem           'a mail message within Outlook
Dim OLNS As Outlook.NameSpace           'need this to get to the MAPI name space
Dim OLFld As Outlook.MAPIFolder         'need this for sending mail
Dim OLIns As Outlook.Inspector          'need this to set encryption
Dim OLBar As Office.CommandBar          'make this a command-bar object
Dim OLEnc As Office.CommandBarButton
Dim OLSgn As Office.CommandBarButton

...

    Set OLApp = Nothing                 'start with simplest assumption
    
    On Error Resume Next                'block traps while we poke around
    Set OLApp = GetObject(, "Outlook.Application") 'find existing instance of Outlook
    On Error GoTo 0                     'remove the trap block

...

    OLNew = False                       'assume we found one
    If OLApp Is Nothing Then            'did we find an existing Outlook we could use?
        OLNew = True                    'no, have to create one, so ...
        Set OLApp = CreateObject("Outlook.Application")    'create a private apps object
    End If

...

    Set OLNS = OLApp.GetNamespace("MAPI")   'get into the namespace

    OLNS.Logon , , False, False         'we created or tested the app already

    Set OLFld = OLNS.GetDefaultFolder(olFolderOutbox) 'find the outbox folder

    Set OLMsg = OLFld.Items.Add(olMailItem)   'from which we will send a single mail message

...

    OLMsg.Importance = olImportanceHigh 'start populating the mail message
    OLMsg.To = OLTo                     'fill in first set of recipients
    OLMsg.cC = OLCC                     'fill in second set of recipients
    OLMsg.Subject = OLSbj               'fill in the message subject
    OLMsg.Body = OLBdy                  'fill in the message body
    OLMsg.BodyFormat = olFormatPlain    'make it totally plaintext

'    encrypt if requested to do so

    If bEnc = True Then                 'see if we even need to bother
                
        Set OLIns = OLMsg.GetInspector  'get the inspector so we can open the toolbars
        
        Set OLBar = OLIns.CommandBars("Standard")   'get to the standard toolbar
        
        Set OLEnc = OLBar.FindControl(, EncryptMsgCtrl) 'which has ENCRYPT and SIGNDIGITAL
        Set OLSgn = OLBar.FindControl(, DigitalSigCtrl)
        
        If Not OLSgn Is Nothing Then    'if we found it, ...
            If OLSgn.Enabled = True Then    'and if it is enabled, ...
                If OLSgn.State = msoButtonUp Then   'and if it is not already set, ...
                    OLSgn.Execute
                End If
            End If
        End If
        
        If Not OLEnc Is Nothing Then
            If OLEnc.Enabled = True Then
                If OLEnc.State = msoButtonUp Then
                    OLEnc.Execute
                End If
            End If
        End If
        
    End If                              'end - if we are going to encrypt the message

    OLMsg.Send                          'send it on its way


OK, here's my observation. It does not encrypt, but it DOES send.

In debugger, I single step the code to the point where I have set OLEnc and OLSgn to valid command bar buttons. The ugly little If-ladders are there so I can debug one step at a time.

In the locals window I can see that these buttons are enabled. Due to defaults set in my Outlook account, the code does not attempt to set the Digital Signature button because it is already set (state=msoButtonDown). But the Encryption button is NOT set by default in this case. So I check that I have instantiated the OLEnc button, which I have. Locals window shows me its content. I check that it is enabled. It is. I check that the button is currently up. It is. So I try to encrypt the message with that .Execute. It executes, no traps occur, ... but the resultant message is not encrypted. If I try to set the button by direct value assignment, I get an error trap because you can't do something like:

OLEnc.state = msoButtonDown

The Help says that intrinsic command bar buttons are read-only, though a user-added button could possibly be read/write. Well, these are intrinsic.

I've explored the possibility that I'm missing a library, but I have the MS Outlook 11.0 library referenced. The Encrypt button works manually if I am actually running Outlook, so I'm at a loss for what I've left out. Anyone have any ideas?

(In case anyone wants to try...) The constants you need are

Public Const DigitalSigCtrl = 719 'for outlook, the SIGN IT button
Public Const EncryptMsgCtrl = 718 'for outlook, the ENCRYPT IT button
 

darbid

Registered User.
Local time
Today, 19:07
Joined
Jun 26, 2008
Messages
1,428
Hi Doc Man,

I had a look at your toolbar execution and I cannot see anything wrong with it.

For Outlook specific questions I usually head over to Sue Mosher and her book. I am sure you are aware of both.

For example here is a discussion on your topic http://www.outlookcode.com/codedetail.aspx?id=390

The first post talks about problems using Word as the editor but that does not appear to apply to you.
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 12:07
Joined
Feb 28, 2001
Messages
27,163
darbid, thanks for the look-over. I was not aware of Sue Mosher's book - but I probably will be soon. Thanks also for the link. You are correct, we don't use Word as an editor. Though we can send Word as attachments, our security guys go nuts if we send a mail body that could somehow embed a macro. So we don't even sent HTML. Just plain (and I mean PLAIN) text.

It's driving me nuts because I can see the stuff I wanted to see in the locals window. I can see the controls change from "nothing" to the appropriate object types. I can see the properties. I just can't trigger the change I want.
 

darbid

Registered User.
Local time
Today, 19:07
Joined
Jun 26, 2008
Messages
1,428
i have set up a test.

Can you display your mailitem and then execute your commandbuttons.

I think you will find this will solve your problem.
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 12:07
Joined
Feb 28, 2001
Messages
27,163
Can't do this until Tuesday. I'm a contractor with a USA Dept. of Defense branch. The office is closed until then and I can't bring that particular piece of code home because of some of the network configuration info it contains. (Which is also why encryption is required.)

Are you suggesting that when I do the .Display operation, it makes the buttons work? That would be fine with me. I'll surely give it a whack then and let you know what happens. Thanks again!
 

darbid

Registered User.
Local time
Today, 19:07
Joined
Jun 26, 2008
Messages
1,428
Are you suggesting that when I do the .Display operation, it makes the buttons work? That would be fine with me.
Yes. Fire the .Display method of the mailitem. Then you should be able to do it.

In my opinion you need to "kick start" the inspector object (in this case a mailitem) into action.

On that thought and I did not test this if you don't want to display the email you might test what happens when you simply .Activate the inspector object. By this I do not mean the mailitem object.

http://msdn.microsoft.com/en-us/library/aa219395(v=office.11).aspx
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 12:07
Joined
Feb 28, 2001
Messages
27,163
I think I see the point. If it ain't active, I can click buttons until the cows come home and it might not care. I'll keep both .Activate and .Display in mind when testing. However, in this case, there is no particular problem with making the mail visible before sending it. I'll see what happens Tuesday.
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 12:07
Joined
Feb 28, 2001
Messages
27,163
darbid, thanks again. Tested it this morning first thing. Putting the OLIns.Activate just behind the Set OLIns = ... works perfectly.

To the other members of the forum, knowing that the code snippet works to encrypt and digitally sign outbound e-mail now works, is it worth composing the snippet and putting it in one of the sample areas?
 

darbid

Registered User.
Local time
Today, 19:07
Joined
Jun 26, 2008
Messages
1,428
Great work. I am glad I could help. I do find Outlook a little strange sometimes.
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 12:07
Joined
Feb 28, 2001
Messages
27,163
"Strange" ain't the half of it. For instance, that "Inspector" ... is a window. You inspect the object by seeing in a window, but go figure why it is called an inspector instead of just calling it a window. And when I did the digging, you could use .Display, but that is a "declining" feature now being replaced with .Activate - but it does the same bloody thing. Half the time my problem isn't that I don't understand the mechanism - it is that I don't understand their nomenclature and can't find the glossary!
 

Users who are viewing this thread

Top Bottom