Solved

Getting email messages into a SQL Table

Posted on 2003-10-27
6
390 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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

Suggested Solutions

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

737 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