Solved

Getting email messages into a SQL Table

Posted on 2003-10-27
6
391 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 250 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
What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

 
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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

724 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