如果SQL查询结果为空,则发送电子邮件

nyoman89

该脚本可以正确运行以发送电子邮件SQL查询结果,但是如果SQL查询结果为空,则需要脚本来停止发送电子邮件。

'Declare Constants

Const CDO_SCHEMA = "http://schemas.microsoft.com/cdo/configuration/"
Const CDO_MAIL_HEADER = "urn:schemas:mailheader:"

'Method used to send mail
Const CDO_SEND_USING_REMOTE = 2 'Send using Remote SMTP Server

'Security method used on remote SMTP server
Const CDO_ANONYMOUS = 0 'Use no authentication
Const CDO_BASIC = 1 'Use the basic (clear text) authentication
Const CDO_NTLM = 2 'Use the NTLM authentication

'Delivery Status Notifications
Const cdoDSNDefault = 0 'No DSN commands are issued
Const cdoDSNNever = 1 'No DSN commands are issued
Const cdoDSNFailure = 2 'Return a DSN if delivery fails
Const cdoDSNSuccess = 4 'Return a DSN if delivery succeeds
Const cdoDSNDelay = 8 'Return a DSN if delivery is delayed
Const cdoDSNSuccessFailOrDelay = 14 'Return a DSN if delivery succeeds, fails, or is delayed

'Set method of sending
strSendMethod = CDO_SEND_USING_REMOTE

'Remote SMTP Server Settings
strSmtpServer = "smtp.abc.com" 'Name or IP of SMTP Server
intSmtpPort = 25 'SMTP Server Port; typically 25
intSmtpTimeout = 60 'Number of seconds to try establishing a connection to the SMTP Server
strAuthenticationMethod = CDO_ANONYMOUS

'SMTP Server Authentication - IF BASIC or NTLM; NOT needed for ANONYMOUS
strUserName = ""
strPassword = ""
strUserSSL = False 'True if SMTP Server uses SSL; False if Not

'Message Settings
strTo = "[email protected]"
'Separate multiple addresses with a semi-colon (;)
strCC = ""
strBCC = ""
strFrom = "[email protected]"
strSubject = "Pending Sales Order - Perlu di follow up"
strBodyType = "TEXT"
strAttachment = "D:\File.txt" 'Attachment Path i.e. C:\Temp\File.txt
strDSNotification = cdoDSNDefault 'Delivery Status Option Change as needed

'WScript.Echo "Connecting to database..."

'Connect to database & select all from Table
Set objDB = DBConnect()
Set oRS = objDB.Execute("SELECT S_ORDER 'SO#               ',CUSTOMER_NAME'CUSTOMER          ',DATE 'Tanggal           ',USERID 'INTERNAL          ',CALLING 'Approval from     ',LIMIT 'LIMIT             ',TERM 'TERM              ' from abc")

'Dump Records from Table
strOutput = "Please Check This Report :"  & vbCrLf
nRec = 1
Do While Not oRS.EOF
    strOutput = strOutput & "----- " & nRec & " -----" & vbCrLf
    nRec = nRec + 1
    For Each oFld In oRS.Fields
        strOutput = strOutput & oFld.Name & " = " & oFld.Value & vbCrLf
    Next
    oRS.MoveNext
Loop

SendEmail strOutput

'WScript.Echo "Script Finished"

'This function sets up DB Connection using specified DSN
Function DBConnect
    Set objDB = CreateObject("ADODB.Connection")
    objDB.Open "DSN=SQL;uid=sa;pwd=12345"
    'Set Conn = Server.CreateObject("ADODB.Connection")
  'Conn.open "SQL","sa","12345"
  Set DBConnect = objDB
End Function

Sub SendEmail(strBody)
    'Create Objects
    Set objConfig = CreateObject("CDO.Configuration")
    Set objEmail = CreateObject("CDO.Message")

    'Prepare email configuration
    With objConfig.Fields
        .Item(CDO_SCHEMA & "sendusing") = strSendMethod
        .Item(CDO_SCHEMA & "smtpserver") = strSmtpServer
        .Item(CDO_SCHEMA & "smtpserverport") = intSmtpPort
        .Item(CDO_SCHEMA & "smtpconnectiontimeout") = intSmtpTimeout
        .Item(CDO_SCHEMA & "smtpauthenticate") = strAuthenticationMethod

        If.Item(CDO_SCHEMA & "smtpauthenticate") <> 0 Then
            .Item(CDO_SCHEMA & "sendusername") = strUsername
            .Item(CDO_SCHEMA & "sendpassword") = strPassword
            .Item(CDO_SCHEMA & "smtpusessl") = strUserSSL
        End If
        .Update
    End With

    'Create email and send
    With objEmail
        Set.Configuration = objConfig

        .To = strTo

        If strCC <> "" Then
            .CC = strCC
        End If

        If strBCC <> "" Then
            .BCC = strBCC
        End If

        .From = strFrom

        .Subject = strSubject

        If strBodyType = "HTML" Then
            .HTMLBody = strBody
        ElseIf strBodyType = "TEXT" Then
            .TextBody = strBody
        End If

        If strAttachment <> "" Then
            .AddAttachment strAttachment
        End If

        If strDSNotification <> 0 And strDSNotification <> 1 Then
            .Fields(CDO_MAIL_HEADER & "disposition-notification-to") = strFrom
            .Fields(CDO_MAIL_HEADER & "return-receipt-to") = strFrom
            .DSNOptions = strDSNotification
            .Fields.update
        End If

        .Send
    End With
End Sub
安斯加·威彻斯(Ansgar Wiechers)

只需根据您的查询是否返回记录来发送电子邮件即可。

更改此行:

SendEmail strOutput

到这个:

If nRec > 1 Then SendEmail strOutput

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

选择通过Gmail发送电子邮件时,电子邮件正文为空

来自分类Dev

SQL电子邮件触发将查询发送为文本而不是实际结果

来自分类Dev

此代码可以运行以向客户发送电子邮件和短信通知。需要进行一些更改,以确保如果对象中的电子邮件为空,则不会发送任何电子邮件

来自分类Dev

发送电子邮件结果为“缺少所需的身份验证”

来自分类Dev

从列表发送电子邮件后,为什么字段TO为空?

来自分类Dev

尽管mail.outbox 为空,但AnyMail 未在Django 测试中发送电子邮件?

来自分类Dev

发送电子邮件时结果重复

来自分类Dev

从 PHP 脚本的结果发送电子邮件的最佳方式

来自分类Dev

如果磁盘空间超过则发送电子邮件

来自分类Dev

Python,运行测试,如果失败则发送电子邮件

来自分类Dev

PHPMailer不发送电子邮件(如果构造)

来自分类Dev

如果磁盘空间超过则发送电子邮件

来自分类Dev

如果出错,则跳过发送电子邮件

来自分类Dev

从<>空发件人地址发送电子邮件

来自分类Dev

PHP为每个用户发送电子邮件

来自分类Dev

Ajax 不发送电子邮件,但状态为 200

来自分类Dev

如果用Rails ActionMailer清空电子邮件,如何不发送电子邮件

来自分类Dev

发送电子邮件smtplib时将电子邮件解析为HTML

来自分类Dev

通过HTML表单发送电子邮件,SQL地址

来自分类Dev

电子邮件表格未发送电子邮件

来自分类Dev

SQL Server:通过电子邮件发送动态SQL查询的结果

来自分类Dev

SQL查询以查找在发送电子邮件后第二天到第五天之间打开过电子邮件的用户

来自分类Dev

如何从ELMAH发送电子邮件?

来自分类Dev

使用sendmail发送电子邮件

来自分类Dev

用PHP发送电子邮件

来自分类Dev

使用php发送电子邮件

来自分类Dev

JavaMail无法发送电子邮件

来自分类Dev

Gitlab无法发送电子邮件

来自分类Dev

以横向发送电子邮件给WickedPDF

Related 相关文章

  1. 1

    选择通过Gmail发送电子邮件时,电子邮件正文为空

  2. 2

    SQL电子邮件触发将查询发送为文本而不是实际结果

  3. 3

    此代码可以运行以向客户发送电子邮件和短信通知。需要进行一些更改,以确保如果对象中的电子邮件为空,则不会发送任何电子邮件

  4. 4

    发送电子邮件结果为“缺少所需的身份验证”

  5. 5

    从列表发送电子邮件后,为什么字段TO为空?

  6. 6

    尽管mail.outbox 为空,但AnyMail 未在Django 测试中发送电子邮件?

  7. 7

    发送电子邮件时结果重复

  8. 8

    从 PHP 脚本的结果发送电子邮件的最佳方式

  9. 9

    如果磁盘空间超过则发送电子邮件

  10. 10

    Python,运行测试,如果失败则发送电子邮件

  11. 11

    PHPMailer不发送电子邮件(如果构造)

  12. 12

    如果磁盘空间超过则发送电子邮件

  13. 13

    如果出错,则跳过发送电子邮件

  14. 14

    从<>空发件人地址发送电子邮件

  15. 15

    PHP为每个用户发送电子邮件

  16. 16

    Ajax 不发送电子邮件,但状态为 200

  17. 17

    如果用Rails ActionMailer清空电子邮件,如何不发送电子邮件

  18. 18

    发送电子邮件smtplib时将电子邮件解析为HTML

  19. 19

    通过HTML表单发送电子邮件,SQL地址

  20. 20

    电子邮件表格未发送电子邮件

  21. 21

    SQL Server:通过电子邮件发送动态SQL查询的结果

  22. 22

    SQL查询以查找在发送电子邮件后第二天到第五天之间打开过电子邮件的用户

  23. 23

    如何从ELMAH发送电子邮件?

  24. 24

    使用sendmail发送电子邮件

  25. 25

    用PHP发送电子邮件

  26. 26

    使用php发送电子邮件

  27. 27

    JavaMail无法发送电子邮件

  28. 28

    Gitlab无法发送电子邮件

  29. 29

    以横向发送电子邮件给WickedPDF

热门标签

归档