This is a post I put out on an msdn forum today. I have since inserted the
code to sync objects (as you can see), and it appears to be working (where
when I open Outlook, the message is no longer in the Outbox and appears in my
inbox shortly after login).
I am stll having the problem where OUTLOOK.EXE still appears as a process in
task manager. I do not know how to stop this process. Can you check to see
if there is something in my code that is preventing this process from
stopping? It only happens when mailitem.display(true) method is called.
When we are doing our 'autosend', it closes the process correctly. Thanks
very much.
I am writing a VB.NET (2005) exe using Microsoft.Office.Interop.Outlook 2003
that is called by our main software app (not writte in VB.NET). Based on
parameters passed in, the end user can either autosend the email or have the
outlook interactive pop up.
The problem I am having is when using the MailItem.Display() method. There
are two issues:
1. If Outlook is closed, once the user selects Send on the Outlook
interactive (called form the Display method), the mail item stays in the
outbox until the use logs into outlook.
2. The OUTLOOK.EXE program/process remains in Task Manager after Display()
method (even if I open/close Outlook itself).
If we just do the straight objMailItem.Send(), all seems to work okay.
It's when we do the objMailItem.Display(True), that we have the problem.
Would like to know:
1. How to capture whether or not the user actually selected Send or
Cancelled from the .Display(True) interactive
2. How to force a send of the objMailItem.Display(True) if Outlook is
closed and the user selected Send (so as not
to have to open Outlook to force the email out of the outbox)
3. How to close Outlook.exe after the email is sent.
My code that does the send is below (again, written in VB.NET 2005).
I have tried doing some research, but have not been able to find a solution.
Any help would be greatly appreciated. Thank you.
Sub SendTheEmail()
Dim intCounter As Integer
Dim intRecipientCount As Integer
Dim strToList As String = ""
Dim strCcList As String = ""
Dim strBccList As String = ""
Dim objOutlookApplication As Object
Dim objMapiNameSpace As Object
Dim objMailItem As Object
Dim objOutlookSyncs As Object
Dim objOutlookSync As Object
Dim i As Integer
Try
'Create Outlook application. (late binding)
objOutlookApplication = CreateObject("Outlook.Application")
'Get Mapi NameSpace.
objMapiNameSpace = objOutlookApplication.getnamespace("mapi")
Try 'Logon to Outlook using default outlook profile
objMapiNameSpace.Logon("", "", True, True)
' 05/04/09 - See this link for details on default profile
logon:
'
http://www.c-sharpcorner.com/Upload...2005000124AM/SendingEmailsThroughOutlook.aspx
Catch ex As Exception
strErrorMessage = "Email will not be sent. " & _
"Error setting Default Outlook Profile. "
& _
"Error Message: " & ex.Message & _
" Contact your system administrator."
Exit Sub
End Try
'Create a mail item object.
objMailItem =
objOutlookApplication.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem)
'Set the email subject.
objMailItem.Subject = strEmailSubject
'Set the email body (text of message).
objMailItem.Body = strEmailBody
'Build the attachment list for the mail item from the retrieved
attachments
For intCounter = 0 To
objDBAccess.SPResultSetProp.Tables(con_RS_S_SY_EMAIL_ATTACHMENTS_WK).Rows.Count - 1
objMailItem.Attachments.Add(objDBAccess.SPResultSetProp.Tables(con_RS_S_SY_EMAIL_ATTACHMENTS_WK).Rows.Item(intCounter).Item(1))
Next
'09/08/08 - We are using this variable intRecipientCount rather
rather than the objmailitem.recipient.count
' property to prevent the security question 'Someone
is trying to access your outlook...' from
' popping up. We are setting this count in the For
loop below when the type of addres is NOT an
' 'X' (for external)
intRecipientCount = 0
'Build the distribution lists for the mail item from the
retrieved distribution.
'Do distribution last so annoying Outlook security messages come
one after another.
For intCounter = 0 To
objDBAccess.SPResultSetProp.Tables(con_RS_S_SY_EMAIL_DISTRIBUTION_WK).Rows.Count - 1
Select Case
objDBAccess.SPResultSetProp.Tables(con_RS_S_SY_EMAIL_DISTRIBUTION_WK).Rows.Item(intCounter).Item(2)
Case "T" 'Build To List
intRecipientCount = intRecipientCount + 1
If strToList = "" Then
strToList = _
objDBAccess.SPResultSetProp.Tables(con_RS_S_SY_EMAIL_DISTRIBUTION_WK).Rows.Item(intCounter).Item(1)
Else
strToList = _
strToList & ";" & _
objDBAccess.SPResultSetProp.Tables(con_RS_S_SY_EMAIL_DISTRIBUTION_WK).Rows.Item(intCounter).Item(1)
End If
Case "C" 'Build CC List
intRecipientCount = intRecipientCount + 1
If strCcList = "" Then
strCcList = _
objDBAccess.SPResultSetProp.Tables(con_RS_S_SY_EMAIL_DISTRIBUTION_WK).Rows.Item(intCounter).Item(1)
Else
strCcList = _
strCcList & ";" & _
objDBAccess.SPResultSetProp.Tables(con_RS_S_SY_EMAIL_DISTRIBUTION_WK).Rows.Item(intCounter).Item(1)
End If
Case "B" 'Build BCC List
intRecipientCount = intRecipientCount + 1
If strBccList = "" Then
strBccList = _
objDBAccess.SPResultSetProp.Tables(con_RS_S_SY_EMAIL_DISTRIBUTION_WK).Rows.Item(intCounter).Item(1)
Else
strBccList = _
strBccList & ";" & _
objDBAccess.SPResultSetProp.Tables(con_RS_S_SY_EMAIL_DISTRIBUTION_WK).Rows.Item(intCounter).Item(1)
End If
End Select
Next
'If AutoSend, do not display the message, just put it in Outbox.
If intRecipientCount > 0 Then
'Only set the list if there is data to be inserted in it.
If strToList <> "" Then
Try
objMailItem.To = strToList
Catch ex As Exception
blnErrorOccurred = True
strErrorMessage = "Error occurred inserting into the
To list." & vbCrLf & "Email was not sent."
End Try
End If
If strCcList <> "" Then
Try
objMailItem.CC = strCcList
Catch ex As Exception
blnErrorOccurred = True
strErrorMessage = "Error occurred inserting into the
Cc list." & vbCrLf & "Email was not sent."
End Try
End If
If strBccList <> "" Then
Try
objMailItem.BCC = strBccList
Catch ex As Exception
blnErrorOccurred = True
strErrorMessage = "Error occurred inserting into the
Bcc list." & vbCrLf & "Email was not sent."
End Try
End If
End If
If intRecipientCount > 0 And strAutoSend = "Y" Then
'Send automatically
Try
objMailItem.Send()
Catch ex As Exception
blnErrorOccurred = True
strErrorMessage = vbCrLf & "User did not allow automatic
send of" & vbCrLf & "e-mail or an error" & vbCrLf & "occurred within Outlook
itself." _
& vbCrLf & vbCrLf & "The email was not
sent."
End Try
Else
If strAutoSend = "N" Then
'Display the send dialog and have the user send.
'Pass in True for modal to make that screen pop up.
objMailItem.Display(True)
End If
'Is there a way to know the user cancelled the send?
End If
'New Code
objOutlookSyncs = objMapiNameSpace.SyncObjects
For i = 1 To objOutlookSyncs.Count
objOutlookSync = objOutlookSyncs.Item(i)
objOutlookSync.start()
Next
'Log off
objMapiNameSpace.Logoff()
'Clean Up
objMailItem = Nothing
objMapiNameSpace = Nothing
objOutlookApplication = Nothing
Catch ex As Exception
strErrorMessage = "Error Message: " & ex.Message
End Try
End Sub