Search code examples
javajmeterjmeter-5.0

Jmeter 5.5 XML SMTP Sampler config - cannot set headers


I'm trying to set up a local Jmeter run with this .jmx file (XML):

<?xml version="1.0" encoding="UTF-8"?>
<jmeterTestPlan version="1.2" properties="5.0" jmeter="5.4">
  <hashTree>
    <TestPlan guiclass="TestPlanGui" testclass="TestPlan" testname="Perf Tests" enabled="true">
      <stringProp name="TestPlan.comments"></stringProp>
      <boolProp name="TestPlan.functional_mode">false</boolProp>
      <boolProp name="TestPlan.tearDown_on_shutdown">true</boolProp>
      <boolProp name="TestPlan.serialize_threadgroups">false</boolProp>
      <elementProp name="TestPlan.user_defined_variables" elementType="Arguments" guiclass="ArgumentsPanel" testclass="Arguments" testname="User Defined Variables" enabled="true">
        <collectionProp name="Arguments.arguments"/>
      </elementProp>
      <stringProp name="TestPlan.user_define_classpath"></stringProp>
    </TestPlan>
    <hashTree>
      <ThreadGroup guiclass="ThreadGroupGui" testclass="ThreadGroup" testname="Big - 8mb attachment" enabled="true">
        <stringProp name="ThreadGroup.on_sample_error">continue</stringProp>
        <elementProp name="ThreadGroup.main_controller" elementType="LoopController" guiclass="LoopControlPanel" testclass="LoopController" testname="Loop Controller" enabled="true">
          <boolProp name="LoopController.continue_forever">false</boolProp>
          <stringProp name="LoopController.loops">10</stringProp>
        </elementProp>
        <stringProp name="ThreadGroup.num_threads">5</stringProp>
        <stringProp name="ThreadGroup.ramp_time">5</stringProp>
        <boolProp name="ThreadGroup.scheduler">false</boolProp>
        <stringProp name="ThreadGroup.duration"></stringProp>
        <stringProp name="ThreadGroup.delay"></stringProp>
        <boolProp name="ThreadGroup.same_user_on_next_iteration">true</boolProp>
      </ThreadGroup>
      <hashTree>
        <SmtpSampler guiclass="SmtpSamplerGui" testclass="SmtpSampler" testname="Email - 8mb attachment" enabled="true">
          <stringProp name="SMTPSampler.server">localhost</stringProp>
          <stringProp name="SMTPSampler.serverPort">8080</stringProp>
          <stringProp name="SMTPSampler.serverTimeout">15000</stringProp>
          <stringProp name="SMTPSampler.serverConnectionTimeout">15000</stringProp>
          <stringProp name="SMTPSampler.mailFrom">[email protected]</stringProp>
          <stringProp name="SMTPSampler.replyTo"></stringProp>
          <stringProp name="SMTPSampler.receiverTo">[email protected]</stringProp>
          <stringProp name="SMTPSampler.receiverCC"></stringProp>
          <stringProp name="SMTPSampler.receiverBCC"></stringProp>
          <stringProp name="SMTPSampler.subject">Perf Test - 1 attachment 8mb</stringProp>
          <stringProp name="SMTPSampler.suppressSubject">false</stringProp>
          <stringProp name="SMTPSampler.include_timestamp">false</stringProp>
          <stringProp name="SMTPSampler.message">Hello World - Perf Test!</stringProp>
          <stringProp name="SMTPSampler.plainBody">false</stringProp>
          <stringProp name="SMTPSampler.attachFile">8mb-content-size.pdf</stringProp>
          <stringProp name="SMTPSampler.useSSL">false</stringProp>
          <stringProp name="SMTPSampler.useStartTLS">false</stringProp>
          <stringProp name="SMTPSampler.trustAllCerts">false</stringProp>
          <stringProp name="SMTPSampler.enforceStartTLS">false</stringProp>
          <stringProp name="SMTPSampler.useLocalTrustStore">false</stringProp>
          <stringProp name="SMTPSampler.trustStoreToUse"></stringProp>
          <stringProp name="SMTPSampler.tlsProtocols"></stringProp>
          <boolProp name="SMTPSampler.use_eml">false</boolProp>
          <stringProp name="SMTPSampler.emlMessageToSend"></stringProp>
          <stringProp name="SMTPSampler.useAuth">false</stringProp>
          <stringProp name="SMTPSampler.password"></stringProp>
          <stringProp name="SMTPSampler.username"></stringProp>
          <stringProp name="SMTPSampler.messageSizeStatistics">false</stringProp>
          <stringProp name="SMTPSampler.enableDebug">false</stringProp>
          <collectionProp name="SMTPSampler.headerFields"/>
        </SmtpSampler>
        <hashTree/>
      </hashTree>
    </hashTree>
  </hashTree>
</jmeterTestPlan>

This XML sends email messages to the local server at localhost:8080 without any errors.

However, I would like to set the date header for these emails. In front of the SmtpSampler element I have tried adding a HeaderManager like this:

<HeaderManager guiclass="HeaderPanel" testclass="HeaderManager" testname="HTTP Header Manager" enabled="true">
          <collectionProp name="HeaderManager.headers">
            <elementProp name="Date" elementType="Header">
              <stringProp name="Header.name">Date</stringProp>
              <stringProp name="Header.value">Fri, 21 Nov 1997 09:55:06 -0600</stringProp>
            </elementProp>
          </collectionProp>
        </HeaderManager>

But this XML does not properly set the header on the SMTP messages.

I've tried replacing <collectionProp name="SMTPSampler.headerFields"/> with an expanded element:

<collectionProp name="SMTPSampler.headerFields">
            <elementProp name="Date" elementType="Header">
              <stringProp name="Header.name">Date</stringProp>
              <stringProp name="Header.value">Fri, 21 Nov 1997 09:55:06 -0600</stringProp>
            </elementProp>
          </collectionProp>

This results in the following stacktrace:

java.lang.ClassCastException: class org.apache.jmeter.protocol.http.control.Header cannot be cast to class org.apache.jmeter.config.Argument (org.apache.jmeter.protocol.http.control.Header and org.apache.jmeter.config.Argument are in unnamed module of loader org.apache.jmeter.DynamicClassLoader @372f7a8d)

I've also tried this:

<collectionProp name="SMTPSampler.headerFields">
              <elementProp name="date" elementType="Argument">
                <stringProp name="Argument.name">Date</stringProp>
                <stringProp name="Argument.value">Fri, 21 Nov 1997 09:55:06 -0600</stringProp>
              </elementProp>
            </collectionProp>

This configuration also fails.

I would love to see an example of setting headers in SMTP Sampler XML but I cannot find anything in the official Apache docs.

Thank you!


Solution

  • As it evidenced by its name HTTP Header Manager controls headers for the HTTP protocol

    If you need to add header for the SMTP protocol there is a special input in the SMTP sampler:

    enter image description here

    Or if you prefer manually editing the XML:

    <collectionProp name="SMTPSampler.headerFields">
      <elementProp name="foo" elementType="Argument">
        <stringProp name="Argument.name">foo</stringProp>
        <stringProp name="Argument.value">bar</stringProp>
      </elementProp>
    </collectionProp>