Outlook : Select Address of a Contact

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • NeoPa
    Recognized Expert Moderator MVP
    • Oct 2006
    • 32633

    Outlook : Select Address of a Contact

    I am using Outlook Automation (from within Access) to create a MailItem object (an email to you and me). The {MailItem}.reci pients.Add() method is often fine for adding an addressee (from the Contacts folder), but I find that I am unable to select the individual SMTP address where the contact has more than one.

    If anyone knows how this can be done then I'd be interested to hear.
  • nico5038
    Recognized Expert Specialist
    • Nov 2006
    • 3080

    #2
    Guess you need to process the items of the Recipients Collection Object, see: http://msdn.microsoft.com/en-us/libr...CHG.10%29.aspx

    Nic;o)

    Comment

    • NeoPa
      Recognized Expert Moderator MVP
      • Oct 2006
      • 32633

      #3
      Thanks Nico.

      I was already looking at the Recipient object of course, but your link pointed me at a resource that explained it better than what I'd found in the Help system. Obviously I'd Googled, but hadn't found what I needed.

      When I have finalised my code (after finalising my understanding of course) I'll post some more info for anyone else to find with similar problems.

      Cheers :)

      PS. The bit I found that I think most likely to give me what I need is http://msdn.microsoft.com/en-us/libr...EXCHG.10).aspx.

      Comment

      • mshmyob
        Recognized Expert Contributor
        • Jan 2008
        • 903

        #4
        Hey Neo. I am working on a new project and will need to implement an email portion. When you are done can you send me some stuff you did maybe I can use some of it. It will save me a lot of time. I'm not quite ready for that aspect but should be in a couple weeks.


        cheers,

        Comment

        • NeoPa
          Recognized Expert Moderator MVP
          • Oct 2006
          • 32633

          #5
          Just let me know nearer the time. I will have made more progress then.
          You can also let me know (then) exactly what you're looking for.

          PS. I should warn you that this code does fall foul of fairly annoying Outlook warnings that non-Outlook software is trying to (1) access addresses and (2) send email on your behalf.

          This is something I intend to progress with, but to date I've hit problems.

          Comment

          • nico5038
            Recognized Expert Specialist
            • Nov 2006
            • 3080

            #6
            Instead of Outlook you might try the CDO object for emailing.
            Check: http://www.paulsadowski.com/WSH/cdo.htm

            Nic;o)

            Comment

            • NeoPa
              Recognized Expert Moderator MVP
              • Oct 2006
              • 32633

              #7
              That looks very interesting Nico - Thanks.

              I am most of the way there using the standard Outlook object model, but this one may prove a better solution for me. I will certainly look into it and post back my comments. That may be a bit delayed though, as I am now working on another aspect of this particular project where I have to dig up the correct data that later will be e-mailed or FTPed across to the customer.

              I know nothing about CDO at this time, so I will need to read up some. I do know that the firewalls we have here will disallow any SMTP traffic from anywhere but a few well restricted servers (and all the Exchange servers are set not to allow SMTP relaying). Had it not been for that I think I would have been happiest simply knocking an SMTP solution together.

              All this because a bunch of personality-less morons decided to use the internet to spread their detritus. Anyway, all good fun.

              Comment

              • NeoPa
                Recognized Expert Moderator MVP
                • Oct 2006
                • 32633

                #8
                PS. From my initial research it seems that CDO does use the SMTP protocol exclusively, which puts it out of scope for me I'm afraid.

                I will update the thread with any progress I make using Outlook automation.

                I will include here a link that was passed to me that may prove promising (Avoiding Excessive Security Warnings when Sending Automated E-mail Messages). This deals with problems I have already encountered using this method. Hopefully this link will enable me to get around them.

                Comment

                • nico5038
                  Recognized Expert Specialist
                  • Nov 2006
                  • 3080

                  #9
                  Good link NeoPa, shows that when SMTP is allowed the CDO is the easy way to "bypass" the Outlook Object model build-in warnings. Guess the "morons" found that too <LOL>

                  Nic;o)

                  Comment

                  • NeoPa
                    Recognized Expert Moderator MVP
                    • Oct 2006
                    • 32633

                    #10
                    I'm afraid so Nico. There was a flood of virus payloads which included a basic SMTP engine that ran on the PC itself :(

                    I first found that out when an Exchange server I was responsible for was flagged as an open relay (it would allow SMTP messages to be sent on when it determined the address was not local). I had to fix it and then apply to be rechecked before I could get it removed from the blacklist (Various servers were refusing to accept any mail from us).

                    It seems there were two methods used by the virus software :
                    1. SMTP via the local mail server. This gives the e-mails the impression of being more kosher.
                    2. SMTP straight out to the web.

                    To block the first I needed to tighten up the security on my Exchange server. To block the second I needed to block SMTP traffic on the firewall from all internal addresses except my Exchange server.

                    As you will see, this doesn't leave much scope for a general purpose SMTP based routine in my code :(

                    Comment

                    • mshmyob
                      Recognized Expert Contributor
                      • Jan 2008
                      • 903

                      #11
                      Hello Neo,

                      I am now ready to look at the e-mail part of my app. Would you mind sending me anything you have got working (in particular how you got around the security messages). I will even trade if need be. I just created a very nice Calendar that allows you to add events such as meetings, to-do, etc., multiple participants, and such. I could trade you that if a calendar/scheduler is of any use to you.

                      You can PM me for email address.

                      cheers,
                      Rob

                      Comment

                      • NeoPa
                        Recognized Expert Moderator MVP
                        • Oct 2006
                        • 32633

                        #12
                        Rob,

                        My e-mail project has been delayed further.

                        I am now using FTP to transfer this current set of files to the customer, although I would like to include e-mailing in my repertoire once this is finished. I hope to reduce the manual workload considerably by enabling this.

                        I haven't made much further progress than my last post on this though I'm afraid. I have the ability to code the sending of the mail, but the security is something I've only found links for.

                        I suggest, unless you have similar restrictions yourself with respect to SMTP, that you check out what CDO can do for you (Re Nico's post). If not, check out my earlier link and see if you can make some progress with that.

                        PS. Thanks for the offer of your code. I'll bear it in mind if ever I need something along those lines. I'm also more than happy to share anything I manage to come up with if it can be of any use to you. Unfortunately, that's not likely to start for some weeks now, and even that's assuming other priority work is not inserted ahead of it (I hope not though as this is something I really want to get cracked).

                        Comment

                        • NeoPa
                          Recognized Expert Moderator MVP
                          • Oct 2006
                          • 32633

                          #13
                          This module (only half-finished I'm afraid) is as far as I've got so far. Nothing's finished yet (specifically some comments not implemented yet) but SendEM can send an email - albeit with all the security popups getting in the way :(
                          Code:
                          Option Compare Database
                          Option Explicit
                          
                          Private appOutlook As Outlook.Application
                          
                          'SendEM Sends an email
                          'For each address, any text found in <> will be treated as an SMTP address.
                          'An attempt is first made to refer to an object with a matching name.
                          'If this succeeds, but the address is ambiguous, the SMTP address will be used
                          ' to resolve it.
                          'If no matching item is found then it will create a simple SMTP: entry
                          ' of the following format : "Display Name<SMTP Address>".
                          Public Function SendEM(Optional ByVal strTo As String, _
                                                 Optional ByVal strCC As String, _
                                                 Optional ByVal strBCC As String, _
                                                 Optional ByVal strSubject As String, _
                                                 Optional ByVal strBody As String, _
                                                 Optional ByVal strAttachments As String) As Boolean
                              Dim blnOpen As Boolean
                              Dim strItem As String
                              Dim miMail As MailItem
                          
                              SendEM = False
                              blnOpen = Not (appOutlook Is Nothing)
                              If Not blnOpen Then
                                  On Error Resume Next
                                  Set appOutlook = GetObject(Class:="Outlook.Application")
                                  On Error GoTo 0
                                  blnOpen = Not (appOutlook Is Nothing)
                                  If Not blnOpen Then _
                                      Set appOutlook = CreateObject(Class:="Outlook.Application")
                              End If
                              With appOutlook
                                  Set miMail = .CreateItem(ItemType:=olMailItem)
                                  With miMail
                                      Call .Display
                                      Call AddRecipient(miMail, strTo, olTo)
                                      Call AddRecipient(miMail, strCC, olCC)
                                      Call AddRecipient(miMail, strBCC, olBCC)
                                      .Subject = strSubject
                                      .Body = strBody & .Body
                                      Call .Send
                                  End With
                                  If Not blnOpen Then Set appOutlook = Nothing
                              End With
                          End Function
                          
                          Private Function AddRecipient(ByRef miMail As MailItem, _
                                                        strList As String, _
                                                        lngType As Long) As Boolean
                              Dim varItem As Variant
                          
                              With miMail.Recipients
                                  For Each varItem In Split(strList, ";")
                                      With .Add(varItem)
                                          .Type = lngType
                                          AddRecipient = Not .Resolve
                                          If AddRecipient Then Exit Function
                                      End With
                                  Next varItem
                              End With
                          End Function
                          
                          'Some sample code from Microsoft
                          ' function to attempt ambiguous name resolution (ANR)
                          Private Function TryANR(objRecip As Recipient) As Boolean
                              Dim objAmbigEntries As AddressEntries ' possible resolutions
                              Dim strChosenID As String ' ID of address entry chosen by user
                          
                              ' ... set up error handling ...
                              Set objAmbigEntries = objRecip.AmbiguousNames
                              If objAmbigEntries Is Nothing Then
                                  MsgBox "No eligible names for resolution"
                                  objRecip.Delete ' nothing else can be done at this point
                                  TryANR = False
                              Else
                                  ' show address entries to user so one can be chosen, and save its
                                  ' entry identifier: strChosenID = objAddrEntry.ID
                                  objRecip.ID = strChosenID
                                  TryANR = True
                              End If
                          End Function
                          
                          Public Function SendItem(Optional ByVal strTo As String, _
                                                   Optional ByVal strCC As String, _
                                                   Optional ByVal strBCC As String, _
                                                   Optional ByVal strSubject As String, _
                                                   Optional ByVal strBody As String, _
                                                   Optional ByVal strAttachments As String) As Boolean
                              
                          End Function

                          Comment

                          • mshmyob
                            Recognized Expert Contributor
                            • Jan 2008
                            • 903

                            #14
                            Thanks. I just started to look into the email problems and it looks like Extended MAPI might be the way to go instead of standard MAPI. Extended MAPI eliminates the problems of popups. If I get the Extended MAPI to work I will let you know.

                            cheers,

                            Comment

                            • NeoPa
                              Recognized Expert Moderator MVP
                              • Oct 2006
                              • 32633

                              #15
                              Cheers Rob.

                              If that proves true (& I may get to start looking again in mid August sometime if things go according to plan) then I too will be very interested.

                              If I get anywhere I'll keep you posted :)

                              Comment

                              Working...