Solved

Getting email messages into a SQL Table

Posted on 2003-10-27
6
383 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
  • 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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Join & Write a Comment

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how the fundamental information of how to create a table.

758 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now