7.1: Example of smtp connector expressions with variables

1
0
-1

Hello everybody:

I am trying to configure a connector out: smtp email subject/message body with variables.
So far, I have seen videos but they all use a constant and none show how to pull parameters, scripts, or variables to compose a complex email message.

Can you share a video, or examples of implementations where you configure email messages other than hard coded constants?

1 answer

1
0
-1
This one is the BEST answer!

I build all my emails using the groovy expression builder as follows:

  1. Stringbuilder email = new Stringbuilder();
  2. email.append("Hello,");
  3. email.append(myvariablename);
  4. email.append(etc...);
  5.  
  6. return email.toString();

that should do what you want, no need for a video :)

Comments

Submitted by dijifo on Sun, 10/04/2015 - 19:40

I Sean,

This is what I got when trying to test your script...

java.lang.reflect.InvocationTargetException
org.bonitasoft.engine.bpm.connector.ConnectorExecutionException: USERNAME=install | org.bonitasoft.engine.core.connector.exception.SConnectorException: org.bonitasoft.engine.expression.exception.SExpressionEvaluationException: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
BScript39.groovy: 1: unable to resolve class Stringbuilder
@ line 1, column 15.
Stringbuilder email = new Stringbuilder();
^

BScript39.groovy: 1: unable to resolve class Stringbuilder
@ line 1, column 23.
Stringbuilder email = new Stringbuilder();
^

2 errors

any ideas?

Submitted by Sean McP on Sun, 10/04/2015 - 20:59

Before I get to my update you should know I work for my self. I do not work for Bonitasoft and my time is as precious to me as yours is to you. I am giving my free time to helping where I can...so please take this rant with a pinch of salt.

Honestly? Does no one look these thing up themselves any more? I had to learn this myself a year ago...

I was not expecting you to just copy it and it would work. I would have hoped for a little more ingenuity and research the actual code.

If you had of taken one minute out rather than wasting the 75 minutes until my reply and researched Stringbuilder you would have found Stringbuilder is actually spelt StringBuilder.

That's all your problem is. I was on a train at the time, I don't always spell things right, but people not wanting to learn and just hoping everything will work just like that really annoys me...

Please help yourself, try to research things when they go wrong, it's the way to learn. Don't just rely on others.

Again apologies, best regards and I hope you fix it...

Here is a good link as to how to use StringBuilder...

http://www.dotnetperls.com/stringbuilder-java

Submitted by dijifo on Sun, 10/04/2015 - 23:38

Hi Sean,
Apologies for the time spent on this question. Since I'm not a developer, I just spent the last 24 hours learning that Stringbuilder is not the same as StringBuilder. Thanks to your recent answer, I corrected the syntax, and now I get the email. Yeah!!!

StringBuilder email = new StringBuilder();
String newLine = System.getProperty("line.separator");

email.append("Hello,");
email.append("This is line one." + newLine + "This is line two." + "\r" + "This is line three.");
email.append("Today's Date,");
email.append(new Date());
email.append("This is line four." + '\r' + "This is line five." + '\n'+ "This is line six.");
return email.toString();

email received:
Hello,This is line one. This is line two. This is line three.Today's Date,Sun Oct 04 17:36:42 EDT 2015This is line four. This is line five. This is line six.

If I may ask one more stupid question related to this question, (believe me, I have tried to research it and can't find the solution):

How can I get a new line to show in my email? I have tried String newLine = System.getProperty("line.separator"); and also /r, /n, /r/n and %r \r, \n, \r\n and %r but nothing creates a new line in the email message. every attempt produces an email with one liner...???

Submitted by Sean McP on Mon, 10/05/2015 - 09:47

Not a problem, thanks for your understanding...

this is when i like to help, you have shown you've tried something and now require help. A year ago I didn't know Java, HTML, CSS etc. But I've learnt... :) I'm not the best by any means but I can get it done.

there are some very cheap, even free courses out there, which is the best I cannot say.

For Java/Groovy have a look at http://www.javacodegeeks.com/. Disclosure - I use them, I'm not affiliated with them.

Right, good to see you've tried all the normal stuff:

\n would work when writing as a Java Stream but in the case of the email you are writing a String and not print output so what you need is an HTML Stream (you have checked allow HTML in the Connector haven't you?)

So your new lines should in fact be:

  1. StringBuilder email = new StringBuilder();
  2. String newLine = "</br>"; // HTML Line Break
  3.  
  4. email.append("Hello," + newLine + newLine);
  5. email.append("This is line one." + newLine + "This is line two." + newLine + "This is line three.");

That will work,

We use this type of build a lot so well worth learning a bit of HTML as well.

One other thing to learn clearly is the concept of Java Lists and Lists of Lists...will work wonders for developing solutions.

regards

Submitted by dijifo on Mon, 10/05/2015 - 14:05

Thanks Sean. It worked as expected...

import org.bonitasoft.engine.bpm.process.ProcessInstance;
StringBuilder email = new StringBuilder();
String newLine = ""; // HTML Line Break

email.append("Hello,");
email.append("This is line one." + newLine + "This is line two." + newLine + "This is line three.");
email.append(newLine);
email.append("Created Date: " + new Date());

return email.toString();

Submitted by Sean McP on Mon, 10/05/2015 - 15:29

Glad to be of help, now you can mark this one as successfully resolved,

Points make prizes they tell me, I just don't know what though :)

Notifications