?
Solved

Getting email messages into a SQL Table

Posted on 2003-10-27
6
Medium Priority
?
393 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
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

 
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

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

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

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Suggested Courses

801 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