E-Mailing through outlook

  • Thread starter Thread starter John
  • Start date Start date
J

John

Hi

Is there a way to e-mail using outlook2002 from within vb.net without
getting the nasty "An application is trying to access you outlook..."
message?

Thanks

Regards
 
John

you can't prevent the Outlook dialog, but you can write
code to fill it in programmatically so it only appears
for an instant. It needs to run in a separate thread but
VB.Net can do that easily enough.

I have code for this

Dermot Balson
Free VBA code for user interfaces, internet connectivity,
encryption
http://www.webace.com.au/~balson/InsaneExcel/Default.html
Last updated August 2003
 
John,
Outlook 2003 has changed the rules such that properly constructed COM Addins
will no longer get the prompt.

http://www.slipstick.com/dev/ol2003problems.htm

I have not tried it, it sounds like Exchange Server has options to control
the prompt on earlier versions of Outlook.
http://www.slipstick.com/dev/ol2003problems.htm
http://www.slipstick.com/outlook/esecup/admin.htm

In case you don't have it the following site provides a number of articles
on using Outlook from .NET.

http://www.microeye.com/resources/res_outlookvsnet.htm

Hope this helps
Jay
 
Why not just use the SmtpMail class and send it through whatever server
Outlook uses?
 
Keeping a record in outlook is handy for future reference. In this way all
emails sent either from the app or from outlook remain in the same place.

Regards
 
Hi Stephen, John,

Or you could do both. Send it with SmtpMail and use Outlook Automation to
stuff a copy in the Sent folder.

Regards,
Fergus
 
If it can be done in one step then obviously it is the preferable choice.
Normally sending an email via outlook will do both in one go.
 
Hi John,

I agree - one step is better than two steps. But if two steps are needed
to get around that pesky dialogue, then let's go dancing!

Dermot's solution sounds interesting. An alternative is a wonderful little
utility called "Push The Freakin' Button" which can look out for given
dialogues and press a specific button. It's on many download sites.
Unfortunately, the source isn't available for this one either. :-(

Regards,
Fergus
 
Fergus,
What John does is his own decision.
But I find it always wrong to fill up an exchange server with mail that is
batch mail.
When you send mail using Outlook then every independent mail message is
stored in the exchange server. The send address is always the same and only
the address from the receiver is different, but that is bad traceable with
only the Exchange server.
I find it much nicer to keep that information in let's say a CRM system by
instance on a SQL server.
Than you only have to keep one mail and the information to whom you did send
that.
The SMTP mail server is not made for nothing.
But it's of course a matter of taste.
You know, some like the tango others the quickstep.
:-)
Cor
 
Howdy Cor - Good afternoon now!!

Isn't it nice to be able to 'converse' again without having to wait
several hours and then be afraid to click on a message in case it disappears!!

I agree that storing multiple copies of a mail is silly, however I don't
believe this is John's intention.

|| You know, some like the tango others the quickstep.

LOL. And others have a style involving drumsticks and a lot of space which
can't* be named!! ;-))

Cheers,
Fergus

* Actually I'm sure many people <have> got a name for it but I'm not going to
let that put me off!!
 
Here is my code to kill the Outlook permission dialog

I am still learning Vb.Net, my experience is in Excel VBA
& VB, so the code could probably be improved. It launches
a second thread that looks out for the dialog, before the
primary thread accesses Outlook

Dermot Balson
Free VBA code for user interfaces, internet connectivity,
encryption
http://www.webace.com.au/~balson/InsaneExcel/Default.html
Last updated August 2003

***** this is the bit that runs the class ****
Dim OK As New oKiller
OK.SearchAndDestroy()
'Your code to access Outlook follows here
'CODE
'it will trigger the dialog, and the killer class should
'deal with it
'when you have finished, you can stop the killer class
like this
OK.Quit
'it is timed to quit after 15 min anyway, because it
gives permission for one minute only, and the dialog may
come up more than once

*********************************************

***** the class follows **********************

'When you access Outlook folders, a permissions dialog
pops up. This class fills in the dialog for the user

Imports System.Threading

Public Class oKiller

#Region "Declarations"
Private Const GW_HWNDNEXT = 2
Private Const GW_CHILD = 5
Private Const WM_LBUTTONDOWN = &H201
Private Const WM_LBUTTONUP = &H202
Private Const SC_RETURN = &H1C
Private Const BM_SETCHECK = &HF1
Private Const BST_CHECKED = &H1&
Private Const KEYEVENTF_KEYUP = &H2

Private Declare Sub keybd_event Lib "user32" (ByVal bVk
As Byte, ByVal bScan _
As Byte, ByVal dwFlags As Integer, ByVal dwExtraInfo
As Integer)
Private Declare Function GetForegroundWindow
Lib "user32" () As Integer
Private Declare Function GetDesktopWindow Lib "user32"
() As Integer
Private Declare Function GetWindow Lib "user32" _
(ByVal hwnd As Integer, ByVal wCmd As Integer) As
Integer
Private Declare Function GetWindowText Lib "user32"
Alias "GetWindowTextA" _
(ByVal hwnd As Integer, ByVal lpString As String,
ByVal cch As Integer) As Integer
Private Declare Function GetClassName Lib "user32"
Alias "GetClassNameA" _
(ByVal hwnd As Integer, ByVal lpClassName As String,
_
ByVal nMaxCount As Integer) As Integer
Private Declare Function SetForegroundWindow
Lib "user32" _
(ByVal hwnd As Integer) As Integer
Private Declare Function SendMessage Lib "user32"
Alias "SendMessageA" (ByVal hwnd As Integer, ByVal wMsg
As Integer, ByVal wParam As Integer, ByVal lParam As
Integer) As Integer
Private Declare Function BringWindowToTop Lib "user32"
(ByVal hwnd As Integer) As Integer
Private Declare Sub Sleep Lib "kernel32" (ByVal
dwMilliseconds As Integer)
Private Declare Function PostMessage Lib "user32"
Alias "PostMessageA" (ByVal hwnd As Integer, ByVal wMsg
As Integer, ByVal wParam As Integer, ByVal lParam As
Integer) As Integer
#End Region

Dim hDialog As Integer
Dim hCheckbox As Integer
Dim hButton As Integer
Dim FoundhWnd As Integer
Dim PrevhWnd As Integer

Dim t As System.Threading.Thread

'this is the public method used to start this class
Public Sub SearchAndDestroy()

'it needs to be on a separate thread because the main
thread is waiting
'for Outlook to respond, and for the same reason it
needs to be launched
'just before the main thread accesses the Outlook
folders
'this method will run the thread for up to 60
seconds, waiting for the
'dialog window to appear
t = New System.Threading.Thread(AddressOf LieInWait)
t.Start()

End Sub

'this method looks for the dialog window every split
second, for 15 minutes, then exits
Private Sub LieInWait()

Dim Counter As Integer
Dim u As Boolean

Do
'search every 0.25 seconds
Thread.CurrentThread.Sleep(250)
u = SearchForWindow()
Counter += 1
Loop While Counter < 3600

End Sub

'this method allows the main thread to terminate this
class when it is not needed
'any more
Public Sub Quit()

t.Abort()

End Sub

'this function does most of the work, waiting for the
dialog to appear
Private Function SearchForWindow() As Boolean

Dim hwnd As Integer
Dim t As Single, r As Integer, t2 As Single
Dim sWindowText As String, sClassname As String

'has the active window changed?
hwnd = GetForegroundWindow
If hwnd <> PrevhWnd Then

'get the window and class names
sWindowText = Space(255)
r = GetWindowText(hwnd, sWindowText, 255)
sWindowText = Left(sWindowText, r)

sClassname = Space(255)
r = GetClassName(hwnd, sClassname, 255)
sClassname = Left(sClassname, r)

'Check that window matches the search parameters
If (sClassname = "#32770") And sWindowText
= "Microsoft Outlook" Then
hDialog = hwnd
'get the dialog details and kill it
KillDialog()
SearchForWindow = True
'try to restore previous app that was in front
SetForegroundWindow(PrevhWnd)
Exit Function
End If
End If

'remember current active window
PrevhWnd = hwnd

End Function

'this method kills the Outlook dialog
'it has the handle of the dialog
Private Sub KillDialog()

'get handles of controls on form
FindWindowLike(hDialog)

'exit if we didn't find any handles
If hCheckbox = 0 Or hButton = 0 Then Exit Sub

'tick checkbox
Call SendMessage(hCheckbox, BM_SETCHECK, BST_CHECKED,
0&)

'press Yes button
PostMessage(hButton, WM_LBUTTONDOWN, 0, 5) ' wParam,
lParam = x,y
Sleep(50)
PostMessage(hButton, WM_LBUTTONUP, 0, 5)
Sleep(50)
'pass an Enter keystroke as well because sometimes
clicking the button is
'not enough to make the dialog disappear
Call keybd_event(13, 0, 0, 0)
Call keybd_event(13, SC_RETURN, KEYEVENTF_KEYUP, 0)

End Sub

'this function finds all the child windows and is used
to get the
'handles of the child controls on the Outlook dialog
Private Function FindWindowLike(ByVal hWndStart As
Integer) As Integer

Dim hwnd As Integer
Dim sWindowText As String
Dim sClassname As String
Dim r As Integer

'Hold the level of recursion and
'hold the number of matching windows

'Initialize if necessary
If hWndStart = 0 Then hWndStart = GetDesktopWindow()

'Get first child window
hwnd = GetWindow(hWndStart, GW_CHILD)

Do Until hwnd = 0

'Search children by recursion
Call FindWindowLike(hwnd)

'Get the window text and class name
sWindowText = Space(255)
r = GetWindowText(hwnd, sWindowText, 255)
sWindowText = Left(sWindowText, r)

sClassname = Space(255)
r = GetClassName(hwnd, sClassname, 255)
sClassname = Left(sClassname, r)

'look for Yes button and access dropdown
Select Case sWindowText
Case "Yes"
hButton = hwnd
Case "&Allow access for"
hCheckbox = hwnd
End Select

'Get next child window
hwnd = GetWindow(hwnd, GW_HWNDNEXT)

Loop

FindWindowLike = 0

End Function

End Class
 
Hi Dermot,

Thanks for the reminder of the link I went there previously and looked at
everything but couldn't find any code. It's clear that Excel is powerful
beyond the obvious, as is your knowledge of it - the Web Server!! for example.
And on the same page is your OutKill2.exe.

Is this just the utility or is it the code too? I downloaded it but WinZip
saw nothing so I guess it's not a zip2exe. I'm thinking that John would like
the source so that he can incorporate its method into his program, and I am
interested in this too, as a generally useful method.

Is the source there but I just didn't see it? If not, would you mind us
having a look at it?

Thanks.

Regards,
Fergus
 
Fergus

sorry about the delay. My site only has the EXE on it, but I did
include the (VB.NEt) source in the message you just replied to ,and
you are free to use it. After all, I put it together from code I'd
borrowed from other people!

regards

dermot
 
Hi Dermot,

ROFL. Small window in OE - Message, Signature, Link - End of perusal.
Missed it!!

Got it now, though - excellent.

Thanks.:-))

Regards,
Fergus
 
Back
Top