Skip to main content

Sending an email using WSO2 Gmail Connector


The Gmail connector allows you to access the Gmail REST API through WSO2 ESB and send an Email. Gmail is a free, Web-based e-mail service provided by Google. 

First download the Gmail Connector from WSO2 store. And Start the WSO2 ESB server using below steps.
  • download the ESB runtime ZIP file, and then extract the ZIP file.
    The path to this folder will be referred to as <ESB_HOME> throughout the post.
  • Navigate to the <PRODUCT_HOME>/bin/ directory using the Command Prompt.
  • To start the server in a typical environment:
    • On Windows:  wso2server.bat --run
    • On Linux/Mac OS:  sh wso2server.sh
  • To start the server in the background mode of Linux: sh wso2server.sh start
    To stop the server running in this mode, you will enter: sh wso2server.sh stop
  • now go to https://localhost:9443/carbon and log into the management console
    using  username admin and password admin
Step 1 : 

Now go to  Home -> Manage -> Connectors -> Add. 

Upload the downloaded Gmail Connector zip file. Then change the status as enabled.

Step 2 :

Then create a custom proxy service with the name as Send_email_proxy and go to the source view. 

copy and paste below source code to your source

<?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse"
       name="Send_email_proxy"
       transports="http,https"
       statistics="disable"
       trace="disable"
       startOnLoad="true">
   <target>
      <inSequence>
         <property name="userId" expression="json-eval($.userId)"/>
         <property name="accessToken" expression="json-eval($.accessToken)"/>
         <property name="apiUrl" expression="json-eval($.apiUrl)"/>
         <property name="to" expression="json-eval($.to)"/>
         <property name="subject" expression="json-eval($.subject)"/>
         <property name="from" expression="json-eval($.from)"/>
         <property name="messageBody" expression="json-eval($.messageBody)"/>
         <gmail.init>
            <userId>{$ctx:userId}</userId>
            <accessToken>{$ctx:accessToken}</accessToken>
            <apiUrl>{$ctx:apiUrl}</apiUrl>
         </gmail.init>
         <gmail.sendMail>
            <to>{$ctx:to}</to>
            <subject>{$ctx:subject}</subject>
            <from>{$ctx:from}</from>
            <messageBody>{$ctx:messageBody}</messageBody>
         </gmail.sendMail>
         <respond/>
      </inSequence>
   </target>
   <description/>
</proxy>
Step 3:

Now you need to create an app to connect to Gmail API.The Gmail API uses OAuth2 authentication with acessToken. Go to OAuth 2.0 Playground and select the needed API s and authorize them using your gmail account. Click on Exchange authorization code for tokens in the next window and copy the access_token code from the JSON response.

Step 4:


Now we are ready to test our proxy service. We can test our proxy using Postman by sending a JSON request as follows.





I got a new email with "wso2 gmail connector" as the subject.

Reference :




Comments

Popular posts from this blog

How to use WSO2 Class Mediator in WSO2 ESB

The  Class Mediator  creates an instance of a custom-specified class and sets it as a mediator. If any properties are specified, the corresponding setter methods are invoked once on the class during initialization. Use the Class mediator for user-specific, custom developments only when there is no built-in mediator that already provides the required functionality.  The syntax of Class Mediator in ESB < class   name= "class-name" >     <property name= "string"   value= "literal" >     </property> </ class > Creating a Class Mediator lets use the Eclipse  WSO2 Developer Studio Create a New  Mediator project by selecting File --> New --> project --> Mediator Project Now you have class mediator by extending the AbstractMediator class. Then you need to implement the mediate methods Sample class mediator implementation is as follows. package lk.harshana; import org.apache.synapse.Mess

One to Many Mapping using Spring boot

In this blog, I will explain how to use one-to-many mapping in Spring boot Application What you need? JAVA MySql Eclipse IDE ( whatever you like IDE, I'm using Eclipse for this example) Maven ( you can use the Gradle as well) Initial Plan I will create a spring boot application project using the  Spring Initializer  web tool and import the project as a maven project. after configuring the all necessary setting, I will code for one-to-many mapping. Below diagram is the database model diagram which we going to install using the spring boot application. Let's Start to Code. You need to configure the application.properties file for database connections. add the following content to the src/main/resources/application.properties spring.datasource.url=jdbc:mysql://localhost:3306/learning spring.datasource.username=root spring.datasource.password=root spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect spring.jpa.hibernate

When To Use Indexes In MySQL

When deciding when and how to create an index in your MySQL database, it's important to consider how the data is being used. Let's say you have a database of  students . We will create it like this: CREATE TABLE `students` ( `id` int ( 11 ) NOT NULL AUTO_INCREMENT , `first_name` varchar ( 255 ) DEFAULT NULL , `last_name` varchar ( 255 ) DEFAULT NULL , `class` varchar ( 255 ) DEFAULT NULL , PRIMARY KEY ( `id` ) ) ENGINE = InnoDB Indexes are best used on columns that are frequently used in where clauses, and in any kind of sorting, such as "order by". You should also pay attention to whether or not this information will change frequently, because it will slow down your updates and inserts. Since you wont frequently be adding students, you don't have to worry about the inserts Let's say that you will be looking up the students with a web interface and the end user will be typing in the students name to find them, since r