locked
Outlook Opening Multiple Message Windows RRS feed

  • Question

  • User-498848506 posted

    The code below works rather well and enables me to open an Outlook email message and to transfer To, Subject and Msg from my VB.net application.
    The problem is that it opens two e-mail messages? Is there a reason it does that and or a way to only open only one message window?

    Thank you for any assistance!

    Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    '******************** Add E-Mail Function FROM Here ******************
    Dim SendToEmail As String
    SendToEmail = Request.QueryString.Item("Email")

    Dim Subject As String
    Dim Recipient As String
    Dim Message As String
    Subject = "Testing Request Confirmation"
    Recipient = SendToEmail & ""
    'Recipient = "'" & SendToEmail & "'"
    Message = "Thanks for providig your contact information"
    SendOutlookMail(Subject, Recipient, Message)
    End Sub

    Public Sub SendOutlookMail(Subject As String, Recipient As _
    String, Message As String)

    On Error GoTo errorHandler
    Dim oLapp As Object
    oLapp = CreateObject("Outlook.Application")
    'Outlook.Application()
    Dim oItem As Object

    'oLapp = CreateObject("Outlook.application")
    oItem = oLapp.CreateItem(0)

    With oItem
    .Subject = Subject
    .To = Recipient
    .body = Message
    .Display()
    '.Send()
    End With

    oLapp = Nothing
    oItem = Nothing


    Exit Sub

    errorHandler:
    oLapp = Nothing
    oItem = Nothing
    Exit Sub


    End Sub

    Tuesday, March 31, 2020 7:11 PM

All replies

  • User288213138 posted

    Hi SmokinJoe,

    The problem is that it opens two e-mail messages? Is there a reason it does that and or a way to only open only one message window?

    Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    '******************** Add E-Mail Function FROM Here ******************
    Dim SendToEmail As String
    SendToEmail = Request.QueryString.Item("Email")

    Dim Subject As String
    Dim Recipient As String
    Dim Message As String
    Subject = "Testing Request Confirmation"
    Recipient = SendToEmail & ""
    'Recipient = "'" & SendToEmail & "'"
    Message = "Thanks for providig your contact information"
    SendOutlookMail(Subject, Recipient, Message)
    End Sub

    Public Sub SendOutlookMail(Subject As String, Recipient As _
    String, Message As String)

    On Error GoTo errorHandler
    Dim oLapp As Object
    oLapp = CreateObject("Outlook.Application")
    'Outlook.Application()
    Dim oItem As Object

    'oLapp = CreateObject("Outlook.application")
    oItem = oLapp.CreateItem(0)

    With oItem
    .Subject = Subject
    .To = Recipient
    .body = Message
    .Display()
    '.Send()
    End With

    oLapp = Nothing
    oItem = Nothing


    Exit Sub

    errorHandler:
    oLapp = Nothing
    oItem = Nothing
    Exit Sub


    End Sub

    According to the code you provided, I cannot reproduce your problem. Can you provide more detailed code?

    Best regards,

    Sam

    Wednesday, April 1, 2020 6:28 AM
  • User753101303 posted

    Hi,

    Keep also in mind that it runs on the web server. Using an end user application as a server side programming library is discouraged.

    Instead you could use a "true" programming library such as  https://docs.microsoft.com/en-us/dotnet/api/system.net.mail.smtpclient?view=netframework-4.8 or  its cross platform replacement : https://github.com/jstedfast/MailKit

     

    Wednesday, April 1, 2020 7:32 AM
  • User-498848506 posted

    Hi Sam,

    Thank you for responding....

    That is the only code that performs the function. I don't know why it opens two windows? Could it possibly be additionally triggering Outlook to open, in addition to what the code requests... I mean Outlook is loaded on my computer... why are two windows opening... Strange  

    - Joe

    Wednesday, April 1, 2020 4:27 PM
  • User475983607 posted

    SmokinJoe

    Hi Sam,

    Thank you for responding....

    That is the only code that performs the function. I don't know why it opens two windows? Could it possibly be additionally triggering Outlook to open, in addition to what the code requests... I mean Outlook is loaded on my computer... why are two windows opening... Strange  

    - Joe

    As far as I can tell the code is functioning as expected.  One Window is the Outlook application and the other(s) are any Outlook items created.  Outlook interop is like remote controlling Outlook with code.  When you send an email in Outlook you have to first open Outlook then create a new email message.

    If you are getting two instances of Outlook then you probably are calling the Sub twice which is an error elsewhere in the code. 

    Wednesday, April 1, 2020 5:36 PM
  • User288213138 posted

    Hi,

    That is the only code that performs the function. I don't know why it opens two windows? Could it possibly be additionally triggering Outlook to open, in addition to what the code requests... I mean Outlook is loaded on my computer... why are two windows opening... Strange 

    I tested your code and it only opened one windows here.

    The result:

    Please check your code if the SendOutlookMail () method is called multiple times.

    Best regards,

    Sam

    Thursday, April 2, 2020 1:53 AM
  • User-498848506 posted

    Hi Sam...

    Thank you for your continued assistance....

    I went over the simple code over and over it's only is called once... here it is again... perhaps there is some setting in OUTLOOK that is triggering it to open multiple windows.

    - Joe

    Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    '******************** Add E-Mail Function FROM Here ******************
    ' Private Sub EmailTeam_Click(sender As Object, e As EventArgs) Handles EmailTeam.Click

    Dim SendToEmail As String
    SendToEmail = Request.QueryString.Item("Email")

    Dim Subject As String
    Dim Recipient As String
    Dim Message As String
    Subject = "Request Confirmation"
    Recipient = SendToEmail & ""
    Message = "This is your Request Confirmation"
    SendOutlookMail(Subject, Recipient, Message)

    End Sub
    Public Sub SendOutlookMail(Subject As String, Recipient As _
    String, Message As String)

    On Error GoTo errorHandler
    Dim oLapp As Object
    oLapp = CreateObject("Outlook.Application")
    'Outlook.Application()
    Dim oItem As Object

    'oLapp = CreateObject("Outlook.application")
    oItem = oLapp.CreateItem(0)

    With oItem
    .Subject = Subject
    .To = Recipient
    .body = Message
    .Display()
    '.Send()
    End With

    oLapp = Nothing
    oItem = Nothing


    Exit Sub

    errorHandler:
    oLapp = Nothing
    oItem = Nothing
    Exit Sub

    End Sub

    Thursday, April 2, 2020 8:23 PM
  • User753101303 posted

    "went over" that is ? Dou you mean you just read the code or that you used https://docs.microsoft.com/en-us/visualstudio/debugger/using-breakpoints?view=vs-2019 to see if this event happens twice ?

    If I remember it can happen when using both the Handles clause as well as an OnClick event inside the markup.

    Just in case note again that a user won't see any window as Outlook will open on the web server (and needs to be installed). Once again using Outlook at a programming library is IMO a bad idea.

    Thursday, April 2, 2020 9:21 PM
  • User-498848506 posted

    Thank you PatriceSc for responding,
    With the inspiration of your response I "reviewed" the code, not by debugging.  It appeared that the button handling Sub below, had a 'Handles' component.  When I commented that portion out... Only a single Outlook Window opens. I do not know why that is... but it now works.

    Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)  'Handles Button1.Click

    Thank you for your insight!

    Thursday, April 2, 2020 11:15 PM