简体   繁体   中英

How to send an email on Mule ESB

Im trying to send an email with a sample proyect. What i try to do is a sample bookstore, the user receive a form which fill out it with the order (name, nif, email, books...). I want to send to this email address, the information of the order done just before. When I try to do, it shows the next error:

Root Exception stack trace:
java.lang.IllegalArgumentException: The required object/property "Email address" is null
at org.mule.transport.email.MailUtils.stringToInternetAddresses(MailUtils.java:93)
at org.mule.transport.email.transformers.StringToEmailMessage.transformMessage(StringToEmailMessage.java:113)
at org.mule.transport.email.transformers.ObjectToMimeMessage.transformMessage(ObjectToMimeMessage.java:54)
+ 3 more (set debug level logging or '-Dmule.verbose.exceptions=true' for everything)

My flow on the XML is:

<smtp:gmail-connector name="Gmail" validateConnections="true" doc:name="Gmail" contentType="text/html" bccAddresses="#[payload.email]" ccAddresses="#[payload.email]" fromAddress="#[payload.email]"/>
<flow name="Email" doc:name="Email">
    <http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8088" doc:name="HTTP" path="Email"/>
    <http:static-resource-handler resourceBase="docroot" defaultFile="index.html" doc:name="HTTP Static Resource Handler"/>
</flow>
<flow name="Correo" doc:name="Correo">
    <http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8088" path="Facturacion" doc:name="HTTP" transformer-refs="Java"/>
    <component doc:name="Generar Pedido" class="org.mule.transformers.EmailTransformer"/>
    <smtp:outbound-endpoint  responseTimeout="10000" doc:name="SMTP" password="pass" port="587" user="email%40gmail.com" from="#[payload.email]" to="#[payload.email]" connector-ref="Gmail" address="#[payload.email]"/>
</flow>

And the class transformer is:

public class EmailTransformer extends AbstractMessageTransformer {


@Override
public Object transformMessage(MuleMessage message, String outputEncoding)
        throws TransformerException {
    // TODO Auto-generated method stub
    final Pedido pedido = (Pedido) message.getPayload();
    System.out.println("Enviando email a la direccion de correo: "+pedido.getEmail()+"\n\n");
    try {
        MailUtils.stringToInternetAddresses(pedido.getEmail());
    } catch (AddressException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    final StringBuilder mailMessage =  new StringBuilder("A continuación le enviamos los datos del pedido que nos ha solicitado:").append("\n");
    mailMessage.append("Nombre:  " + pedido.getNombre()).append("\n");
    mailMessage.append("NIF: " + pedido.getNIF()).append("\n");
    mailMessage.append("Listado de productos: " + pedido.getProductos()).append("\n");
    mailMessage.append("Recibe un cordial saludo. ");

    message.setOutboundProperty(MailProperties.SUBJECT_PROPERTY, "Información del pedido que nos ha solicitado");
    message.setOutboundProperty(MailProperties.TO_ADDRESSES_PROPERTY, pedido.getEmail());

    return mailMessage.toString();
}
}

Anyone can help me? Thanks!!

I see two issues in EmailTransformer :

  • It assumes that the message payload can be cast to Pedido , although the http:inbound-endpoint will produce a message whose payload is of InputStream type. This cast can't work.

  • It doesn't have a constructor that defines the types it accepts, something like:

     public EmailTransformer() { this.registerSourceType(DataTypeFactory.create(Pedido.class)); this.setReturnDataType(DataTypeFactory.STRING); } 

You would then need to add a stream to Pedido transformer in front of the EmailTransformer .

Alternatively, you can refactor EmailTransformer to work directly with an InputStream .

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