Send Mail using the Mail plugin over SSL

Hi All,

I need to send emails over the Gmail SMTP server (smtp.gmail.com)

I checked the requirements to send email using gmail smtp server which are as follows:
Outgoing Mail (SMTP) Server - requires TLS: smtp.gmail.com (use authentication)
Use Authentication: Yes
Use STARTTLS: Yes (some clients call this SSL)
Port: 465 or 587

Account Name: your full email address (including @gmail.com or @your_domain.com)
Email Address: your email address (username@gmail.com or username@your_domain.com)
Password: your Gmail password

The mail plugin in servoy sample code shows that we need to build the properties array:
var properties = new Array()
//properties[0] = ‘mail.smtp.host=myserver.com
//properties specification can be found at:http://java.sun.com/products/javamail/javadocs/com/sun/mail/smtp/package-summary.html
//var msgs = plugins.mail.sendMail(‘to_someone@example.com,to_someone_else@example.net’, ‘John Cobb from_me@example.com’, ‘subject’, //msgText,null,'unnamed@example.com’,null, properties);

I checked the http://java.sun.com/products/javamail/j … mmary.html to build the array
var properties = new Array()
properties[0] = ‘mail.smtp.host=smtp.gmail.com
properties[1] = ‘mail.smtp.port=465’
properties[2] = ‘mail.smtp.auth=true’
properties[3] = ‘mail.smtp.starttls.enable=true’
properties[4] = ‘mail.smtp.user=james.dcunha@gmail.com’

Its throwing me errors when i use the above as my properties array.

what would be the key to enter for password and do I need to add any values in the properties array?

Down below is the code:
var properties = new Array()
properties[0] = ‘mail.smtp.host=smtp.gmail.com
properties[1] = ‘mail.smtp.port=465’
properties[2] = ‘mail.smtp.auth=true’
properties[3] = ‘mail.smtp.starttls.enable=true’
properties[4] = ‘mail.smtp.user=john.doe@gmail.com’

var msgText = ‘test message’;
var success = plugins.mail.sendMail(‘john.doe@gmail.com’, ‘johndoe@hotmail.com’, ‘subject’, msgText,null,null,properties);
if (!success)
{
plugins.dialogs.showWarningDialog(‘Alert’,‘Failed to send mail’,‘OK’);
}

Any help would be appreciated

Regards
James Dcunha

Hi,

I added the following into the Properties array

var properties = new Array()
properties[0] = ‘mail.smtp.host=smtp.gmail.com
properties[1] = ‘mail.smtp.port=465’
properties[1] = ‘mail.smtp.auth=true’
properties[2] = ‘mail.smtp.starttls.enable=true’
properties[3] = ‘mail.smtp.user=john.doe@gmail.com’
properties[4] = ‘mail.transport.protocol = smtps’;
properties[5] = ‘mail.smtp.starttls.enable = true’;
properties[6] = ‘mail.smtp.socketFactory.port = 465’;
properties[7] = ‘mail.smtp.socketFactory.class = javax.net.ssl.SSLSocketFactory’;
properties[8] = ‘mail.smtp.socketFactory.fallback = false’;

var msgText = ‘test message’;
var success = plugins.mail.sendMail(‘john.doe@hotmail.com’, ‘john.doe@gmail.com’, ‘subject’, msgText,null,null,properties);
if (!success)
{
plugins.dialogs.showWarningDialog(‘Alert’,‘Failed to send mail’,‘OK’);
}

But it didn’t work. Just wanted to share this information with you. Any ideas on the correct way to build the properties array for SSL.

Regards
James Dcunha

Hi James,

I had the exact same question and found this at the forum:

var $properties = new Array();
	$properties[0] = 'mail.smtp.host=smtp.gmail.com';
	$properties[1] = 'mail.smtp.auth=true';
	$properties[2] = 'mail.smtp.starttls.enable=true';
	$properties[3] = 'mail.smtp.port=587';
	$properties[4] = 'mail.smtp.username=<<FULL GMAIL ADDRESS>>';
	$properties[5] = 'mail.smtp.password=<<YOUR PASSWORD>>';

This works for me… I foun this at Send Mail requires SMTP Auth - Classic Servoy - Servoy Community

Good luck!

Hi Marc,

Thanks for helping me out.

I tried the following but it didn’t work, I thought the issue could be a port number so i changed it to 465 and it didn’t work. I checked the server log files

org.mozilla.javascript.WrappedException: Wrapped java.lang.ArrayStoreException (C:\Documents and Settings\Servoy\servoy_workspace\dennis\globals.js#725) at org.mozilla.javascript.Context.throwAsScriptRuntimeEx(Context.java:1828) at org.mozilla.javascript.MemberBox.invoke(MemberBox.java:199) at org.mozilla.javascript.NativeJavaMethod.call(NativeJavaMethod.java:347) at org.mozilla.javascript.Interpreter.interpretLoop(Interpreter.java:3666) at script.send_mail_test(C:\Documents and Settings\Servoy\servoy_workspace\dennis\globals.js:725) at org.mozilla.javascript.Interpreter.interpret(Interpreter.java:2680) at org.mozilla.javascript.InterpretedFunction.call(InterpretedFunction.java:166) at org.mozilla.javascript.ContextFactory.doTopCall(ContextFactory.java:387) at org.mozilla.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:3122) at org.mozilla.javascript.InterpretedFunction.call(InterpretedFunction.java:165) at com.servoy.j2db.scripting.RemoteDebugScriptEngine.executeFunction(RemoteDebugScriptEngine.java:109) at com.servoy.j2db.scripting.ScriptMenuItem$1.run(ScriptMenuItem.java:9) at java.awt.event.InvocationEvent.dispatch(Unknown Source) at java.awt.EventQueue.dispatchEvent(Unknown Source) at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.run(Unknown Source) Caused by: java.lang.ArrayStoreException at java.lang.System.arraycopy(Native Method) at com.servoy.extensions.plugins.mail.MailProvider.js_sendMail(MailProvider.java:227) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.mozilla.javascript.MemberBox.invoke(MemberBox.java:179) … 17 more

My revised script is as follows:

function send_mail_test()
{
	var properties = new Array();
       properties[0] = 'mail.smtp.host=smtp.gmail.com';
       properties[1] = 'mail.smtp.auth=true';
       properties[2] = 'mail.smtp.starttls.enable=true';
       properties[3] = 'mail.smtp.port=465';
       properties[4] = 'mail.smtp.username=<<MY EMAIL ADDRESS>>';
       properties[5] = 'mail.smtp.password=<<MY PASSWORD>>';

	var msgText = 'test message';
	var success = plugins.mail.sendMail('james.dcunha@gmail.com', 'jamesd_cunha@hotmail.com', 'subject', msgText,null,null,properties);
	if (!success) 
	{
		plugins.dialogs.showWarningDialog('Alert','Failed to send mail','OK');
	}
}

I changed the port to 587 and it didn’t work either.

I might have to check the plugin source code to see where could be the issue. But any help would be appreciated

Regards
James Dcunha
The Support Group

jdcunha:
properties[4] = ‘mail.smtp.username=<>’;
properties[5] = ‘mail.smtp.password=<>’;

just to check as it seems your array not being ok: I hope you replaced the <<MY…>> strings by your gmail email address and password… :?

jdcunha:
Wrapped java.lang.ArrayStoreException (C:\Documents and Settings\Servoy\servoy_workspace\dennis\globals.js#725

Anyway, what the content on line 725 of your code?

Hi Marc,

Thanks for helping me out

I did replace it with my email address and password.

line# 725 corresponds to:
var success = plugins.mail.sendMail(‘james.dcunha@gmail.com’, ‘jamesd_cunha@hotmail.com’, ‘subject’, msgText,null,null,properties);

It seems to me I have declared and set the properties array correctly.

I will again investigate the code. But a second set of eyes always helps.

function send_mail_test()
{
	//it is possbile to set all kind of smtp properties
	var properties = new Array();
       properties[0] = 'mail.smtp.host=smtp.gmail.com';
       properties[1] = 'mail.smtp.auth=true';
       properties[2] = 'mail.smtp.starttls.enable=true';
       properties[3] = 'mail.smtp.port=465';
       properties[4] = 'mail.smtp.username=james.dcunha@gmail.com';
       properties[5] = 'mail.smtp.password=notmyrealpassword';
  
	var msgText = 'test message';
	var success = plugins.mail.sendMail('jamesd_cunha@hotmail.com', 'james.dcunha@gmail.com', 'subject', msgText,null,null,properties); //this corresponds to line 725
	if (!success) 
	{
		plugins.dialogs.showWarningDialog('Alert','Failed to send mail','OK');
	}
	
}

The interesting this I noticed is on executing line 725 the function terminates and doesn’t proceed ahead with the if(!success) control block.

Any help would be appreciated.

Regards
James Dcunha

You seem to be missing a parameter.

plugins.mail.sendMail( to,  from,  subject,  msgText,  [cc],  [bcc],  [attachment/attachments array],  [overridePreferenceSMTPHost/properties array])

There should be 8 instead of 7.

Hi all,

Thanks Joas for pointing that out. Second set of eyes do help a lot.

Now for some reason on executing that step, the application hangs.

I am going to check with my network admin to see if the outgoing mail configurations are correct.

Is there something else I need to be aware of.

	var properties = new Array();
       properties[0] = 'mail.smtp.host=smtp.gmail.com';
       properties[1] = 'mail.smtp.port=465';
       properties[2] = 'mail.smtp.auth=true';
       properties[3] = 'mail.smtp.username=james.dcunha@gmail.com';
       properties[4] = 'mail.smtp.password=mypasswordhere';
       properties[5] = 'mail.smtp.starttls.enable=true';
       
        var msgText = 'test message';
	var success = plugins.mail.sendMail('jamesd_cunha@hotmail.com', 'james.dcunha@gmail.com', 'subject', msgText,null,null,null,properties);
	if (!success) 
	{
		plugins.dialogs.showWarningDialog('Alert','Failed to send mail','OK');
	}

Thanks for all the help

Regards
James

Hi,

Thanks for helping me out with this issue.

Once I switched the port # from 465 to 587 it started working fine.

Regards
James Dcunha

Hi guys,
I know this thread is from a while back, but it seems like the closest ot my issue…
I have a client that needs to do emailing thru an SMTP server that requires TLS encryption (not SSL).
Do I need to create a (self-signed?) certificate?
If so, where/how would I store that certificate so that the mail plugin will use it when I say
‘mail.smtp.starttls.enable=true’

I have been trying to do test email thru gmail as detailed here, but it’s not working currently (I think the IT dept. may have blocked port 587 for some reason and am waiting to hear back from them…)

Any help (on TLS certification) would be greatly appreciated.

Thanks,
Rafi

Hi Rafig,

I think for TLS communication you will need a certificate signed/issued by a Certificate Authority.

The storage of the certificate has to do with the setup of your SMTP server. Sorry I am not able to help you on this.

Regards
jdcunha

jdcunha:
Hi Rafig,
I think for TLS communication you will need a certificate signed/issued by a Certificate Authority.

Thanks, I will see if I can work out how to create a self-signed one if that might work…

jdcunha:
The storage of the certificate has to do with the setup of your SMTP server. Sorry I am not able to help you on this.
Regards
jdcunha

Thanks.
I hope someone from Servoy might be able to answer that one…

Rafi

Try openssl to selfsigned certificates.

Un saludo. Roberto.

Roberto Blasco:
Try openssl to selfsigned certificates.

Thanks, I will do.

I want to do the same thing, did you ever get this resolved?

Thanks in advance.

BulldogBen:
I want to do the same thing, did you ever get this resolved?

Thanks in advance.

Hi,
didn’t need a certificate in the end, it all just seemed to work.
Here’s my code, hope it helps.

/**
 * @param {JSEvent} event the event that triggered the action
 *
 * @param {Text} send_to email address to send to
 *
 * @param {Text} send_subject email subject
 *
 * @param {Text} send_msg_txt text of email message
 *
 * @param {Text} attachment attachment to email message
 *
 */
function send_mail ( event, send_to, send_subject, send_msg_txt, attachment )
{
	var properties = new Array ( );
	properties[0] = 'mail.smtp.host=your.smpt.host';
	properties[1] = 'mail.smtp.port=123';
	properties[2] = 'mail.smtp.auth=true';
	properties[3] = 'mail.smtp.username=your.user@your.domain';
	properties[4] = 'mail.smtp.password=yourpassword';
	properties[5] = 'mail.smtp.starttls.enable=true';

	if ( send_to == null )
	{
		return; // no send to, so just return
	}

	if ( send_subject == null )
	{
		send_subject = ''; // no subject
	}

	if ( send_msg_txt == null )
	{
		send_msg_txt = ''; // no body
	}

	if ( attachment != null )
	{
		var attachment_array = plugins.mail.createBinaryAttachment ( 'embedded', plugins.file.readFile ( attachment ) );

		var success = plugins.mail.sendMail ( send_to, 'sent.from@your.domain', send_subject, send_msg_txt, null, null, [attachment_array], properties );
	}
	else
	{
		var success = plugins.mail.sendMail ( send_to, 'sent.from@your.domain', send_subject, send_msg_txt, null, null, null, properties );
	}
	
	if ( globals.is_headless != 1 ) // don't try and show an alert when running as headless client
	{
		if ( !success )
		{
			plugins.dialogs.showWarningDialog ( 'Alert', plugins.mail.getLastSendMailExceptionMsg ( ), 'OK' );
		}
		else
		{
			plugins.dialogs.showInfoDialog ( 'Success', 'Email Sent Successfully!', 'OK' );
		}
	}
}

(obviously replacing the bits that need replacing…)

Rafi

I did have it working just find, using similar code to Rafi’s. However, it recently stopped working when on the server (but was fine in Developer)

I have a couple of questions…

  1. Anyone know the most probably cause for this before I start debugging?
  2. Does passing all sending parameters to the sendMail function negate the need to have anything set in server admin?

Thanks

Ben