Backendless Support
 

Sending email from a mobile, browser or desktop app

Sending an email is a very common operation for many applications. For most of them it is the server-side that is responsible for delivering an email message.

Backendless makes it trivially easy to deliver a branded email (meaning it will look like it was sent by a developer's app) in the plain text or HTML formats (or both). 

Consider the following code which sends an HTML-formatted email message:

JAVA

Asynchronous sample (Plain Java and Android):

private static void sendHTMLEmailAsync()
{
 AsyncCallback<void> sendEmailCallback = new AsyncCallback<void>()
 {
 @Override
 public void handleResponse( Void response )
 {
 System.out.println( "ASYNC: HTML email has been sent" );
 }
 @Override
 public void handleFault( BackendlessFault backendlessFault )
 {
 System.out.println( "Server returned an error " + backendlessFault.getMessage() );
 }
 };
 String subject = "Hello from Backendless! (Async call)";
 String body = "This is an HTML email sent by an <strong>asynchronous</strong> API call from a <a href="\" <a"="">Backendless</a>"><a href="<a href=" http:="" backendless.com\"="">Backendless</a>"><a"><a href="http://backendless.com\">Backendless</a>"><a"><a href="http://backendless.com\">Backendless</a>"><a"><a href="http://backendless.com\">Backendless</a>"><a"><a href="http://backendless.com\">Backendless</a>"><a"><a href="http://backendless.com\">Backendless</a>"><a"><a href="http://backendless.com\">Backendless</a>"><a"><a href="http://backendless.com\">Backendless</a>"><a"><a href="http://backendless.com\">Backendless</a>"><a">http://backendless.com\">Backendless</a>"><a href="</a"></a"></a"></a"></a"></a"></a"></a"><a href="http://backendless.com\">Backendless</a>"><a"><a href="http://backendless.com\">Backendless</a>"><a"><a href="http://backendless.com\">Backendless</a>"><a"><a href="http://backendless.com\">Backendless</a>"><a"><a href="http://backendless.com\">Backendless</a>"><a"><a href="http://backendless.com\">Backendless</a>"><a"><a href="http://backendless.com\">Backendless</a>"><a"><a href="http://backendless.com\">Backendless</a>"><a"><a href="http://backendless.com\">Backendless</a>"><a">http://backendless.com\">Backendless</a>"><a href="</a"></a"></a"></a"></a"></a"></a"></a"><a href="http://backendless.com\">Backendless</a>"><a"><a href="http://backendless.com\">Backendless</a>"><a"><a href="http://backendless.com\">Backendless</a>"><a"><a href="http://backendless.com\">Backendless</a>"><a"><a href="http://backendless.com\">Backendless</a>"><a"><a href="http://backendless.com\">Backendless</a>"><a"><a href="http://backendless.com\">Backendless</a>"><a">http://backendless.com\">Backendless</a>"><a href="</a"></a"></a"></a"></a"></a"><a href="http://backendless.com\">Backendless</a>"><a href="http://backendless.com\">Backendless">http://backendless.com\">Backendless</a>"><a href="http://b...">http://backendless.com\">Backendless">http://backe...">http://b...">http://backendless.com\">Backendless"...</a> backend";
 String recipient = "mark@backendless.com";
 Backendless.Messaging.sendHTMLEmail( subject, body, recipient, sendEmailCallback );
}</a"></a"></a"></void></void>
Synchronous sample (Plain Java only):
private static void sendHTMLEmailSync()
{
 String subject = "Hello from Backendless! (Sync call)";
 String body = "This is an email sent by <strong>synchronous</strong> API call from a <a href="\" <a"="">Backendless</a>"><a href="<a href=" http:="" backendless.com\"="">Backendless</a>"><a"><a href="http://backendless.com\">Backendless</a>"><a"><a href="http://backendless.com\">Backendless</a>"><a"><a href="http://backendless.com\">Backendless</a>"><a"><a href="http://backendless.com\">Backendless</a>"><a"><a href="http://backendless.com\">Backendless</a>"><a"><a href="http://backendless.com\">Backendless</a>"><a"><a href="http://backendless.com\">Backendless</a>"><a"><a href="http://backendless.com\">Backendless</a>"><a">http://backendless.com\">Backendless</a>"><a href="</a"></a"></a"></a"></a"></a"></a"></a"><a href="http://backendless.com\">Backendless</a>"><a"><a href="http://backendless.com\">Backendless</a>"><a"><a href="http://backendless.com\">Backendless</a>"><a"><a href="http://backendless.com\">Backendless</a>"><a"><a href="http://backendless.com\">Backendless</a>"><a"><a href="http://backendless.com\">Backendless</a>"><a"><a href="http://backendless.com\">Backendless</a>"><a"><a href="http://backendless.com\">Backendless</a>"><a"><a href="http://backendless.com\">Backendless</a>"><a">http://backendless.com\">Backendless</a>"><a href="</a"></a"></a"></a"></a"></a"></a"></a"><a href="http://backendless.com\">Backendless</a>"><a"><a href="http://backendless.com\">Backendless</a>"><a"><a href="http://backendless.com\">Backendless</a>"><a"><a href="http://backendless.com\">Backendless</a>"><a"><a href="http://backendless.com\">Backendless</a>"><a"><a href="http://backendless.com\">Backendless</a>"><a"><a href="http://backendless.com\">Backendless</a>"><a">http://backendless.com\">Backendless</a>"><a href="</a"></a"></a"></a"></a"></a"><a href="http://backendless.com\">Backendless</a>"><a href="http://backendless.com\">Backendless">http://backendless.com\">Backendless</a>"><a href="http://b...">http://backendless.com\">Backendless">http://backe...">http://b...">http://backendless.com\">Backendless"...</a> backend";
 String recipient = "mark@backendless.com";
 Backendless.Messaging.sendHTMLEmail( subject, body, recipient );
 System.out.println( "SYNC: HTML email has been sent" );
}</a"></a"></a">

The code above sends an HTML-formatted email to one recipient. 

A developer can also create an array of email addresses and use it instead. That way an email will be delivered to a group of recipients.

If a developer runs the code above without making any configuration changes to your Backendless backend, the following error will be received : 

BackendlessException{ code: '5045', message: 'Cannot send email with the default Email Settings. Change the Email Settings in the console and try again' }

This error occurs when you try using the functionality for sending an email and do not change the Email Settings. By default every Backendless backend is configured to use our special email account. That account does not let you use the API demonstrated above - you need to change it to your SMTP account. To do that:

  1. Login to Backendless console, select an app and click the Manage icon.
  2. Scroll down to the Email Settings section.
  3. Enter the name you would like to use in the emails sent out by Backendless. Also enter the email address (which is the userid) and password used to authenticate against the SMTP server. Before you save the data, click the Test button to validate the credentials.
  4. Click the Save button to save the new settings.

Below is a screenshot of the settings after I made the changes described above: 

Now when I run the same code, I get the following output:
SYNC: HTML email has been sent
ASYNC: HTML email has been sent

When the email arrives, it looks exactly as expected - an HTML-formatted email: 

There are plenty of methods to deliver email messages in different formats to one or more recipients. 

It is highlighted precisely in API to send email from the documentation.

Is article helpful?