Announcement

Collapse
No announcement yet.

Minimal Outlook Email Option?

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Minimal Outlook Email Option?

    I would like to send an Outlook email programmatically, without ever opening up Outlook, but still have the email show up in the Outlook Sent folder.

    Can that be done?

    I've used code that send text for an email to Outlook, but that opens Outlook and I have to respond with actions. I want to press SEND on an app and not have to do anything else, but still have the sent email to appear in the Outlook Sent folder.

  • #2
    for Officey stuff id just google for vbscript demos, eg...
    Code:
    Sub SendMailOutlook(aTo, Subject, TextBody, aFrom)
      
      'Create an Outlook object
      Dim Outlook 'As New Outlook.Application
      Set Outlook = CreateObject("Outlook.Application")
      
      'Create e new message
      Dim Message 'As Outlook.MailItem
      Set Message = Outlook.CreateItem(olMailItem)
      With Message
        'You can display the message To debug And see state
        '.Display
        
        .Subject = Subject
        .Body = TextBody
        
        'Set destination email address
        .Recipients.Add (aTo)
        
        'Set sender address If specified.
        Const olOriginator = 0
        If Len(aFrom) > 0 Then .Recipients.Add(aFrom).Type = olOriginator
        
        'Send the message
        .Send
      End With
    End Sub
    I'm out of my depths when it comes to converting it to PB COM code though (Jose wake up, it's Monday!), but should be pretty straight-forward? I'd be keen on seeing such a ported demo myself
    -

    Comment


    • #3
      Hey Wayne!
      Do you know if the Outlook object puts the sent email into the Sent Folder of the current PST Outlook file?

      Comment


      • #4
        As it's essentially just an automation of what the human user would do I'd be pretty surprised if it didn't. Give the .vbs a quick run to check (i dont have Outlook to test)
        [edit] although you're pretty specific in regards to a specific Sent Items folder? i'm not sure about folder selection, but a quick google for "vbscript Outlook select "Sent Items" folder" came up with quite a lot of results!

        Also this looks good - full message body search in gbThreads/Poffs for: Outlook.Application
        Last edited by Wayne Diamond; 21 Dec 2014, 09:33 PM.
        -

        Comment


        • #5
          Originally posted by Gary Beene View Post
          I would like to send an Outlook email programmatically, without ever opening up Outlook, but still have the email show up in the Outlook Sent folder.

          Can that be done?
          Can be done, but raises alarms from Outlook. MS has thankfully made Outlook (as of version 2000, IIRC) very picky about automation, as all kind of malware authors were abusing Outlook's automation to send out their creations.

          See http://msdn.microsoft.com/en-us/libr...ice.11%29.aspx for further details.

          Not sure if that's worth the hassle. An alternative suggestion: use some of the many available SMTP examples for sending email and add the (Outlook's) sender's email address as (B)CC so he receives a copy of the email. We use this workflow a lot. Won't put it into the "Sent Items" folder though, but that can be solved with an Outlook rule if really needed.
          Last edited by Knuth Konrad; 22 Dec 2014, 08:59 AM.

          Comment


          • #6
            Originally posted by Gary Beene View Post
            I would like to send an Outlook email programmatically, without ever opening up Outlook, but still have the email show up in the Outlook Sent folder.
            Gary, I am pretty sure you can do this using the MAPI protocol.
            (old) PB code here:
            http://www.powerbasic.com/support/pb...t=ddoc_map.bas
            Look for "ddoc_map.bas".
            You might have to change a few settings in Outlook to 'allow' MAPI to use Outlook.

            Kind regards
            Eddy

            Comment


            • #7
              I KNOW I posted code to do exactly this here, somewhere, using the published Outlook API.
              Michael Mattias
              Tal Systems Inc.
              Racine WI USA
              mmattias@talsystems.com
              http://www.talsystems.com

              Comment


              • #8
                Howdy, MCM,
                ..I posted code to do exactly this here, somewhere,...
                I know the feeling. I've frequently asked a question, only to find that I published something on it myself! Thank goodness for gbThreads and Poffs - the older I get the more I need an external memory booster!

                Comment


                • #9
                  Well, I do recall if you "send" it goes in the "sent" folder. I think my demo did a "show", which basically brings up the Outlook "new email" screen with everything filled in and attachments already attached. Waiting, I guess, for the addition of a "personal touch."

                  I am also sure whatever I posted was NOT posted in the Source Code Forum. It was a "one function" thing named something like "SendEmailUsingOutlook" not a complete application which is why I did not put it in the Source Code Forum.
                  Michael Mattias
                  Tal Systems Inc.
                  Racine WI USA
                  mmattias@talsystems.com
                  http://www.talsystems.com

                  Comment


                  • #10
                    Originally posted by Michael Mattias View Post
                    I KNOW I posted code to do exactly this here, somewhere, using the published Outlook API.
                    This one maybe (post #1)?:
                    http://www.powerbasic.com/support/pb...=email+outlook
                    Eddy

                    Comment


                    • #11
                      I tried Paul Squires class code and it worked for me. Quiet operation (no popups) and the email put in the Outlook Sent folder. I would like to keep looking to see what all my options would be, so I'll follow the link.

                      Comment


                      • #12
                        >This one maybe (post #1)?:

                        Yeah, that's the one. Thank you.

                        I copied the URL into my "posts" library in case it comes up again.
                        Michael Mattias
                        Tal Systems Inc.
                        Racine WI USA
                        mmattias@talsystems.com
                        http://www.talsystems.com

                        Comment


                        • #13
                          Hi Gary,

                          This is the Outlook send email routine that I used in one of my old applications and it seemed to work very well at the time.
                          Code:
                          '=============================================================================
                          ' SendOutlookMail() Function using late-binding and minimal error checking
                          '=============================================================================
                          Function SendOutlookMail( ByVal sEmailTo     As String, _
                                                    ByVal sEmailCC     As String, _
                                                    ByVal sSubject     As String, _
                                                    ByVal sBodyText    As String, _
                                                    ByVal lBodyFormat  As Long,   _
                                                    ByVal lImportance  As Long,   _
                                                    ByVal sAttachment  As String, _
                                                    ByVal lDisplayOnly As Long _
                                                    ) Export As Long
                                                       
                             Local oOutlook   As Dispatch
                             Local oMessage   As Dispatch   
                             Local oRecipient As Dispatch
                             Local oAttach    As Dispatch
                          
                             Local sName As String
                             Local i     As Long
                             
                             Local vVar  As Variant
                             Local vTemp As Variant
                          
                             '===========================
                             ' Create an outlook session
                             '
                             oOutlook = NewCom "Outlook.Application" 
                             If IsFalse IsObject(oOutlook) Then
                                ? "Outlook Object Could not be created", %MB_OK, "Error"
                                Function = %FALSE
                                Exit Function
                             End If
                          
                             
                             '================================
                             ' Create a Message
                             '
                             Let vTemp = %olMailItem
                             Object Call oOutlook.CreateItem(vTemp) To oMessage 
                             If IsNothing(oMessage) Then ? "oMessage failed. err=" & Str$(Err)
                          
                                                   
                             '==================================
                             ' Create Recipient
                             '
                             For i = 1 To ParseCount(sEmailTo,";")
                                sName = Parse$(sEmailTo, ";", i)
                                Let vTemp = sName
                                Object Call oMessage.Recipients.Add(vTemp) To oRecipient 
                                Let vTemp = %olTo
                                Object Let oRecipient.Type = vTemp
                             Next
                             
                          
                             ' Add a CC if specified
                             If Len(RTrim$(sEmailCC)) Then
                                For i = 1 To ParseCount(sEmailCC,";")
                                   sName = Parse$(sEmailCC, ";", i)
                                   Let vTemp = sName
                                   Object Call oMessage.Recipients.Add(vTemp) To oRecipient
                                   Let vTemp = %olCC
                                   Object Let oRecipient.Type = vTemp
                                Next
                             End If
                             
                          
                             ' Add the email Subject
                             Let vTemp = sSubject
                             Object Let oMessage.Subject = vTemp
                          
                             
                             ' Add the email body based on the format
                             Let vTemp = sBodyText
                             Select Case lBodyFormat
                                Case %olFormatUnspecified, %olFormatPlain      
                                   Object Let oMessage.Body = vTemp
                                Case %olFormatHTML       
                                   Object Let oMessage.HTMLBody = vTemp
                                Case %olFormatRichText   
                                   Object Let oMessage.RTFBody = vTemp
                             End Select
                             
                             
                             ' Set the email importance
                             Let vTemp = lImportance
                             Object Let oMessage.Importance = vTemp
                             
                          
                             '======================================================================
                             ' Create and Add Attachment if present
                             '
                             If Len(Trim$(sAttachment)) <> 0 Then
                                If Len(Trim$(Dir$(sAttachment))) <> 0 Then     
                                   Let vTemp = sAttachment
                                   Object Call oMessage.Attachments.Add(vTemp) To vVar
                                   Set oAttach = vVar                                 
                                End If
                             End If
                          
                          
                             '=======================================================================
                             ' Display Message Before Sending
                             '
                             If IsTrue(lDisplayOnly) Then
                                Object Call oMessage.Display
                             Else
                                Object Call oMessage.Save
                                Object Call oMessage.Send
                                '? "Outlook Email has been sent.", %MB_OK, "Success"
                             End If
                          
                             Set oOutlook = Nothing
                             Function = %TRUE
                             
                          End Function
                          Paul Squires
                          FireFly Visual Designer (for PowerBASIC Windows 10+)
                          Version 3 now available.
                          http://www.planetsquires.com

                          Comment


                          • #14
                            Hey Paul!
                            Thanks very much! Is there an INCLUDE file that I will need?

                            It stopped compiling on this ...
                            Code:
                               Let vTemp = %olMailItem
                            Gary

                            Comment


                            • #15
                              I did a check in the roca and winapi includes and didn't see any such equates.

                              Comment


                              • #16
                                Originally posted by Gary Beene View Post
                                I did a check in the roca and winapi includes and didn't see any such equates.
                                Hey Gary,

                                You will need to create the include using com browser.
                                but if that is the only equate missing then..

                                %olMailItem = &H00000000

                                (from Office 12)

                                Merry Christmas.
                                N.

                                Comment


                                • #17
                                  Hi Neil!
                                  Thanks for the info!

                                  I found these equates needing values ...
                                  Code:
                                  %olMailItem = &H00000000
                                  %olTo = &H00000000
                                  %olCC = &H00000000
                                  %olFormatUnspecified = &H00000000
                                  %olFormatPlain = &H00000000
                                  %olFormatHTML = &H00000000
                                  %olFormatRichText = &H00000000
                                  ... by giving them a value I was able to compile to find the whole list.

                                  I'll try out the COM browser. I've not actually used it before, so this will be a good time to give it a try!

                                  Comment


                                  • #18
                                    Hi Neil!
                                    Well, that's like looking for a needle in a haystack! There's a darn lot of registered libraries, none of which struck me as the right name for what I'm looking to do.

                                    I'll see if I can find some references that clarify which library I need to draw on.

                                    Comment


                                    • #19
                                      Howdy, Neil,
                                      I haven't figured out which file to use with the COM browser, but I did find these equate values in a file that Jim Dunn sent me ...
                                      Code:
                                      %olMailItem = &H0
                                      %olTo = &H1
                                      %olCC = &H2
                                      %olFormatUnspecified = &H0
                                      %olFormatPlain = &H1
                                      %olFormatHTML = &H2
                                      %olFormatRichText = &H3

                                      Comment


                                      • #20
                                        Hi Paul!
                                        Re:
                                        ... Outlook send email routine that I used in one of my old applications and it seemed to work very well at the time.
                                        Once I put in the equate values, my first test of the Function you posted was a success!

                                        Thanks again for the post. It's exactly what I was looking for!

                                        And Neil, thank you for your help too. Your comments led me to find the equates values that I needed.

                                        Comment

                                        Working...
                                        X