Bug 45053 - SMTP-Sampler for JMeter
Summary: SMTP-Sampler for JMeter
Alias: None
Product: JMeter
Classification: Unclassified
Component: Main (show other bugs)
Version: unspecified
Hardware: PC All
: P2 enhancement with 2 votes (vote)
Target Milestone: ---
Assignee: JMeter issues mailing list
Depends on:
Reported: 2008-05-21 00:54 UTC by Michael Tschannen
Modified: 2010-07-07 11:30 UTC (History)
2 users (show)

Source code for JMeter SMTP-Sampler (130.00 KB, application/x-tar)
2008-05-21 00:54 UTC, Michael Tschannen
Source code for JMeter SMTP-Sampler (with enhancements) (140.00 KB, application/x-tar)
2008-06-02 06:12 UTC, Armin Häberling
Source code for SMTP sampler (21.08 KB, application/x-gzip)
2010-05-21 04:57 UTC, Luciana Moreira
Source code for SMTP sampler witho Apache license (17.85 KB, application/x-gzip)
2010-05-21 06:19 UTC, Luciana Moreira
This patch contains a SMTPSampler with a graceful failure presentation at the GUI level when the bouncy castle jar is not present. (111.74 KB, text/plain)
2010-05-25 08:55 UTC, Luciana Moreira
Screenshot (57.54 KB, image/png)
2010-06-29 12:10 UTC, Luciana Moreira
SMTP Documentation Patch (3.83 KB, patch)
2010-06-29 12:14 UTC, Luciana Moreira
Details | Diff
Small patch to fix the described bug on TrustAllCerts. It also fixes the layout to completely fill the space. (1.17 KB, text/plain)
2010-07-05 04:03 UTC, Luciana Moreira

Note You need to log in before you can comment on or make changes to this bug.
Description Michael Tschannen 2008-05-21 00:54:46 UTC
Created attachment 21986 [details]
Source code for JMeter SMTP-Sampler

For a project, we have extended an existing implementation of a JMeter SMTP-Sampler (initially coded by Luca Maragnani, found at http://www.beolink.org/index/jmeter-plug-in). The sampler has meanwhile the following features: 
- SSL / StartTLS / "Plaintext" - sending-support for SMTP
- Multiple recipients, including "To", "CC" and "BCC"
- Multiple attachments
- and more

We furthermore think about an option to be able to send an ".eml"-file via the sampler, but this could at most be an additional feature for a future version. 

Additionally, we have not implemented any cryptographic functionality by now. It would certainly be a valuable enhancement to implement e.g. BouncyCastle X.509 or PGP-support. 

- Do not use this sampler to send mass e-mails!
- Have a look at the license-file (can be found under /src/)
Comment 1 Michael Tschannen 2008-05-21 01:35:24 UTC
Please don't get frightened because of the "license-note" above - naturally, we are developing all our samplers (including this one) under apache license as all JMeter-parts!
Comment 2 Armin Häberling 2008-06-02 06:12:53 UTC
Created attachment 22055 [details]
Source code for JMeter SMTP-Sampler (with enhancements)

Some enhancements and fixes for the SMTP-Sampler code:

- adds support for sending eml files
- adds a log of the smtp session to the sampler result
- jmeter.smtpsampler.protocol.SendMailCommand.execute() used PipedInputStreams in a way that could lead to a deadlock. This was fixed.
Comment 3 Ralf Hauser 2008-06-02 06:15:48 UTC
... and no longer only compiles/runs with java6
Comment 4 Ralf Hauser 2008-08-06 05:22:27 UTC
see also bug 25355
Comment 5 Luciana Moreira 2010-05-21 04:53:43 UTC
There has been no comments on this Bug for almost 2 years. I would like to see the proposed changes in JMeter.

I have tested it with SVN Head revision 946591 and it works as expected. I have moved the packages to a path similar to the Mail Reader Sampler classes, so it should be simple to add this to the current version of JMeter.
Comment 6 Luciana Moreira 2010-05-21 04:57:00 UTC
Created attachment 25469 [details]
Source code for SMTP sampler
Comment 7 Sebb 2010-05-21 05:12:39 UTC
There is a slight problem with the file headers - they contain Copyright lines which need to be removed/moved:


So we need permission from Luca Maragnani & Michael Tschannen (or their agents) to change the source files accordingly.
Comment 8 Michael Tschannen 2010-05-21 05:20:20 UTC
Permission granted (I don't work on this project anymore but feel free to use my part of the source code), if you need to remove the copyright statement please do so.
Comment 9 Luciana Moreira 2010-05-21 05:56:13 UTC
E-mail correspondence from Luca Maragnani:

> From:  	Luca Maragnani (luca.maragnani@gmail.com)
> Sent: 	Friday, October 02, 2009 7:21:14 AM
> Hi Kevin,
> I'm glad that the plugin is of your interest: certainly you can remove the
copyright statement in order to include the code into jmeter source tree.
> Bye,
> Luca
Comment 10 Sebb 2010-05-21 06:02:59 UTC
Thanks for the very prompt responses  - it will take a bit longer (!) to add the code to SVN and test it, but hopefully there will be some progress next week.
Comment 11 Luciana Moreira 2010-05-21 06:19:22 UTC
Created attachment 25470 [details]
Source code for SMTP sampler witho Apache license

I have removed the install certificate feature since it contained SUN Microsystems copyright. We may opt to integrate it once more in the future with an alternative code that does not have license problems.
Comment 12 Ralf Hauser 2010-05-25 07:40:43 UTC
see also bug 38387 comment 8 for how to be robust against absent bc*.jar
Comment 13 Luciana Moreira 2010-05-25 08:55:51 UTC
Created attachment 25482 [details]
This patch contains a SMTPSampler with a graceful failure presentation at the GUI level when the bouncy castle jar is not present.
Comment 14 Luciana Moreira 2010-05-31 07:59:21 UTC
Sebb, do we have progress on this bug? I have some time that I can invest on any required changes on this code.

Comment 15 Sebb 2010-06-02 13:44:35 UTC
Latest patch looks mostly OK.

Not sure why the sampler has the "Check for failure" option. This seems out of place for a sample.

There a few other tweaks that need to be made, but these can be done later.

As for Bug 38387, it would be very helpful to have a patch for component_reference.xml and some unit tests, though that may be quite hard.
Comment 16 Luciana Moreira 2010-06-03 02:42:01 UTC
Could you point an existing junit test that I could use as example for the new tests?
Comment 17 Sebb 2010-06-28 14:06:55 UTC
Any chance you could provide a short description for compononent_reference.xml?

I can do the screnshot, but having the text would be very useful.
Comment 18 Luciana Moreira 2010-06-29 10:22:09 UTC
Will be working on it today. Sorry for delay, got flooded with work the last weeks.
Comment 19 Luciana Moreira 2010-06-29 12:10:39 UTC
Created attachment 25658 [details]
Comment 20 Luciana Moreira 2010-06-29 12:14:16 UTC
Created attachment 25659 [details]
SMTP Documentation Patch
Comment 21 Luciana Moreira 2010-06-29 12:17:56 UTC
Documentation patch is done. If need anything else let me know. I believe that the original author is also willing to put some time into this patch:

>Hi Luciana,
>I confirm you can remove the copyright note in order to include the source
>code into JMeter distribution.

>Since there were many tentative to achieve this result, don't hesitate to
>contact me in case of any problem. I would be really glad to give a little
>contribute to the project.


I will call his attention to this rfe via e-mail once again since he is not registered in the bugzilla platform.
Comment 22 Sebb 2010-06-29 14:42:31 UTC
I've started working on adding the code, however I have come across a serious problem:

The SendMailCommand code currently changes the value of the "javax.net.ssl.trustStore" system property at run-time. This will cause problems when running multiple threads.

Some other way will have to be found to handle this.

Any suggestions?
Comment 23 Ralf Hauser 2010-06-29 16:23:42 UTC
not particularly beautiful, but can avoid side-effects on other takers of the "javax.net.ssl.trustStore" :

Extend java.lang.InheritableThreadLocal and set the variable there.
And obviously in the right place, also take it from there again...
Comment 24 Sebb 2010-06-29 17:16:36 UTC
(In reply to comment #23)
> not particularly beautiful, but can avoid side-effects on other takers of the
> "javax.net.ssl.trustStore" :
> Extend java.lang.InheritableThreadLocal and set the variable there.

Useful to know.

> And obviously in the right place, also take it from there again...

Not sure that's possible. The code does not read the property so I assume it must be read by one of the libraries.
Comment 25 Luciana Moreira 2010-06-29 17:21:25 UTC
The clean approach for this is to have a SocketFactory implementation with built in TrustManager that will check the certificate based on the truststore.

To understand it better look at the class TrustAllSSLSocketFactory written by Luca. We need to do the same idea but accepting only certificates that are n the truststore.
Comment 26 Sebb 2010-07-01 08:08:57 UTC
The SendMailCommand class also calls:


which again will affect all threads.

The code needs to be changed to set the socket factory for the specific sampler only.

Similar code is used in the HttpSamplers, but I'm not sure those classes are directly usable here - they may well be http-specific.

I think the best approach would be to commit the code with the current restrictions and then fix it, because most of it seems to be working.
Comment 27 Sebb 2010-07-01 08:11:22 UTC
Another issue - the code does not seem to need Bouncy Castle, so I removed the code which checks for it.
Comment 28 Sebb 2010-07-01 14:46:35 UTC
(In reply to comment #26)
> The SendMailCommand class also calls:
> Security.setProperty("ssl.SocketFactory.provider",...)
> which again will affect all threads.

Removed, as the prepareMessage() method already sets the appropriate property.
Comment 29 Sebb 2010-07-03 06:28:46 UTC
The cbUseLocalTrustStore tooltip currently just says "Please note:"

I assume that there was supposed to be something else in the message?
Any idea what it was?
Comment 30 Sebb 2010-07-03 06:54:35 UTC
Also, the trust settings are assymetrical.

"Use SSL" can be used with "Trust All" and "Use Local", whereas 
StartTLS disables "Trust All" yet allows "Use Local".

Surely if StartTLS can "Use Local" it should also be able to "Trust All"?
Otherwise maybe it should not use either?
Comment 31 Luciana Moreira 2010-07-05 03:28:57 UTC
I don£t see any reason for this behaviour with the trustAll x Local keystore.

If you look in the code of SendMailCommand, trustAllCerts is independent of start TLS.

        if (useStartTLS) {
            props.put("mail.smtp.starttls.enable", "true");
            //props.put("mail.debug", "true");

        if (trustAllCerts && protocol.equalsIgnoreCase("smtps")) {
            props.setProperty("mail.smtps.socketFactory.fallback", "false");

To me this is a clear GUI bug.
Comment 32 Luciana Moreira 2010-07-05 04:03:27 UTC
Created attachment 25702 [details]
Small patch to fix the described bug on TrustAllCerts. It also fixes the layout to completely fill the space.
Comment 33 Sebb 2010-07-05 06:31:59 UTC
OK, thanks for confirming. Applied to SVN:

URL: http://svn.apache.org/viewvc?rev=960528&view=rev
Bug 45053 - SMTP-Sampler for JMeter
+ Fix trustAllCerts so it applies also to StartTLS option
Comment 34 Sebb 2010-07-06 12:14:13 UTC
Finally worked out how to use properties to control LocalTrustStore for both SSL and StartTLS. Requires JavaMail 1.4.2+

URL: http://svn.apache.org/viewvc?rev=960899&view=rev
New screenshot. SMTP sampler no longer thread-hostile


I think this is all complete now, so will close the bug.

If there are any further issues, please raise a new issue.
Comment 35 Luciana Moreira 2010-07-07 09:26:50 UTC
Hello Sebb,

Sorry to reopen this bug, but I attempted to run some test scripts with the code fresh from svn and I got this error. I tried to connect using local keystore and truts all certificates, both gave me the same error

Thread Name: SigSvc_Pass_Mail 1-1
Sample Start: 2010-07-07 15:20:27 CEST
Load time: 5151
Latency: 0
Size in bytes: -1
Sample Count: 1
Error Count: 1
Response code: 500
Response message: MessagingException: Server certificate not trusted - perhaps you have to restart JMeter!
javax.mail.MessagingException: Can't send command to SMTP host;
  nested exception is:
	javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

Response headers:

SampleResult fields:
DataEncoding: null

I used this script many times before to connect to the same server I am trying now and had no troubles.Any ideas of what could be happening?
Comment 36 Sebb 2010-07-07 10:15:16 UTC
The code now requires JavaMail 1.4.2+ (and comes with JavaMail 1.4.3).

Previously JMeter nightly was using the Geronimo mail implementation - however that does not (yet) support all the mail properties now used by JMeter.

Make sure that you delete the geronimo javamail and activation jars from the lib directory. 

Try also enabling debug - this should show if you are using Geronimo or the JavaMail from Oracle(Sun).
Comment 37 Luciana Moreira 2010-07-07 11:07:33 UTC
Thx a lot, it fixed my problem :)

I will close the bug again.
Comment 38 Sebb 2010-07-07 11:30:49 UTC
OK, no problem, thanks for confirming it's OK.

It was probably because I added a .ssl prefix to the socketFactory.class property.
This was needed to support TrustAll with StartTLS.
Without it, the mail implementation tried to use the SSL socketfactory to create the initial non-SSL socket - and failed.

i.e. TrustAll needs mail.smtp.ssl.socketFactory.class, and will not work with mail.smtp.socketFactory.class

For consistency, for SSL mode I also used the mail.smtps.ssl.socketFactory.class property. This requires Javamail 1.4.2+.

I suppose I could have omitted the .ssl here as smtps always uses SSL sockets - in which case your test case would have worked with SSL+TrustAll on Geronimo.

But Geronimo does not support the "mail.smtps[.ssl].socketFactory" property so it would not have worked with SSL+Local truststore.