Share via


Sending Mail Using SMTP

This content is no longer actively maintained. It is provided as is, for anyone who may still be using these technologies, with no warranties or claims of accuracy with regard to the most recent product version or service release.

The following example uses the modSendMail function to send e-mail to a designated e-mail address using Simple Mail Transfer Protocol (SMTP). SMTP is a member of the TCP/IP suite of protocols that governs the exchange of electronic mail between message transfer agents. To use this example, you must have SMTP running.

You can add the modSendMail function to an event to notify a user when an issue is opened or closed.

To use this example, add the "Calling the Function" code to the appropriate script procedure, and add the "Example Script" code wherever you usually store functions.

Calling the Function

'// modSendMail
'//
'// ---------------------------------------------------------------
    ret= modSendMail("sendto@microsoft.com", " sendfrom@microsoft.com ", "test subject", "test message")
    call logger.printstring("send mail return value: " & ret & chr(13) & chr(10))
'// ---------------------------------------------------------------

Example Script

'// ---------------------------------------------------------------
'// Name      : modSendMail
'// Purpose   : send e-mail using SMTP
'//
'// Prereq    : assumes that you have a smarthost assigned in the SMTP Settings to route
'//           : mail using Exchange

'// Inputs    : strTo - where to send e-mail
'//           : strFrom - name to use for sending e-mail
'//           : strSubject - subject of e-mail
'//           : strBody - body of e-mail
'//
'// Return    : True if function succeeds, otherwise false
'// ---------------------------------------------------------------
Function modSendMail(strTo, strFrom, strSubject, strBody)

    '// declarations
    Dim objmail
  
    '// assume failure
    modSendMail = False

    On Error Resume Next
    Set mail = CreateObject("CDONTS.NewMail")
    
    Call mail.Send(strFrom, strTo, strSubject, strBody)
    
    '// check for fail
    If Err Then
        logger.printstring ("Unable to Send Mail" & vbCrLf)
        Exit Function
    End If
    modSendMail = True

End Function

See Also

Script Examples for SQL Server | Finding a User's E-Mail Address in the User Directory | Finding a Manager in the User Directory | Sending Mail to a Manager | Calling a Stored Procedure