Send a simple email message with the result callback

This set of functions sends a simple email message to one recipient, (with sending result callback) from the default email account on the device. At least one email account must be already configured on the device. PRIVILEGE NEEDED to be set in tizen-manifest.xml: http://tizen.org/privilege/email
//	PRIVILEGE NEEDED to be set in tizen-manifest.xml file:
//	http://tizen.org/privilege/email

#include <email.h>
#include <dlog.h> //for logging purposes

//callback function to be passed as a last parameter to the function below
void email_callback(email_h email, email_sending_e result, void *user_data)
{
   if (result == EMAIL_SENDING_SUCCEEDED)
   {
	   dlog_print(DLOG_INFO, LOG_TAG, "Message sending success!");
   }
   else
   {
	  dlog_print(DLOG_ERROR, LOG_TAG, "Message sending failure!");
   }
   if (email_destroy_message(email) == EMAILS_ERROR_NONE)
   {
	   dlog_print(DLOG_INFO, LOG_TAG, "Message memory freed successfully");
   }
}

bool send_simple_email_with_callback(const char *recipient, const char *subject, const char *body, email_message_sent_cb result_callback)
{
	email_h mail;

	if (email_create_message(&mail)!= EMAILS_ERROR_NONE)
	{
		dlog_print(DLOG_ERROR, LOG_TAG, "Error occurred when creating email message!");
		return false;
	}

	if ((email_add_recipient(mail, EMAIL_RECIPIENT_TYPE_TO, recipient)!= EMAILS_ERROR_NONE)
		|| (email_set_subject(mail, subject) != EMAILS_ERROR_NONE)
		|| (email_set_body(mail, body) != EMAILS_ERROR_NONE))
	{
	   dlog_print(DLOG_ERROR, LOG_TAG, "Error occurred when adding data to email message!");
	   email_destroy_message(mail);
	   return false;
	}
	if (email_save_message(mail) != EMAILS_ERROR_NONE)
	{
	   dlog_print(DLOG_ERROR, LOG_TAG, "Error occurred when saving message!");
	   email_destroy_message(mail);
	   return false;
	}
	else
	{
		dlog_print(DLOG_INFO, LOG_TAG, "Message saved.");
	}
	if (email_set_message_sent_cb(mail, result_callback, NULL) != EMAILS_ERROR_NONE)
	{
	   dlog_print(DLOG_ERROR, LOG_TAG, "Error occurred when setting the sending result callback");
	   email_destroy_message(mail);
	   return false;
	}
	if (email_send_message(mail, false) != EMAILS_ERROR_NONE)
	{
	   dlog_print(DLOG_ERROR, LOG_TAG, "Error occurred when sending an email message!");
	   email_destroy_message(mail);
	   return false;
	}
	else
	{
		dlog_print(DLOG_INFO, LOG_TAG, "Sending message...");
	}

	return true;
}

Responses

0 Replies