WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

Web UDFs

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Send Internet Email Using SSL

 Keywords: udfSendInternetEmail Send Web Internet Email SSL CDO YAHOO HOTMAIL LIVE GMAIL AOL 

#DefineFunction udfSendInternetEmail( sEMailID, sPassword, sTo, sCC, sBCC, sSubject, sBody, sAttachment )
   ;--------------------------------------------------------------------------------;
   ; udfSendInternetEmail : Send SSL Web / Internet Email Using CDO.                ;
   ;                        supports: hotmail, live, yahoo, gmail, aol              ;
   ;--------------------------------------------------------------------------------;
   ; sEmailID      : Sender's Mail ID String                                          ;
   ; sPassword      : Sender's Password String                                          ;
   ; sTo            : Recipient's Mail ID String (Primary)                              ;
   ; sCC            : Recipient's Mail ID String (CC)                                 ;
   ; sBCC         : Recipient's Mail ID String (BCC)                                 ;
   ; sSubject      : Subject String                                                   ;
   ; sBody         : Body Message String                                             ;
   ; sAttachment   : File Attachment in the form of a URL. file://c:/mydir/myattachment.pdf;
   ;--------------------------------------------------------------------------------;
   ; returns    : @true if success, @false if failure                                 ;
   ;--------------------------------------------------------------------------------;
   ; REQUIRES: CDO (Collaboration Data Objects), version 1.2.1                        ;
   ;   Beginning in Exchange Server 2007 and Outlook 2007, CDO 1.2.1 will no longer   ;
   ;   be provided as a part of the install of the product. As a result, there is   ;
   ;   functionality missing that this code depends upon. CDO 1.2.1 is a package      ;
   ;   providing access to Outlook-compatible objects through a COM-based API.      ;
   ;                                                                                 ;
   ;   CDO 1.2.1 download:                                                            ;
   ;   http://www.microsoft.com/downloads/details.aspx?FamilyID=2714320d-c997-4de1-986f-24f081725d36&DisplayLang=en
   ;                                                                                 ;
   ; NOTES:                                                                           ;
   ; HOTMAIL, LIVE and YAHOO all require premium account in order to send            ;
   ; email using their SMTP servers. This code will fail if you do not have a         ;
   ; premium account.                                                               ;
   ;                                                                                 ;
   ; Deana Falk 2010.04.30                                                            ;
   ;--------------------------------------------------------------------------------;
   ; Confirm email address is valid string
   If !StrIndex( sEMailID, '@', 1, @FWDSCAN )
       Pause( '','Invalid email id. Must be complete email account name: soandso@here.com' )
       Return 0
   EndIf
   accountname = ItemExtract( 1, sEMailID, '@' )

   ; Determine SMTP hostname
   If StrIndex( StrLower( sEMailID ), "@yahoo", 1, @FWDSCAN ) <> 0 Then
      cdoSMTPServer = "plus.smtp.mail.yahoo.com"
   ElseIf  StrIndex( StrLower( sEMailID ), "@gmail", 1, @FWDSCAN ) <> 0 Then
      cdoSMTPServer = "smtp.gmail.com"
   ElseIf  StrIndex( StrLower( sEMailID ), "@hotmail", 1, @FWDSCAN ) <> 0 ||  StrIndex( StrLower( sEMailID ), "@live", 1, @FWDSCAN ) <> 0 Then
      cdoSMTPServer = "smtp.live.com"
   ElseIf  StrIndex( StrLower( sEMailID ), "@aol", 1, @FWDSCAN ) <> 0 Then
      cdoSMTPServer = "smtp.aol.com"
   EndIf

   ; Select the port based on the host
   If  StrIndex( StrLower( sEMailID ), "@yahoo", 1, @FWDSCAN ) <> 0 ||  StrIndex( StrLower( sEMailID ), "@gmail", 1, @FWDSCAN ) <> 0
      cdoOutgoingMailSMTP = 465
   ElseIf  StrIndex( StrLower( sEMailID ), "@aol", 1, @FWDSCAN ) <> 0  ||  StrIndex( StrLower( sEMailID ), "@hotmail", 1, @FWDSCAN ) <> 0 ||  StrIndex( StrLower( sEMailID ), "@live", 1, @FWDSCAN ) <> 0
      cdoOutgoingMailSMTP = 587
   Else
      cdoOutgoingMailSMTP = 25
   EndIf

   ; Use SSL?
   cdoUseSSL = @TRUE
   If  StrIndex( StrLower( sEMailID ), "@aol", 1, @FWDSCAN ) <> 0 Then
      cdoUseSSL = @FALSE
   EndIf

   ;Use Full Email Address for account name
   If  StrIndex( StrLower( sEMailID ), "@hotmail", 1, @FWDSCAN ) <> 0 ||StrIndex( StrLower( sEMailID ), "@live", 1, @FWDSCAN ) <> 0
      accountname = sEMailID
   EndIf


   oMsg = ObjectCreate("CDO.Message")
   oConf = ObjectCreate("CDO.Configuration")

   ; Define CDO Schemas
   oFields = oConf.Fields
   oFields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver")         = cdoSMTPServer; "plus.smtp.mail.yahoo.com"
   oFields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport")      =  cdoOutgoingMailSMTP
   oFields.Item("http://schemas.microsoft.com/cdo/configuration/smtpaccountname")   = accountname
   oFields.Item("http://schemas.microsoft.com/cdo/configuration/smtpemailaddress")   =  sEMailID
   oFields.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate")   = 1
   oFields.Item("http://schemas.microsoft.com/cdo/configuration/sendusername")      =  accountname
   oFields.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword")      =  sPassword
   oFields.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl")         = cdoUseSSL
   oFields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing")         = 2
   oFields.Item("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout")= 60  ; Default is 60 Seconds
   oFields.Update()


   ;Send Message
   oMsg.Configuration = oConf
   oMsg.To = sTo
   oMsg.CC = sCC
   oMsg.BCC = sBCC
   oMsg.From = sEMailID
   oMsg.Subject = sSubject
   oMsg.TextBody = sBody
   ;oMsg.HTMLBody = "<h1>This is some sample message html.</h1>" ; !!! Add code to send HTML formatted BODY.

   If sAttachment != "" Then oMsg.AddAttachment(sAttachment)
   oMsg.Send()

   oMsg   = 0
   oConf   = 0
   oFields   = 0
   Return 1
#EndFunction

;Sample Input
password      = "secret"                ; MODIFY TO FIT YOUR NEEDS
ToAddress   = "soandso@hotmail.com"      ; MODIFY TO FIT YOUR NEEDS
FromAddress   = "soandso@hotmail.com"   ; MODIFY TO FIT YOUR NEEDS
Cc            = ""                     ; MODIFY TO FIT YOUR NEEDS
BCC         = ""                        ; MODIFY TO FIT YOUR NEEDS
Subject      = "Test Email"               ; MODIFY TO FIT YOUR NEEDS
TextBody      = "Test of CDO Email"   ; MODIFY TO FIT YOUR NEEDS
FileAttach   = "file://c:/mydir/myattachment.pdf"         ; MODIFY TO FIT YOUR NEEDS
udfSendInternetEmail( FromAddress, Password, ToAddress, CC, BCC, Subject, TextBody, FileAttach )

Exit

Article ID:   W18413
Filename:   Send Internet Email Using SSL.txt
File Created: 2010:05:03:09:48:42
Last Updated: 2010:05:03:09:48:42