SendEmail_CDO

Sending an email using CDO object
Can pass all needed variables including mail server user name and password.
Can send either html body or text
Does not have ability to attach files, yet!

Function SendEmail_CDO(ToEmail, Subject, BodyHTML, or_BodyText, MailServerFrom, MailServerPassword)
 ' ASP Send Mail Sample. In order to run this sample,
 ' replace ConstUserName 'mail@example.com' with the e-mail of the account used to send the e-mail,
 ' replace ConstPassword 'PASSWORD' with the proper password of the account used to send the e-mail,
 ' replace the mail FROM address 'mail@example.com' with the e-mail of the account used to send the e-mail,
 ConstMailServer                                = "localhost"
 ConstUserName                                 = MailServerFrom ' "mail@example.com"
 ConstPassword                                 = MailServerPassword ' "PASSWORD"

 Set myMail                                  = CreateObject("CDO.Message")
   ' Create CDO.Message COM object.
   ' http://msdn.microsoft.com/en-us/library/ms527271(v=exchg.10)
 myMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusing")      = 2
   ' Message Configuration. Specifies the method used to send messages. ' Must be set to 2!
   ' http://msdn.microsoft.com/en-us/library/ms526994(v=exchg.10) ' http://msdn.microsoft.com/en-us/library/ms527265(v=exchg.10)
 myMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate")    = 1
   ' Specifies the authentication mechanism to use when authentication is required to send messages to an SMTP service using a TCP/IP network socket. ' Must be set to 1!
   ' http://msdn.microsoft.com/en-us/library/ms526600(v=exchg.10) ' http://msdn.microsoft.com/en-us/library/ms526961(v=exchg.10)
 myMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver")      = ConstMailServer
   ' The name (DNS) or IP address of the machine hosting the SMTP service through which messages are to be sent. ' Set to 'localhost' for local SMTP server.
   ' http://msdn.microsoft.com/en-us/library/ms527294(v=exchg.10)
 myMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport")     = 25
   ' The port on which the SMTP service specified by the smtpserver field is listening for connections. ' Set to 25 for local SMTP server.
   ' http://msdn.microsoft.com/en-us/library/ms526227(v=exchg.10)
 ' Authentication.
 myMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusername")     = ConstUserName
   ' The username for authenticating to an SMTP server using basic (clear-text) authentication.
   ' http://msdn.microsoft.com/en-us/library/ms527002(v=exchg.10)
 myMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendpassword")     = ConstPassword
   ' The password used to authenticate to an SMTP server using basic (clear-text) authentication.
   ' http://msdn.microsoft.com/en-us/library/ms526940(v=exchg.10)
 myMail.Configuration.Fields.Update
   ' Update the configuration fields.
   ' http://msdn.microsoft.com/en-us/library/jj250294(v=office.15).aspx
 myMail.From                                  = ConstUserName
   ' Set mail FROM. ' Must match user name used for SMTP authentication!
   ' http://msdn.microsoft.com/en-us/library/ms527318(v=exchg.10)
 myMail.To                                  = ToEmail ' "mail@example.com"
   ' Set mail TO.
   ' http://msdn.microsoft.com/en-us/library/ms527328(v=exchg.10)
 myMail.Subject                                 = Subject ' "Sending e-mail with CDO"
   ' Set mail SUBJECT. ' http://msdn.microsoft.com/en-us/library/ms527248(v=exchg.10)
 If BodyHTML > "" Then
  myMail.HTMLBody                               = BodyHTML ' "<H3>This is an HTML message.</H3>"
   ' Set mail BODY (HTMLBody). ' Do not uncomment both, HTMLBody and TextBody, at the same time.
   ' http://msdn.microsoft.com/en-us/library/ms527537(v=exchg.10)
 ElseIf or_BodyText > "" Then
  myMail.TextBody                               = or_BodyText ' "This is a plain text message."
   ' Set mail BODY (TextBody). ' Do not uncomment both, HTMLBody and TextBody, at the same time.
   ' http://msdn.microsoft.com/en-us/library/ms526935(v=exchg.10)
 End If
 myMail.Send
   ' Send the mail.
   ' http://msdn.microsoft.com/en-us/library/ms527781(v=exchg.10)
 Set myMail                                  = nothing
   ' Release CDO object.
End Function

ToEmail, Subject, BodyHTML, or_BodyText, MailServerFrom, MailServerPassword

Views 206 Downloads 107

'email', 'mail', 'cdo', 'server', 'send', 'subject', 'from', 'to', 'body'

ANmarAmdeen
333
Components Classic ASP
Revisions

v1.0