简体   繁体   中英

sending email using Mule esb

I have to do test on "sent" variable, if its false then i have to send an email, there is my code:

<http:request-config name="HTTP_Request_Configuration" protocol="HTTPS" host="gist.githubusercontent.com" port="443" doc:name="HTTP Request Configuration"/>
<smtp:gmail-connector name="Gmail" validateConnections="true" doc:name="Gmail"/>
<flow name="push3Flow">
    <quartz:inbound-endpoint jobName="HTTP-Puller-Scheduler" repeatInterval="5000" responseTimeout="10000" doc:name="Quartz" repeatCount="0">
        <quartz:event-generator-job/>
    </quartz:inbound-endpoint>
    <http:request config-ref="HTTP_Request_Configuration" path="Rajeun/b550fe17181610f5c0f0/raw/9cf0940d1b534220a4e50493f42a70e2f4a20584/file.json" method="GET" doc:name="HTTP"/>
    <json:json-to-object-transformer returnClass="java.lang.Object" doc:name="JSON to Object"/>
    <choice doc:name="Choice">
        <when expression="#[(message.payload.sent) &amp;&amp; (message.payload.email != &quot;&quot;)]">
            <logger level="INFO" doc:name="Logger" message="its ok"/>
        </when>
        <otherwise>
            <component doc:name="Java" class="Component1"/>
          <smtp:outbound-endpoint host="smtp.gmail.com" port="587" 
    user="myemail%40gmail.com" password="pass" to= "<Json-email>@gmail.com"
    from="Rajeun" subject="Testing mule" responseTimeout="10000" connector-ref="Gmail" doc:name="Send notification email"/>
        </otherwise>
    </choice>
</flow>

Java class:

import org.mule.api.MuleEventContext;
import org.mule.api.lifecycle.Callable;


public class Component1 implements Callable{


 @Override
 public Object onCall(MuleEventContext eventContext) throws Exception {
  eventContext.getMessage().setPayload("This is email Body ");
  return eventContext.getMessage();
 }

}

my json file:

https://gist.githubusercontent.com/Rajeun/b550fe17181610f5c0f0/raw/3f5eaeea955b6eb56bc4715f535fa8bb6d7ab798/file.json

what i want to do is take the email automatically from the json file (email associated with the token that i had specified). When i try this:

 <smtp:outbound-endpoint host="smtp.gmail.com" port="587" 
        user="myemail%40gmail.com" password="pass" to= "#[payload.email]"
        from="Rajeun" subject="Testing mule" responseTimeout="10000" connector-ref="Gmail" doc:name="Send notification email"/>

it gives that the "Email address" is null how can i do this?! thank you in advance.

Don't use #[payload.email] use #[message.payload.email] as following :-

<smtp:outbound-endpoint host="smtp.gmail.com" port="587" 
        user="myemail%40gmail.com" password="pass" to= "#[message.payload.email]"
        from="Rajeun" subject="Testing mule" responseTimeout="10000" connector-ref="Gmail" doc:name="Send notification email"/>

#[payload.email] as sometimes payload alone fails for no reason .. so better use #[message.payload.email]

Also your expression should be <when expression="#[message.payload.sent &amp;&amp; message.payload.email != &quot;&quot;]">

Edited version

<set-variable variableName="email" value="#[message.payload.email]" doc:name="Variable"/>
 <component doc:name="Java" class="Component1"/>
 <smtp:outbound-endpoint host="smtp.gmail.com" port="587" 
            user="myemail%40gmail.com" password="pass" to= "#[flowVars['email']]"
            from="Rajeun" subject="Testing mule" responseTimeout="10000" connector-ref="Gmail" doc:name="Send notification email"/>

Set the #[message.payload.email] in a variable called email and then use the variable email in smtp component like above

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM