Forum Discussion

yingkai1208's avatar
yingkai1208
Contributor
11 years ago

Could not send an email via SendMail Function in TestComplete

Hi   The detail of the function is as follows:   SendMail(ToAddress, FromHost, FromName, FromAddress, Subject, Body, FileName1, FileName2, ...)   After replace the parameters with th...
  • tristaanogre's avatar
    11 years ago
    Since you need to be able to specify a port and, potentially, authenticate the user that you need to use to send the e-mail, you might want to consider using the CDO objects to send the e-mail.  You can find the article describing this method here.  It contains examples as to how to create the routine in order to do what you're asking.



    It does sound like, based upon the responses and functionality, that your problem is due to authentication on the server.  Let me know if CDO helps or if you need additional help.
  • mgroen2's avatar
    mgroen2
    9 years ago

    Hi,

     

    I have worked on the script, and it works now. For both gmail and outlook.com

    Entering user name/password fails to send the mails so I deleted these lines. Apparently no authentication or whatever is needed.

     

    Here is the working code , including server names for Outlook and gmail:

     

    Outlook.com:

     

    Function Send_Email_using_Outlook(mFrom, mTo, mSubject, mBody,   mAttachment)

     

      Dim i, schema, mConfig, mMessage

     

      Err.Clear

      On Error Resume Next

     

      schema = "http://schemas.microsoft.com/cdo/configuration/"

      Set mConfig = Sys.OleObject("CDO.Configuration")

      mConfig.Fields.Item(schema + "sendusing") = 2 ' cdoSendUsingPort

      mConfig.Fields.Item(schema + "smtpserver") = "smtp-mail.outlook.com" ' SMTP server

      mConfig.Fields.Item(schema + "smtpserverport") = 25  ' Port number

      mConfig.Fields.Update

     

      Set mMessage = Sys.OleObject("CDO.Message")

      mMessage.Configuration = mConfig

      mMessage.From = mFrom

      mMessage.To = mTo

      mMessage.Subject = mSubject

      mMessage.HTMLBody = mBody

     

      aqString.ListSeparator = ","

      For i = 0 To aqString.GetListLength(mAttachment) - 1

        mMessage.AddAttachment aqString.GetListItem(mAttachment, i)

      Next

     

      mMessage.Send

     

      If Err.Number > 0 Then

        Log.Error "E-mail cannot be sent", Err.Description

        SendEMail = False

      Else

        Log.Message "Message to <" + mTo + "> was successfully sent"

        SendEMail = True

      End If

    End Function

     

     

     

     

    GMAIL:

     

    Function Send_Email_using_GMail(mFrom, mTo, mSubject, mBody,     mAttachment)

     

      Dim i, schema, mConfig, mMessage

     

      Err.Clear

      On Error Resume Next

     

      schema = "http://schemas.microsoft.com/cdo/configuration/"

      Set mConfig = Sys.OleObject("CDO.Configuration")

      mConfig.Fields.Item(schema + "sendusing") = 2 ' cdoSendUsingPort

      mConfig.Fields.Item(schema + "smtpserver") = "smtp.gmail.com" ' SMTP server

      mConfig.Fields.Item(schema + "smtpserverport") = 25  ' Port number

      mConfig.Fields.Update

     

      Set mMessage = Sys.OleObject("CDO.Message")

      mMessage.Configuration = mConfig

      mMessage.From = mFrom

      mMessage.To = mTo

      mMessage.Subject = mSubject

      mMessage.HTMLBody = mBody

     

      aqString.ListSeparator = ","

      For i = 0 To aqString.GetListLength(mAttachment) - 1

        mMessage.AddAttachment aqString.GetListItem(mAttachment, i)

      Next

     

      mMessage.Send

     

      If Err.Number > 0 Then

        Log.Error "E-mail cannot be sent", Err.Description

        SendEMail = False

      Else

        Log.Message "Message to <" + mTo + "> was successfully sent"

        SendEMail = True

      End If

    End Function

     

     

    If you call these functions using KDT testing enter From/To/subject/body and Path to attach files:

     

     

    Hope this helps you. I will also inform support on this.

     

    Regards,

    Mathijs