Forum Discussion

m_essaid's avatar
m_essaid
Valued Contributor
11 years ago
Solved

Using the CDO SendMail functions without attaching files

Hi,

I use the following help to send a pdf :

http://support.smartbear.com/viewarticle/56308/?_ga=1.182325346.417838577.1387551043#CDO



It works pretty well when i have a file to attach. But in some cases i have to not send any files.



I tried to modify this appeal of the mail function :



SendEmail("tester@mycompany.com", "boss@mycompany.com", "Subject", _

             "Message body", "c:\File1.txt,c:\File2.txt,c:\File3.txt")



by putting '' or nil but in the first case it attach an empty file, in the second case it don't execute the script.



Any help please ?



Mehdi
  • It's a little strange that the SendEmail is attempting to send an attachment if there are no files to attach.  Maybe you need to modify the SendEmail script a bit...



    Currently, in JScript, we have this section:



        for(i = 0; i < aqString.GetListLength(mAttach); i++)

          mMessage.AddAttachment(aqString.GetListItem(mAttach, i));





    It seems that, if GetListLength is 0, it will still attempt 1 iteration through the for loop (i from 0 to 0).  So, what if we put, before the for loop, some if/then logic.  Like so:



    if (aqString.GetListLength(mAttach) > 0)

    {

    for(i = 0; i < aqString.GetListLength(mAttach); i++)

          mMessage.AddAttachment(aqString.GetListItem(mAttach, i));

    }





    That way, if the mAttach parameter is empty, it won't even execute the for loop.



    See if that works for you.

8 Replies

  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor
    It's a little strange that the SendEmail is attempting to send an attachment if there are no files to attach.  Maybe you need to modify the SendEmail script a bit...



    Currently, in JScript, we have this section:



        for(i = 0; i < aqString.GetListLength(mAttach); i++)

          mMessage.AddAttachment(aqString.GetListItem(mAttach, i));





    It seems that, if GetListLength is 0, it will still attempt 1 iteration through the for loop (i from 0 to 0).  So, what if we put, before the for loop, some if/then logic.  Like so:



    if (aqString.GetListLength(mAttach) > 0)

    {

    for(i = 0; i < aqString.GetListLength(mAttach); i++)

          mMessage.AddAttachment(aqString.GetListItem(mAttach, i));

    }





    That way, if the mAttach parameter is empty, it won't even execute the for loop.



    See if that works for you.
  • TanyaYatskovska's avatar
    TanyaYatskovska
    SmartBear Alumni (Retired)

    Hi Mehdi,


     


    If you do not need to attach files at all, you may consider modifying the SendEmail function by removing the code that adds attachments.


     

  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor
    What script code language are you using for your coding?



    As it is, if you don't want to send a file, you should be able to just do:



    SendEmail("tester@mycompany.com", "boss@mycompany.com", "Subject", "Message body", "")




    The empty string for the attachments should return a string length of zero and end up with an empty set of attachments.
  • m_essaid's avatar
    m_essaid
    Valued Contributor
    Hi Robert,



    I'm using a Delphi like script to test a Delphi program.



    When i put '' (quotes-quotes) at the attachment location it sends a file with a few bytes (less than a kb) with the mail.



    Could I avoid that ?



    Thank you,



    Mehdi
  • m_essaid's avatar
    m_essaid
    Valued Contributor
    Hi Tanya,

    Unfortunatly I have to keep this parameter, because if the test fails it sends a PDF file via email, and if not it should not send any file, just an email.

    Mehdi
  • m_essaid's avatar
    m_essaid
    Valued Contributor
    Hello Robert,



    That's what i've done in delphi script and it work pretty well thank you very much.




        if(mAttach <> '') then


        begin  


          aqString.ListSeparator := ',';


          for i := 0 to aqString.GetListLength(mAttach) - 1 do


            mMessage.AddAttachment(aqString.GetListItem(mAttach, i));


        end;

  • mgroen2's avatar
    mgroen2
    Super Contributor

    Hi, 

    I try to use CDO, in VB Script. Send the mail via outlook server.

     

    I copied the example from here  but for me it's unclear where to enter mail settings, in the file. Did try some input fields and when I run the script, the output is "Mail is sent successfully", however no mail is received (also not in spambox).

    Maybe serverport is wrong or the wrong smtp authentication (what does 1 mean? 2 ? )?

     

    Here is the code I use (I used outlook username and password):

     

     

    Function SendEmail(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.Item(schema + "smtpauthenticate") = 1 ' Authentication mechanism
    mConfig.Fields.Item(schema + "sendusername") = "USERNAME" ' User name (if needed)
    mConfig.Fields.Item(schema + "sendpassword") = "PASSWORD" ' User password (if needed)
    mConfig.Fields.Update

    Set mMessage = Sys.OleObject("CDO.Message")
    mMessage.Configuration = mConfig
    mMessage.From = "testrunner"
    mMessage.To = "m.groen@BLABLA.COM"
    mMessage.Subject = "test message"
    mMessage.HTMLBody = "this is a test message!"

    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

    Sub MainTest
    If SendEmail("testrunner_dp@BLABLA.COM", "m.groen@BLABLA.COM", "Subject", _
    "Message body", "c:\a\File1.txt") Then
    ' Message was sent
    Else
    ' Message was not sent
    End If
    End Sub