Send Mail requires SMTP Auth

Using the excellent Mail Plugin but have a problem because my client is using Earthlink and SendMail failing as Earthlink require SMTP authorisation. Is it possible to add authorisation (username/password)to the SendMail function?

Server.log and .log.txt both show following (even though email address is correct!):

SMTPSend javax.mail.SendFailedException: Invalid Addresses;
nested exception is:
class com.sun.mail.smtp.SMTPAddressFailedException: 550 Please configure your mail client to use authentication.
;
nested exception is:
class com.sun.mail.smtp.SMTPAddressFailedException: 550 Please configure your mail client to use authentication.

Found a suggestion (on a Unix blog) to try

smtpauth.earthlink.net username@earthlink.net:password

but that generates a “SMTPSend javax.mail.MessagingException: Unknown SMTP host:” error.

F1/Help system appears to hint that more can be done - [overridePreferenceSMTPHost/properties array] - but I can’t find any info on how to setup the array.

Thanks in advance

Graham Greensall
Worxinfo Ltd

Yes graham, this is possible,

look into de server-admin pages, here you can set it hardcoded.

you can also set it softcoded in the method:

Look at the sample of the sendMail method of the plugin

Thanks Harjo

Have been able to hardcode into the Server Admin pages but wanted to see if possible to soft-code. Clients are in several countries and use different ISP’s.

Had already changed the SMTP Host using the examples in F1/Help docs - but can’t work out how to code the SMTP auth including Username & Password. I’ve experimented trying to setup arrays but failed miserably :?

Regards

Graham Greensall
Worxinfo Ltd

No documentation that I know of to help you on this one. I had to look at the plugin source code when I was messing with this a while back. The array needs name=value pairs, not just values in order to work. Here you go:

var authorization = new Array("mail.smtp.host=<smtp mail server>", "mail.smtp.auth=true", "mail.smtp.username=<user name>", "mail.smtp.password=<password>");

var emailSend = "<email address>";
var emailFrom = "<email address>";
var emailSubject = "SMTP help";
var emailMsg = "test code";

var success = plugins.mail.sendMail(emailSend, emailFrom, emailSubject, emailMsg, null, null, null, authorization );

Makes things a lot more flexible for sure!

Many thanks David

That looks just right - I hadn’t thought about trying name=value pairs. Can I ask how you looked at the source code as I’d like to try to learn more about this plugin.

Regards

Graham Greensall

Unzip the mail.jar file in the plugins directory and you will get a folder containing a bunch of files. The Servoy programmers included the source code files (ones with the .java extension) to the mail plugin specifically to help us figure out how plugins things work. Open with any text editor.

The following code in “MailServer.java” is what tips you off to the formatting needed for the smtp server overrides (well, this combined with knowing what the Server page settings required and a bit of testing):

private Properties overrideProperties(Properties defaults, String[] overrideProperties)
	{
		Properties properties = new Properties(defaults);
	
		if (overrideProperties == null)
			return properties;
		
		for (int i = 0; i < overrideProperties.length; i++)
		{
			String property = overrideProperties[i];
			if (property != null)
			{
				int j = property.indexOf('=');
				if (j > 0)
				{
					String propertyName = property.substring(0, j);
					String propertyValue = property.substring(j + 1);
					properties.put(propertyName, propertyValue);
				}
			}			
		}
		return properties;
	}

Writing Servoy plugins is surprisingly easy with a little help. Your little help is here:

http://developer.servoy.com/generic.jsp?taxonomy_id=299

Plugin API here:

http://developer.servoy.com/docs/plugin-api/

Fun once you get the hang of it.

David

Many thanks for taking the time to explain in detail. Very useful. Johan’s how-to is so well written even I can (almost) understand it.

Regards

Graham

An update on using multiple SMTP accounts with the Send Mail plugin.

It works brilliantly - although I’ve lost some hair in the process so here’s some gotcha’s to avoid.

1
Assuming you are getting the SMTP account details from stored fields (and not hard coding) be sure to close the inverted commas “” after the = sign

var authorization = new Array(“mail.smtp.host=” + smtp mail server, “mail.smtp.auth=true”, “mail.smtp.username=” + user name, “mail.smtp.password=” + password);

2
Send Mail will fail if Subject or MsgText are empty. Either error trap or add a space ie: MsgText += ’ ’

3
Send Mail failure messages can be found in the Server Log.

Finally, double-check that your Users have entered their passwords correctly before you spend 2hrs debugging & testing your code trying to work out why its failing :?

Regards

Graham Greensall
Worxinfo Ltd

It will also fail if any of your email addresses are not formatted correctly. One of the many functions in Marcel’s indispensable tools plugin validates email addresses:

http://www.servoymagazine.com/home/2005 … eleas.html

This thread just saved me hours and hours. Thanks especially to Graham for providing that one line of code for creating the autorization array - it worked right off the bat.

Just to add my two cents worth on the subject of SMTP authorization…

You might find that your SMTP server authorization needs two additional variables supplied, namely the PORT number and the ’ Use STARTTLS’ box or sometimes called ‘SSL’. You can normally figure this out by going to your email client (Eudora, Mail, Outlook, etc.) and looking up how the Outgoing Mail Server client is configured. So if there is a Server Port number use that. If you have something like a ‘Use Secure Sockets Layer (SSL)’ checkbox then you must also configure that. So for instance using Gmail as your SMTP server you need to configure the authorization variable as follows:

var authorization = new Array("mail.smtp.host=smtp.gmail.com", "mail.smtp.auth=true", "mail.smtp.starttls.enable=true", "mail.smtp.port=587", "mail.smtp.username=username@gmail.com", "mail.smtp.password=userpassword");

thanks John, great tip!

hope that will make it into the Servoy-docs as well.