Javax Mail -> Send Mail with Attachment

Dear all master, i got some problem when trying to send mail with attachment. Here is the flow:

  1. I created a custom keyword with .groovy script
    sendmail.txt (2.5 KB)

  2. Next i call the keyword on the test case, like this:
    image

And i got error like this:

Test Cases/Email/Email File FAILED.
Reason:
java.lang.NoClassDefFoundError: Could not initialize class com.yeehaw.mail.SendMailWithFile
at com.kms.katalon.core.main.CustomKeywordDelegatingMetaClass.invokeStaticMethod(CustomKeywordDelegatingMetaClass.java:46)
at Email File.run(Email File:18)
at com.kms.katalon.core.main.ScriptEngine.run(ScriptEngine.java:194)
at com.kms.katalon.core.main.ScriptEngine.runScriptAsRawText(ScriptEngine.java:119)
at com.kms.katalon.core.main.TestCaseExecutor.runScript(TestCaseExecutor.java:337)
at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:328)
at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:307)
at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:299)
at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:233)
at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:114)
at com.kms.katalon.core.main.TestCaseMain$runTestCase$0.call(Unknown Source)
at TempTestCase1585729436239.run(TempTestCase1585729436239.groovy:23)

  1. when i run the script using groovy console, it work fine:

What should i do dear kind sir

The script in the sendmail.txt does not have any package statement

package com.yeehaw.mail

You should create a file Keywords/com/yeehaw/mail/SendMailWithFile.groovy. You want paste the sendmail.txt into it. And you want to insert a package statement as above.

forgot to tell you. I’ve tried my things before, such as:

  1. Deleting the bin, libs, classpath, and project files
  2. Upgrading my KS (from 7.2.1 to 7.3)
  3. Reinstalling JDK 14
  4. Reimporting javax.jar and activation.jar

regards

Your point 1,2,3,4 has nothing to do with your coding mistake in your custom keyword (missing “package” statement).

I think you need to correct the keyword script.

hi Kazurayam, i took your advice and added the package statement to the script, the result is still the same:

My guess is there something to do with the static method, but im not that good with Java.

I’d advise against using public static void main(string[args]) since it’s a reserved thing. You should just write something like:

@Keyword
def yourKeyword(String arg1, String arg2, ...) {
}

Try to close and open the project so as to parse custom keywords classes.

A typical source of a class as Keyword looks like this:

package com.yeehaw.mail

import ...

class SendMailWithFile {
    /** constructor has nothing todo but should be there */
    SendMailWithFile() {}

    /** keyword implementation as an instance method, Katalon will call it */
    @Keyword
    public void execute() {
        // your codes currently contained in the main() method comes here 
        ....
    }

    /** main() is used in commandline only, Katalon Studio does not use main() */
    public static void main(String[args]) {
        SendMailWithFile instance = new SendMailWithFile()
        instance.execute();
    }
}

And you testcase script will call it like:

import com.yeehaw.mail.SendMailWithFile

SendMailWithFile instace = new SendMailWithFile()
instance.execute()

or similarly

CustomKeywords.'com.yeehaw.mail.SendMailWithFile.execute'()

Dear Kazurayam and ThanhTo,

Im pretty sure my lack of knowledge about katalon and Java is the main problem. to all master, thank you very much. it worked now, using kazurayam advice. Guess there’s a lot more to learn about!

once again, thank you

Sorry to disturbe you. Let me go back to your initial question and explain what I thought.

You had a testcase script with the following line:

CustomKeywords.'com.yeehaw.mail.SendMailWithFile.main'([])

When you run it you got an error like this:

Reason: java.lang.NoClassDefFoundError: Could not initialize class com.yeehow.mail.SendMailWithFile
at ...

One of the reasons of errror were missing package statement. But there seemed to at least one other reason.

I think you wanted the following code.

CustomKeywords.'com.yeehaw.mail.SendMailWithFile.main'(new String[]{""})

The difference is data type of the argument to the main(…) method.


Your Keyword implementation had @Keyword public static void main(String[] args). Therefore your testcase script was required to pass an argument as Array of String.

new String[]{""} is an instance of Array of String.

However you gave main([]). In Groovy language, [] means an empty instance of java.util.List. [] is NOT an array of java.lang.String. These two data types behaves similarly in some context, but they behave differently in other context. This ambiguity of language may cause confusion.

You specified an unmatching data type(List) as argument to main(String[] args) method. Therefore Groovy could not determine which class to load and which method to invoke. Therefore Groovy threw a Runtime Exception java.lang.NoClassDefFoundError.

Hi Team,

I have done the same but i am getting error like this

Is this the correct way of declaring in the keyword

And also can u mention the office 365 settings.

Thanks Regards
Karthik

@unnecessary.333

The screenshot you provided to us does not show how your Keyword class is named. The test case scripts indicates that the fully qualified class name of your Keyword is:

tomobile_.SendMailWithFile

On the other hand, you have a directory structure:

Keywords/tomobile_/sendMail.groovy

This directory+file name is against the convention. This is not the correct way of declaring the keyword.


First I would recommend you to rename the package tomobile_ to tomobile; as a underline character _ is rarely used in package names. To me a package name with _ looks to be a mistake.

Then, the directory structure of your Keyword class MUST be:

Keywords/tomobile/SendMailWithFile.groovy

Please study the following article about how package/class in Java+Groovy is named and how they relates to the underlying directory structure.



What do you what to ask?