</pre>
<p align=center><big><strong>howto:send a mail, subject=db_name, body=table_name, get query result as email</strong></big></p>
<p align=center><big><strong>关键词:</strong></big>Sql Server, ASP, Exchange</p>
<p><span class=smallFont>
no error handle, no optimize, just demo test...<br>
paste this to your exchange event script<br>
=========================================================<br>
&lt;SCRIPT RunAt=Server Language=VBScript&gt;<br>
<br>
<br>
Option Explicit <br>
<br>
<br>
' Event Handlers<br>
'---------------------------------------------------------------------<br>
' DESCRIPTION: This event is fired when a new message is added to the folder<br>
<br>
Public Sub Folder_OnMessageCreated<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;On Error Resume Next<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;Dim AMSession<br>
&nbsp;&nbsp;&nbsp;&nbsp;Dim fldrOutbox<br>
&nbsp;&nbsp;&nbsp;&nbsp;Dim msgTarget<br>
&nbsp;&nbsp;&nbsp;&nbsp;Dim fldrTarget<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;Dim oStores<br>
&nbsp;&nbsp;&nbsp;&nbsp;Dim Temp<br>
&nbsp;&nbsp;&nbsp;&nbsp;Dim idTargetFolder<br>
&nbsp;&nbsp;&nbsp;&nbsp;Dim idTargetMessage<br>
&nbsp;&nbsp;&nbsp;&nbsp;Dim msgResponse<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;idTargetFolder = EventDetails.FolderID<br>
&nbsp;&nbsp;&nbsp;&nbsp;idTargetMessage = EventDetails.MessageID<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;Set AMSession = EventDetails.Session<br>
&nbsp;&nbsp;&nbsp;&nbsp;Set fldrOutbox = AMSession.Outbox<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;Set fldrTarget = AMSession.GetFolder( idTargetFolder, Null )<br>
&nbsp;&nbsp;&nbsp;&nbsp;Set msgTarget = AMSession.GetMessage( idTargetMessage, Null )<br>
' =========================================================================<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;Dim oConn <br>
&nbsp;&nbsp;&nbsp;&nbsp;Dim oRs <br>
&nbsp;&nbsp;&nbsp;&nbsp;Dim Index <br>
&nbsp;&nbsp;&nbsp;&nbsp;dim strDBResult<br>
&nbsp;&nbsp;&nbsp;&nbsp;dim dbcnstr<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>
&nbsp;&nbsp;&nbsp;&nbsp;Set oConn = CreateObject(&quot;ADODB.Connection&quot
<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dbcnstr = &quot;driver={SQL Server};server=sqlserver;database=&quot; &amp; msgTarget.subject &amp; &quot;;uid=sa;pwd=&quot;<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;oConn.Open dbcnstr<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;Set oRs = oConn.Execute(&quot;SELECT * From &quot; &amp; msgTarget.text)<br>
&nbsp;&nbsp;&nbsp;&nbsp;<br>
&nbsp;&nbsp;&nbsp;&nbsp;Do while (Not oRs.eof)<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;For Index=0 to (oRs.fields.count-1)<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;strDBResult = strDBResult &amp; &quot;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;&quot; &amp; chr(9) &amp; oRs(Index) <br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Next<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;strDBResult = strDBResult &amp; chr(13)<br>
&nbsp;&nbsp;&nbsp;&nbsp;oRs.MoveNext <br>
&nbsp;&nbsp;&nbsp;&nbsp;Loop <br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;oRs.close<br>
&nbsp;&nbsp;&nbsp;&nbsp;oConn.close <br>
<br>
' =========================================================================&nbsp;&nbsp;&nbsp;&nbsp;<br>
&nbsp;&nbsp;&nbsp;&nbsp;Set msgResponse = fldrOutbox.Messages.Add( &quot;Query Result for database:&quot; &amp; msgTarget.subject &amp; &quot;;table:&quot; &amp; msgTarget.text, strDBResult)<br>
&nbsp;&nbsp;&nbsp;&nbsp;msgResponse.Recipients.Add &quot;&quot;, &quot;&quot;, 1, msgTarget.Sender.ID<br>
&nbsp;&nbsp;&nbsp;&nbsp;msgResponse.Recipients.Resolve(False)<br>
&nbsp;&nbsp;&nbsp;&nbsp;msgResponse.Send<br>
&nbsp;&nbsp;&nbsp;&nbsp;<br>
End Sub<br>
<br>
' DESCRIPTION: This event is fired when a message in the folder is changed<br>
Public Sub Message_OnChange<br>
&nbsp;&nbsp;&nbsp;&nbsp;On Error Resume Next<br>
<br>
End Sub<br>
<br>
' DESCRIPTION: This event is fired when a message is deleted from the folder<br>
Public Sub Folder_OnMessageDeleted<br>
&nbsp;&nbsp;&nbsp;&nbsp;On Error Resume Next<br>
<br>
End Sub<br>
<br>
' DESCRIPTION: This event is fired when the timer on the folder expires<br>
Public Sub Folder_OnTimer<br>
&nbsp;&nbsp;&nbsp;&nbsp;On Error Resume Next<br>
<br>
End Sub<br>
<br>
&lt;/SCRIPT&gt;&nbsp;