public SampleResult sample(Entry e) {
Message message = null;
SampleResult res = new SampleResult();
res.setSampleLabel(getName());
boolean isOK = false; // Did sample succeed?
SendMailCommand instance = new SendMailCommand();
instance.setSmtpServer(getPropertyAsString(SmtpSampler.SERVER));
instance.setSmtpPort(getPropertyAsString(SmtpSampler.SERVER_PORT));
instance.setUseSSL(getPropertyAsBoolean(SecuritySettingsPanel.USE_SSL));
instance.setUseStartTLS(getPropertyAsBoolean(SecuritySettingsPanel.USE_STARTTLS));
instance.setTrustAllCerts(getPropertyAsBoolean(SecuritySettingsPanel.SSL_TRUST_ALL_CERTS));
instance.setEnforceStartTLS(getPropertyAsBoolean(SecuritySettingsPanel.ENFORCE_STARTTLS));
instance.setUseAuthentication(getPropertyAsBoolean(USE_AUTH));
instance.setUsername(getPropertyAsString(USERNAME));
instance.setPassword(getPropertyAsString(PASSWORD));
instance.setUseLocalTrustStore(getPropertyAsBoolean(SecuritySettingsPanel.USE_LOCAL_TRUSTSTORE));
instance.setTrustStoreToUse(getPropertyAsString(SecuritySettingsPanel.TRUSTSTORE_TO_USE));
instance.setEmlMessage(getPropertyAsString(EML_MESSAGE_TO_SEND));
instance.setUseEmlMessage(getPropertyAsBoolean(USE_EML));
instance.setEnableDebug(getPropertyAsBoolean(ENABLE_DEBUG));
if (getPropertyAsString(MAIL_FROM).matches(".*@.*")) {
instance.setSender(getPropertyAsString(MAIL_FROM));
}
final String receiverTo = getPropertyAsString(SmtpSampler.RECEIVER_TO).trim();
final String receiverCC = getPropertyAsString(SmtpSampler.RECEIVER_CC).trim();
final String receiverBcc = getPropertyAsString(SmtpSampler.RECEIVER_BCC).trim();
final String replyTo = getPropertyAsString(SmtpSampler.MAIL_REPLYTO).trim();
try {
// Process address lists
instance.setReceiverTo(getPropNameAsAddresses(receiverTo));
instance.setReceiverCC(getPropNameAsAddresses(receiverCC));
instance.setReceiverBCC(getPropNameAsAddresses(receiverBcc));
instance.setReplyTo(getPropNameAsAddresses(replyTo));
if(getPropertyAsBoolean(SUPPRESS_SUBJECT)){
instance.setSubject(null);
}else{
String subject = getPropertyAsString(SUBJECT);
if (getPropertyAsBoolean(INCLUDE_TIMESTAMP)){
StringBuilder sb = new StringBuilder(subject);
sb.append(" <<< current timestamp: ");
sb.append(new Date().getTime());
sb.append(" >>>");
subject = sb.toString();
}
instance.setSubject(subject);
}
if (!getPropertyAsBoolean(USE_EML)) { // part is only needed if we
// don't send an .eml-file
instance.setMailBody(getPropertyAsString(MESSAGE));
instance.setPlainBody(getPropertyAsBoolean(PLAIN_BODY));
final String filesToAttach = getPropertyAsString(ATTACH_FILE);
if (!filesToAttach.equals("")) {
String[] attachments = filesToAttach.split(FILENAME_SEPARATOR);
for (String attachment : attachments) {
File file = new File(attachment);
if(!file.isAbsolute() && !file.exists()){
log.debug("loading file with relative path: " +attachment);
file = new File(FileServer.getFileServer().getBaseDir(), attachment);
log.debug("file path set to: "+attachment);
}
instance.addAttachment(file);
}
}
}
// needed for measuring sending time
instance.setSynchronousMode(true);
instance.setHeaderFields((CollectionProperty)getProperty(SmtpSampler.HEADER_FIELDS));
message = instance.prepareMessage();
if (getPropertyAsBoolean(MESSAGE_SIZE_STATS)) {
// calculate message size
CounterOutputStream cs = new CounterOutputStream();
message.writeTo(cs);
res.setBytes(cs.getCount());
} else {
res.setBytes(-1);
}
} catch (AddressException ex) {
log.warn("Error while preparing message", ex);
res.setResponseCode("500");
res.setResponseMessage(ex.toString());
return res;
} catch (IOException ex) {
log.warn("Error while preparing message", ex);
res.setResponseCode("500");
res.setResponseMessage(ex.toString());
return res;
} catch (MessagingException ex) {
log.warn("Error while preparing message", ex);
res.setResponseCode("500");
res.setResponseMessage(ex.toString());
return res;
}
// Set up the sample result details
res.setDataType(SampleResult.TEXT);
try {
res.setRequestHeaders(getRequestHeaders(message));
res.setSamplerData(getSamplerData(message));
} catch (MessagingException e1) {
res.setSamplerData("Error occurred trying to save request info: "+e1);
log.warn("Error occurred trying to save request info",e1);
} catch (IOException e1) {
res.setSamplerData("Error occurred trying to save request info: "+e1);
log.warn("Error occurred trying to save request info",e1);
}
// Perform the sampling
res.sampleStart();
try {
instance.execute(message);
res.setResponseCodeOK();
/*
* TODO if(instance.getSMTPStatusCode == 250)
* res.setResponseMessage("Message successfully sent!"); else
* res.setResponseMessage(instance.getSMTPStatusCodeIncludingMessage);
*/
res.setResponseMessage("Message successfully sent!\n"
+ instance.getServerResponse());
isOK = true;
}
// username / password incorrect
catch (AuthenticationFailedException afex) {
log.warn("", afex);