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

Understanding C1 and C2 Compilers in Java

Understanding C1 and C2 Compilers in Java Understanding C1 and C2 Compilers in Java In Java, the Just-In-Time (JIT) compiler is a part of the Java Virtual Machine (JVM) that improves the performance of Java applications by compiling bytecode into native machine code at runtime. The JIT compiler includes two different compilers, known as the C1 and C2 compilers, each with distinct optimization strategies and purposes. C1 Compiler (Client Compiler) The C1 compiler, also known as the client compiler, is designed for fast startup times and lower memory consumption. It performs lighter and quicker optimizations, which makes it suitable for applications that require quick startup and responsiveness. Key characteristics of the C1 compiler include: Quick Compilation: Prioritizes fast compilation times over deep optimizations. Low Overhead: Consumes less memory and resources during compilation. Profile-Guided Optimization: Ca...

Java Increment Operations: `n++` vs `n = n + 1` vs `n += 1`

In Java, incrementing a variable by one can be done in several ways: n++ , n = n + 1 , and n += 1 . While these expressions achieve the same end result, they differ slightly in syntax and use cases. Let's explore each one and discuss their performance. 1. n++ Post-Increment Operator : Increments the value of n by 1 after its current value has been used. Common Usage : Typically used in loops and other contexts where the current value needs to be used before incrementing. int n = 5; n++; // n is now 6 2. n = n + 1 Addition Assignment : Explicitly sets n to its current value plus 1. Readability : Straightforward and clear, though slightly more verbose. int n = 5; n = n + 1; // n is now 6 3. n += 1 Compound Assignment Operator : Equivalent to n = n + 1 , but more concise. Usage : Combines addition and assignment into one step. int n = 5; n += 1; // n is...

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 ...