Tech or Treat! Write an article about your scariest tech disaster to win gadgets!Learn more

x
?
Solved

Getting email messages into a SQL Table

Posted on 2003-10-27
6
Medium Priority
?
396 Views
Last Modified: 2012-08-13
I need to find a fairly easy way to get SQL to retrieve email messages into a table. I have an exchange email account, and when someone sends a message to the address, I need the FromName, FromAddress, Subject, Body, etc to be stored in a table in SQL.

Is SQL Mail the way to do this. If so, how? Can I just periodically export out of Outlook and have a stored procedure append the exported entries to the table?
0
Comment
Question by:lab837
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 3

Accepted Solution

by:
galori earned 750 total points
ID: 9630559
I would use VBScript:

Sub GetMessages()
      Dim objMAPISession
      Set objMAPISession = CreateObject("MAPI.session")
      Dim objInbox,objMessages,objMessage,strText
'                     objSession.Logon( [profileName] [, profilePassword] [, showDialog] [, newSession] [, parentWindow] [, NoMail] [, ProfileInfo]              )
      Call objMAPISession.Logon(                ,                   , False        , True         ,                , True     , "zaxxon" & VbLf & "tasrun")
      Set objInbox = objMAPISession.Inbox
      If objInbox is Nothing Then
            Err.Raise 1,"","failed to open inbox"
      End If
      Set objMessages = objInbox.Messages
      If objMessages is Nothing Then      
            Err.Raise 1,"","failed to open inbox messages object"
      End If
      For Each objMessage in objMessages
            strText=objMessage.body  '** not sure about this method, look it up. The object is a MAPI Message object. It might be "message" or "text"
' code here to insert into SQL
      Next
End Sub
0
 
LVL 30

Expert Comment

by:nmcdermaid
ID: 9632598
yep you can do using SQL Mail, look up xp_sendmail and go from there. You need to configure SQL Server to use the local mail profile etc. Just make sure you have SP3 for SQL Server 2000
0
 

Author Comment

by:lab837
ID: 9636524
Can I insert that VBScript code into a stored procedure in SQL?
0
Survive A High-Traffic Event with Percona

Your application or website rely on your database to deliver information about products and services to your customers. You can’t afford to have your database lose performance, lose availability or become unresponsive – even for just a few minutes.

 
LVL 30

Expert Comment

by:nmcdermaid
ID: 9640279
Either use the VBScript method described by galori to copy data from the inbox into your table. This involves putting that script into an ActiveX task in DTS or into a VBS file and running it.

OR

Use xp_findnextmsg, xp_readmail, xp_deletemail to access your inbox directly from SQL

You decide. They'll both work though I am biased towards my own solution :)


PS If you really want to you can try using xp_cmdshell to run a VBScript, but I think you may be missing the point.
0
 

Author Comment

by:lab837
ID: 9653313
I'm having trouble accessing the fields (body, sender, subject). Referring to objMessage.body does not work (Object does not support this property of method). In fact, objMessage is a string storing the subject. How do I reference the field other than subject? Here's my code:
 
***********************************
Private Sub Application_NewMail()
     Dim objMAPISession
     Set objMAPISession = CreateObject("MAPI.session")
     Dim objInbox, objMessages, objMessage, strText
     Dim cmd
     Set cmd = CreateObject("ADODB.Command")
 
     objMAPISession.Logon profileName:="Outlook"
 
     Set objInbox = objMAPISession.Inbox
     Set objMessages = objInbox.Messages
     For Each objMessage In objMessages
     MsgBox (objMessage)
        With cmd
            .ActiveConnection = "ODBC;DRIVER=SQL Server;SERVER=XXX;UID=XXX;PWD=XXX;DSN=XXX"
            .CommandType = 1
            .CommandText = "INSERT INTO tblCOBIS (Sender, Subject, Description) VALUES ('" & objMessage.FromAddress & "', '" & objMessage.Subject & "', '" & objMessage.Body & "');"
            .Execute
        End With
    Next
        Set cmd = Nothing
End Sub
 
*******************************
The problem lies in the SQL statement line, because you can't have "objMessage.FromAddress", etc. Any ideas?
0
 
LVL 30

Expert Comment

by:nmcdermaid
ID: 9656377
You can browse this Message objects properties if you open up VB and set a reference to the MAPI object, then press F2. Select the MAPI object in the drop down and it will listall of the object properties/methods.

Then when you have found the property you want, you can use it in your VBScript.

Or if you decide to use T-SQL, the xp_readmail stored procedure will return that info for you.
0

Featured Post

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

647 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question