MailItem.Display(True)...any way to Force the send of the Mail Ite

N

N11689

Have a VB.NET (2005) application where we bring up the outlook interface
using the mailitem.display(True) method. Having an issue where once the user
'sends' the email (from the display), the message isn't automatically sent.
Most cases when Outlook is not open on their machine. Is there a way to:
1. Force a 'send' or
2. To know if the user actually selected 'send' or canceled out of the
email display?

We are using the Microsoft.Office.Interop.Outlook for Outlook 2003

Thank you.
 
K

Ken Slovak - [MVP - Outlook]

If the user sends the email there should be no need to have code also send
it. It might not trigger a send/receive in Outlook, but the item will be
submitted to the email transport on the user clicking send.

If you want to force a send/receive use the NameSpace.SyncObjects()
collection. Get a reference to SyncObjects(1) and call its Start() method.

You can handle the Send() event on the item that was opened if desired.

It's usually not a good idea to open a mail item or Inspector modally, it
can lead to "ghost" Inspectors that are empty and must be closed by clicking
on the close box, they can't be closed in code.
 
N

N11689

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
 
K

Ken Slovak - [MVP - Outlook]

I told you, don't call Display() with modal being true. I'm not about to
analyze all that code for you.
 
N

N11689

I have changed VB.NET to use MailItem.Display() without the true.
Problem now is that when the outlook interactive displays, it loses focus
and my vb.net program continues processing without waiting for the
user to send/cancel the mail message.

We need to have the ability to display the mail message, but wait until
the user processes that message (either send or cancel) before continuing
on. I've researched the WaitForExit, but that seems to only apply to
files that are executed.

Can you help me to find a way to:
1. Keep the focus on the mailitem.display when it does appear and
2. Prevent my .net program from exiting until the mailitem has been
processed (send/cancelled).

Thank you in advance.
 
K

Ken Slovak - [MVP - Outlook]

If you are handling the Inspector.Close(), item.Close() and item.Send()
events you will know when the user has closed/sent the item. Your code can
then do what it has to do in those event handlers. You can call the
Activate() method on the Inspector to give it focus when you want.

The whole purpose of event driven code is to let the events manage when
specific code and handling is invoked.
 

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