HOW TO: Send Email using JavaBeans

Find out how to get things done with Servoy. Post how YOU get things done with Servoy

HOW TO: Send Email using JavaBeans

Postby mattman » Wed Feb 18, 2004 9:37 am

Recently, I've been playing with using beans to send email from within Servoy. There are a number of advantages to using a bean to send email. One of the immediate advantages are the number of additional features you can have with a bean over the default email sending provided by Servoy.

These advantages include being able to set various values such as some header fields like the X-Mailer, Content-Type and Message-Id.

There are two beans that I have played with so far. The first one has been mentioned on this forum and is a one-part sequence for sending. It's a "quick-and-simple" bean called EZSMTP and can be purchased for $35.00. You can find it here. (NOTE: As of June 2004 it seems that EZJavabeans has vanished off the web, I suggest using the ElegantJ beans anyway because of their feature set)

http://www.ezjavabeans.com/

The other email bean I have worked with is from ElegantJBeans. The bean is the SMTPClient bean and it's broken down into two parts for sending - meaning you need two beans (derived from the one smtpclient.jar file) added to your form . It's included as part of the Networking collection (which is quite cool because you get other beans for FTP, HTTP, POP3, SMTP, MIME, Server, Server Socket, Datagram packet & Datagram socket). However, the price for the ElegantJBeans Networking beans is $149.00. You can find them here.

http://www.elegantjbeans.com/network/

The following code below will get you started for sending email with either of these SMTP bean solutions.

EZSMTP ADVANTAGES
- You can set message priority
- You can set the HELO command (controlling who the server thinks is sending)
- Supports HTML content
- Control over the timeout setting for server connections
- Single bean required

ELEGANT SMTPClient ADVANTAGES
- Ability to set X-Mailer header
- Ability to set Organization header
- Ability to change Date header
- Ability to set Message-Id header
- Ability to set Mime version header
- Full Content-Type control
- You can set the encoding for emails (Content-Transfer-Encoding)
- Base 64 encoding function
- Save sent messages (although you could do this with a method)
- read/write external files for sending

MISSING FEATURES
- Ability to control any header (Reply-To and others)

EZSMTP CODE

NOTE: You'll need to change the ".bean_337" to whatever id is assigned to the bean you add to your form!

Code: Select all
var m = elements.bean_337         //Create an object within a variable
m.authLogin = true            //Set true/false if authorization is required
m.username = 'username';         //Set Username
m.password = '******';            //Set login password
m.mailServer = 'mail.server.com';      //Set the mail server
m.multipleSend = false;            //Set true for keeping a connection open
m.subject = 'This is the subject line';      //Set the subject of the email
m.from = 'from@domain.com';         //Set the from for the email
m.to = 'to@domain.com';            //Set the to address(es)
//m.cc = 'cc@domain.com';         //Optional cc
//m.bcc = 'bcc@domain.com';         //Optional bcc
m.body = 'This is the email body';      //Set the body of the email
m.htmlContent = false;            //Set the content to HTML or plain
m.sendMail()               //Send the email


ELEGANT J BEANS SMTPClient CODE

NOTE: You'll need to change the ".bean_51" and ".bean_166" to whatever id is assigned to the beans you add to your form! There are two beans that need to be added to your form.

Code: Select all
/* The first step is to create a MIME compliant message */

var m = elements.bean_51;         //First we need to create the MIME email
m.from = 'from@domain.com';         //Set the FROM property
m.to = 'to@domain.com';            //Set the TO property (Use comma for multiples)
m.subject = 'Subject line is elegant';      //Set the SUBJECT property
m.body = 'This is the body';         //Set the BODY property
//m.cc = 'cc@domain.com';         //Set the CC property
//m.bcc = 'bcc@domain.com';         //Set the BCC property
m.organization = 'My Company';         //Optional ORGANIZATION header field
m.XMailer = 'My Mailer';         //Optional XMailer header field
m.contentType = 'text/plain; charset="use-ascii"; format=flowed'; //See RFC 2822

/* The second step is to open a connection to the SMTP server and send */

var s = elements.bean_166;
s.authentication = true;
s.hostName = 'mail.server.com';
s.userName = 'username';
s.password = '*******';
s.openConnection();
s.sendMail(m);               //We pass the message object from var m into the send

globals.testing = elements.bean_166.getStatus();//Capture the reply from the server into a global

elements.bean_166.closeConnection();      //Close the connection to the server

Closing comments: There are a number of features in either bean that I would like to see consolidated into one "comprehensive bean". The ability to control the HELO and any header field would be desirable. I also found that setting some of the properties in the ElegantJBean stay persistant. Meaning if you set the BCC field for one record but then do another send, you need to make sure and clear this value by setting it to an empty value - otherwise you might BCC someone on an email you didn't intend for them.

Getting a server response is much easier from the ElegantJBean as you can use the .getStatus() function. Without knowing how to implement a java listener into Servoy, getting a reply from the server is much harder with the EZSMTP bean. Maybe someone with more bean knowledge will know how to return the results of a listener into a field.

Hope this information makes it easier for you to learn about working with beans and sending email!
Last edited by mattman on Wed Jun 09, 2004 9:53 pm, edited 1 time in total.
Matt Petrowsky
mattman
 
Posts: 160
Joined: Wed Aug 06, 2003 8:23 am
Location: Murrieta, CA

Postby IT2Be » Wed Feb 18, 2004 11:00 am

Matt, thanks for this insight into these beans. However I would like to add the following for completeness sake:

When you define
Code: Select all
var m = elements.bean_foo;
You actually create a new instance of that bean so it is only logical that the values remain. If you send another mail it is best to set to delete this instance and create a new one so you would do something like
Code: Select all
var m = null;
var m = elements.bean_foo;
.

Cheers,

Marcel
Marcel J.G. Trapman (IT2BE)
SAN partner - Freelance Java and Servoy
Servoy Components - IT2BE Plug-ins and Beans for Servoy
ServoyForge - Open Source Components for Servoy
User avatar
IT2Be
Servoy Expert
 
Posts: 4766
Joined: Tue Oct 14, 2003 7:09 pm
Location: Germany

Postby jcompagner » Wed Feb 18, 2004 11:34 am

You actually create a new instance of that bean so it is only logical that the values remain. If you send another mail it is best to set to delete this instance and create a new one so you would do something like

this doesn't matter..

You never create a new instance what ever you do with the examples above. You are only assigning an already existing instance of the bean to a javascript variable. (just another pointer to the same object)
so doing

Code: Select all
var m =null;


you just clear the pointer to the object/bean. Not the object/bean itself.

with

Code: Select all
var m = elements.bean_foo;


you just set the pointer back to the same object/bean. It will NOT create a new object. Thats why the values remains. Because that bean doesn't clear its internal values after send (maybe the do this on purpose, can be handy in some situations!)
Johan Compagner
Servoy
User avatar
jcompagner
 
Posts: 8829
Joined: Tue May 27, 2003 7:26 pm
Location: The Internet

Postby IT2Be » Wed Feb 18, 2004 11:42 am

OK, you overclassed me (again :oops:). So what would you do then to clear all values?
Marcel J.G. Trapman (IT2BE)
SAN partner - Freelance Java and Servoy
Servoy Components - IT2BE Plug-ins and Beans for Servoy
ServoyForge - Open Source Components for Servoy
User avatar
IT2Be
Servoy Expert
 
Posts: 4766
Joined: Tue Oct 14, 2003 7:09 pm
Location: Germany

Postby jcompagner » Wed Feb 18, 2004 12:37 pm

reset them youre self.
there is no other choice.
just make sure that you always set the variables (even if they are null or "") before sending the mail.
Johan Compagner
Servoy
User avatar
jcompagner
 
Posts: 8829
Joined: Tue May 27, 2003 7:26 pm
Location: The Internet

Postby patrick » Wed Feb 18, 2004 3:18 pm

If have played around with the ezbeans myself, but found some rather annoying shortcomings (no possibility to get cc, bcc addresses when receiving mail for example). I am not only interested in sending mail, but also retrieving.

There is my first problem: when installing the elegantJbeans, I don't see the pop3client in Servoy. Anyone else with this problem?

There is a third email bean possibilty, that on is free also: http://www.alphaworks.ibm.com/ab.nsf/bean/POP3

They also have a SMTP bean and other interesting stuff.

The problem I have with these beans is that they (for me) are a little hard to implement. You have to able to pass events from one bean to another and so forth. If someone could make an example for these, that'd be great!
patrick
 
Posts: 3703
Joined: Wed Jun 11, 2003 10:33 am
Location: Munich, Germany

Postby mattman » Wed Feb 18, 2004 8:51 pm

patrick wrote:There is a third email bean possibilty, that on is free also: http://www.alphaworks.ibm.com/ab.nsf/bean/POP3

They also have a SMTP bean and other interesting stuff.

The problem I have with these beans is that they (for me) are a little hard to implement. You have to able to pass events from one bean to another and so forth. If someone could make an example for these, that'd be great!


Yeah, the IBM SMTP bean is actually a three part bean. Setting it up is going to be a bit more complex and you have to have all three beans on your form. (You might be able to get away with not using the MIME Encoder bean).

The docs for these beans are REALLY good. I'll try working on the SMTP example then working on the POP example after that.

Does anyone know if you can use the Alpha beans in a commercial solution without having to pay licensing?
Matt Petrowsky
mattman
 
Posts: 160
Joined: Wed Aug 06, 2003 8:23 am
Location: Murrieta, CA

Postby patrick » Wed Feb 18, 2004 11:37 pm

as far as I know, no licence. If you have found a solution, let me know :wink: I am currently busy on other areas but have already played with this a day or so with no final results.
patrick
 
Posts: 3703
Joined: Wed Jun 11, 2003 10:33 am
Location: Munich, Germany

Postby mattman » Thu Feb 19, 2004 8:25 am

mattman wrote:
patrick wrote:There is a third email bean possibilty, that on is free also: http://www.alphaworks.ibm.com/ab.nsf/bean/POP3

They also have a SMTP bean and other interesting stuff.

The problem I have with these beans is that they (for me) are a little hard to implement. You have to able to pass events from one bean to another and so forth. If someone could make an example for these, that'd be great!


Yeah, the IBM SMTP bean is actually a three part bean. Setting it up is going to be a bit more complex and you have to have all three beans on your form. (You might be able to get away with not using the MIME Encoder bean).

The docs for these beans are REALLY good. I'll try working on the SMTP example then working on the POP example after that.

Does anyone know if you can use the Alpha beans in a commercial solution without having to pay licensing?


Quoting myself on this one. I just got the Alphaworks SMTP bean to work. I'm providing my working code and some notes...

Code: Select all
var smtp = elements.bean_428; //Points to the SMTPUISupport bean
var mime = elements.bean_928; //Points to the MIMEEncoder bean
var core = elements.bean_426; //Points to the CoreProtocolBean

smtp.setCoreProtocolBean(core); //You need to reference this bean because the SMTP bean needs it.
smtp.setMimeEncoderBean(mime); //This can be used to encode a message, but may not be necessary.

smtp.setSmtpHostName('mail.server.com');

smtp.setName('Matt Petrowsky'); //Sender name
smtp.setFromId('name@domain.com'); //Sender email
smtp.setTo('matt@isoproductions.com');
smtp.setReplyTo('"The Editor" <editor@domain.com>'); //You can also use more modern formats for email that include names.
//smtp.setCc(''); //You've gotta clear this because it persists
//smtp.setBcc(''); //You've gotta clear this because it persists

smtp.setOrganization('My Org');
smtp.setPriority('Normal'); //"Highest", "High", "Normal", "Low", "Lowest"

smtp.setSubject('The subject goes here');
smtp.setMessageBody('This works without authentication. The SMTP server won\'t show a SMTP login');

smtp.configureServerOptions();
application.output(smtp.getStatus()); //Testing
smtp.configureUserOptions();
application.output(smtp.getStatus()); //Testing
smtp.sendMessage();
application.output(smtp.getStatus()); //Testing


NOTES: This bean is interesting. It does provide the Reply-To header, which is something I use, and it also doesn't use authentication - that I can see. In these days of prying eyes, it would be nice to see SSL support for these email beans.

You can't beat free if distribution is allowed and if you need those few additional header controls then the ElegantJBean may be better. But it will cost you.

When looking at the documentation for this bean it's obvious it was created more for a Java based frontend, although it functions faceless in the Servoy environment (a Java frontend :wink: ) There are a lot of feedback mechanisms that are integrated into Listeners. Which brings me to ask the question.

Hey, Servoy developers! How can I implement a listener so I can get feedback into fields? Can this be done through the JavaScript implementation?

Hope this helps those of you working with email. I'll look into figuring out the POP version of the bean. We should actually have a "figure out the bean contest". It's sort of like going on a treasure hunt trying to figure out how to make them work in Servoy. :D
Matt Petrowsky
mattman
 
Posts: 160
Joined: Wed Aug 06, 2003 8:23 am
Location: Murrieta, CA

EZSMTP

Postby rainer / dsp » Fri Feb 20, 2004 9:42 pm

I just tried the EZSMTP bean and it works fine.
The only problem is, that i get every email twice !

Any trick to stop this or a workaround ?!

Rainer
DSP Memory Distribution GmbH
Philipp-Reis-Str..11
24941 Flensburg / Germany
www.dsp-memory.de
rainer / dsp
 
Posts: 103
Joined: Sat Nov 22, 2003 1:37 am

Postby mattman » Wed Jun 09, 2004 9:47 pm

patrick wrote:The problem I have with these beans is that they (for me) are a little hard to implement. You have to able to pass events from one bean to another and so forth. If someone could make an example for these, that'd be great!


Understanding how to reference and provide beans the values they want/need means you need to understand how Servoy passes parameters from one Method to another.

I am now woking with the ElegantJ beans in order to send email. Their performance is great.

I've attached a PDF I created for them that outlines how to use their SMTP beans.

Their site is here

http://www.elegantjbeans.com/network/
Attachments
ElegantJ_Servoy_SMTP.pdf.zip
Zipped PDF that shows how to install and use the ElegantJ SMTP Networking beans.
(501.82 KiB) Downloaded 504 times
Matt Petrowsky
mattman
 
Posts: 160
Joined: Wed Aug 06, 2003 8:23 am
Location: Murrieta, CA

Postby faheemhameed » Mon Nov 15, 2004 10:57 am

Hi Matt,

How can I set content type to html?
Is the following correct?

msg.contentType = 'text/html; charset=us-ascii';

This seems working. But if I set the attachments like the following:

msg.attachments = "c:\temp\test.jpg";

then the message is sent as plain text. Any ideas please??

Thanks
Hameed
Pilot simple software
Hong Kong
User avatar
faheemhameed
 
Posts: 763
Joined: Wed Sep 10, 2003 7:23 am


Return to How To

Who is online

Users browsing this forum: No registered users and 3 guests