如何在 Mule ESB 上发送电子邮件

How to send an email on Mule ESB

我正在尝试发送带有示例项目的电子邮件。我尝试做的是一个示例书店,用户收到一个表格,其中填写了订单(姓名、nif、电子邮件、书籍......)。我想将之前完成的订单信息发送到这个电子邮件地址。当我尝试这样做时,它显示下一个错误:

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)

我在 XML 上的流量是:

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

class 变压器是:

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();
}
}

谁能帮帮我? 谢谢!!

我在 EmailTransformer 中看到两个问题:

  • 它假定消息有效负载可以转换为 Pedido,尽管 http:inbound-endpoint 将生成一个有效负载为 InputStream 类型的消息。此转换无效。

  • 它没有定义它接受的类型的构造函数,类似于:

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

然后您需要在 EmailTransformer 之前向 Pedido 转换器添加一个流。

或者,您可以重构 EmailTransformer 以直接使用 InputStream