Friday, July 22, 2011

Servoy TIP: How To Automate Sending Mail from Gmail

I recently needed to create some functionality to send mail via my Gmail account from within Servoy. It sounds pretty straight forward, but it turned out that there was one parameter that tripped me up. If you don't include this line of code - it won't send email, no matter what you try:

mail.smtp.starttls.enable=true

Took me a little while of digging on the Internet and trying stuff to get it to work. So, for this week's tip - I thought it might be a good idea to share the code:

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

Enjoy!

No comments:

Web Analytics