Search in sources :

Example 1 with SendMailCommand

use of org.apache.jmeter.protocol.smtp.sampler.protocol.SendMailCommand in project jmeter by apache.

the class SmtpSampler method sample.

/**
     * Performs the sample, and returns the result
     *
     * @param e Standard-method-header from JMeter
     * @return Result of the sample
     * @see org.apache.jmeter.samplers.Sampler#sample(org.apache.jmeter.samplers.Entry)
     */
@Override
public SampleResult sample(Entry e) {
    SendMailCommand sendMailCmd;
    Message message;
    SampleResult result = createSampleResult();
    try {
        sendMailCmd = createSendMailCommandFromProperties();
        message = sendMailCmd.prepareMessage();
        result.setBytes(calculateMessageSize(message));
    } catch (Exception ex) {
        log.warn("Error while preparing message", ex);
        result.setResponseCode("500");
        result.setResponseMessage(ex.toString());
        return result;
    }
    // Set up the sample result details
    result.setDataType(SampleResult.TEXT);
    try {
        result.setRequestHeaders(getRequestHeaders(message));
        result.setSamplerData(getSamplerData(message));
    } catch (MessagingException | IOException ex) {
        result.setSamplerData("Error occurred trying to save request info: " + ex);
        log.warn("Error occurred trying to save request info", ex);
    }
    // Perform the sampling
    result.sampleStart();
    boolean isSuccessful = executeMessage(result, sendMailCmd, message);
    result.sampleEnd();
    try {
        result.setResponseData(processSampler(message));
    } catch (IOException | MessagingException ex) {
        log.warn("Failed to set result response data", ex);
    }
    result.setSuccessful(isSuccessful);
    return result;
}
Also used : Message(javax.mail.Message) MessagingException(javax.mail.MessagingException) SampleResult(org.apache.jmeter.samplers.SampleResult) IOException(java.io.IOException) SendMailCommand(org.apache.jmeter.protocol.smtp.sampler.protocol.SendMailCommand) MessagingException(javax.mail.MessagingException) AuthenticationFailedException(javax.mail.AuthenticationFailedException) AddressException(javax.mail.internet.AddressException) IOException(java.io.IOException)

Example 2 with SendMailCommand

use of org.apache.jmeter.protocol.smtp.sampler.protocol.SendMailCommand in project jmeter by apache.

the class SmtpSampler method createSendMailCommandFromProperties.

private SendMailCommand createSendMailCommandFromProperties() throws AddressException {
    SendMailCommand sendMailCmd = new SendMailCommand();
    sendMailCmd.setSmtpServer(getPropertyAsString(SmtpSampler.SERVER));
    sendMailCmd.setSmtpPort(getPropertyAsString(SmtpSampler.SERVER_PORT));
    sendMailCmd.setConnectionTimeOut(getPropertyAsString(SmtpSampler.SERVER_CONNECTION_TIMEOUT));
    sendMailCmd.setTimeOut(getPropertyAsString(SmtpSampler.SERVER_TIMEOUT));
    sendMailCmd.setUseSSL(getPropertyAsBoolean(SecuritySettingsPanel.USE_SSL));
    sendMailCmd.setUseStartTLS(getPropertyAsBoolean(SecuritySettingsPanel.USE_STARTTLS));
    sendMailCmd.setTrustAllCerts(getPropertyAsBoolean(SecuritySettingsPanel.SSL_TRUST_ALL_CERTS));
    sendMailCmd.setEnforceStartTLS(getPropertyAsBoolean(SecuritySettingsPanel.ENFORCE_STARTTLS));
    sendMailCmd.setUseAuthentication(getPropertyAsBoolean(USE_AUTH));
    sendMailCmd.setUsername(getPropertyAsString(USERNAME));
    sendMailCmd.setPassword(getPropertyAsString(PASSWORD));
    sendMailCmd.setUseLocalTrustStore(getPropertyAsBoolean(SecuritySettingsPanel.USE_LOCAL_TRUSTSTORE));
    sendMailCmd.setTrustStoreToUse(getPropertyAsString(SecuritySettingsPanel.TRUSTSTORE_TO_USE));
    sendMailCmd.setEmlMessage(getPropertyAsString(EML_MESSAGE_TO_SEND));
    sendMailCmd.setUseEmlMessage(getPropertyAsBoolean(USE_EML));
    if (!getPropertyAsBoolean(USE_EML)) {
        // if we are not sending an .eml file
        sendMailCmd.setMailBody(getPropertyAsString(MESSAGE));
        sendMailCmd.setPlainBody(getPropertyAsBoolean(PLAIN_BODY));
        getAttachmentFiles().forEach(sendMailCmd::addAttachment);
    }
    sendMailCmd.setEnableDebug(getPropertyAsBoolean(ENABLE_DEBUG));
    if (getPropertyAsString(MAIL_FROM).matches(".*@.*")) {
        sendMailCmd.setSender(getPropertyAsString(MAIL_FROM));
    }
    // Process address lists
    sendMailCmd.setReceiverTo(getPropAsAddresses(SmtpSampler.RECEIVER_TO));
    sendMailCmd.setReceiverCC(getPropAsAddresses(SmtpSampler.RECEIVER_CC));
    sendMailCmd.setReceiverBCC(getPropAsAddresses(SmtpSampler.RECEIVER_BCC));
    sendMailCmd.setReplyTo(getPropAsAddresses(SmtpSampler.MAIL_REPLYTO));
    sendMailCmd.setSubject(calculateSubject());
    // needed for measuring sending time
    sendMailCmd.setSynchronousMode(true);
    sendMailCmd.setHeaderFields((CollectionProperty) getProperty(SmtpSampler.HEADER_FIELDS));
    return sendMailCmd;
}
Also used : SendMailCommand(org.apache.jmeter.protocol.smtp.sampler.protocol.SendMailCommand)

Aggregations

SendMailCommand (org.apache.jmeter.protocol.smtp.sampler.protocol.SendMailCommand)2 IOException (java.io.IOException)1 AuthenticationFailedException (javax.mail.AuthenticationFailedException)1 Message (javax.mail.Message)1 MessagingException (javax.mail.MessagingException)1 AddressException (javax.mail.internet.AddressException)1 SampleResult (org.apache.jmeter.samplers.SampleResult)1