繁体   English   中英

生产者模板。 骆驼。 如何添加附件

[英]ProducerTemplate. Camel. How to add attachment

有人能告诉我如何使用 ProducerTemplate 添加附件吗? 我一直在寻找,但我找不到我的案例的答案。

我正在使用 Camen 2.1 并且我有这三个类:

邮件发送器2.java


import java.util.HashMap;
import java.util.Map;
import java.util.ResourceBundle;

import org.apache.camel.Exchange;
import org.apache.camel.ExchangePattern;
import org.apache.camel.ProducerTemplate;

public class MailSender2 extends TypeMail{

    private static final ResourceBundle RES = ResourceBundle.getBundle("mail");
    protected static final String MAIL_NOTIFICATION_ENDPOINT=RES.getString("mail.host.location").trim()+":"+RES.getString("mail.port").trim();

    private Map<String, Object> header;

    public MailSender2() {
        this.header=new HashMap<>();
    }

    public void send(ProducerTemplate template) {
        this.header.put("From", this.getT_from());
        this.header.put("To", this.getT_to());
        this.header.put("Subject", this.getT_subject());
        this.header.put(Exchange.CONTENT_TYPE, "text/html; charset=UTF-8");

        //this.getF_ficher() <-- I have here the file to attach
        //this.getT_ficnon() <-- I have here the name ot the file
        //this.getT_ficext() <-- I have here the extension ot the file

        template.sendBodyAndHeaders(MAIL_NOTIFICATION_ENDPOINT, this.getT_mensaje(), header);
    }

}

类型邮件.java :


public class TypeMail {


    private String t_id;
    private String t_from;
    private String t_to;
    private String t_subject;
    private String t_mensaje;
    private byte[] f_ficher;
    private String t_ficnon;
    private String t_ficext;

    public String getT_id() {
        return t_id;
    }

    public void setT_id(String t_id) {
        this.t_id = t_id;
    }

    public String getT_from() {
        return t_from;
    }

    public void setT_from(String t_from) {
        this.t_from = t_from;
    }

    public String getT_to() {
        return t_to;
    }

    public void setT_to(String t_to) {
        this.t_to = t_to;
    }

    public String getT_subject() {
        return t_subject;
    }

    public void setT_subject(String t_subject) {
        this.t_subject = t_subject;
    }

    public String getT_mensaje() {
        return t_mensaje;
    }

    public void setT_mensaje(String t_mensaje) {
        this.t_mensaje = t_mensaje;
    }

    public byte[] getF_ficher() {
        return f_ficher;
    }

    public void setF_ficher(byte[] f_ficher) {
        this.f_ficher = f_ficher;
    }

    public String getT_ficnon() {
        return t_ficnon;
    }

    public void setT_ficnon(String t_ficnon) {
        this.t_ficnon = t_ficnon;
    }

    public String getT_ficext() {
        return t_ficext;
    }

    public void setT_ficext(String t_ficext) {
        this.t_ficext = t_ficext;
    }
}

MailCommunicationTransformer.java :


import org.apache.camel.CamelContext;
import org.apache.camel.Exchange;
import org.apache.camel.ProducerTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ws.soap.client.SoapFaultClientException;

public class MailCommunicationTransformer {

    MailSender2 mailSender = null;

    static Logger logger = LoggerFactory.getLogger(MailCommunicationTransformer.class);


    public MailCommunicationTransformer()
    {
    }

    public MailLog transform(Object actualMessage, Exchange exchange, CamelContext context)
    {

        mailSender = exchange.getIn().getBody(MailSender2.class);
        try {
            MailSenderDAO mailSenderDAO = (MailSenderDAO)context.getRegistry().lookup("MailSenderDAO");
            mailSenderDAO.validarInput(mailSender);

            if (mailSender!=null) {
                ProducerTemplate template=exchange.getContext().createProducerTemplate();
                try {
                    mailSender.send(template);
                }
                catch (Throwable ex) {
                    ex.printStackTrace();
                    exchange.setProperty(Exchange.EXCEPTION_CAUGHT,ex);
                }
            }
        }catch (MailException me) {
            me.printStackTrace();
            exchange.setProperty(Exchange.EXCEPTION_CAUGHT,me);
        }

        Throwable e = exchange.getProperty(Exchange.EXCEPTION_CAUGHT,
                Throwable.class);
        String response = "OK";
        if (e != null) {
            StringBuffer mensaje = new StringBuffer();
            if (e instanceof SoapFaultClientException) {
                mensaje.append("MAIL fault exception: CLIENT. ");
            } else {
                mensaje.append("MAIL fault exception: MAIL. ");
            }
            logger.info("MailCommunicationTransformer",e);
            while (e != null) {
                e.printStackTrace();
                mensaje.append(e.getMessage());
                e = e.getCause();
            }
            response = mensaje.toString();
        }

        MailLog log = new MailLog(mailSender, response); //, protocolo
        return log;
    }
}

在 TypeMail 中,我有 f_ficher 中的文件,以及文件名 (t_ficnon) 和扩展名 (t_ficext),但我找不到如何在 template.sendBodyAndHeaders(.....) 之前在 MailSender2 中附加此文件

任何帮助将不胜感激。 问候。

也许我不完全理解你的问题,但 ProducerTemplate 不知道消息类型。

您只需将正文和标题发送到端点。

因此,正文只需要是Camel Mail文档中记录的完全构造的MimeMessage对象。

您可以简单地用 Java 构造邮件消息,然后将对象与 ProducerTemplate 一起使用(您已经这样做了)。

template.sendBodyAndHeaders("your-smtp-endpoint", yourMimeMessageInstance, yourHeaderMap);

谢谢你的回答!

但是,最后,我可以这样做:

新类EmailProcessor.java

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.nio.file.Files;
import java.util.Objects;
import java.util.ResourceBundle;

import javax.activation.DataHandler;

import org.apache.camel.Exchange;
import org.apache.camel.Message;
import org.apache.camel.Processor;
import org.apache.commons.codec.binary.Base64;

public class EmailProcessor implements Processor {

    // Atributos de la clase
    private TypeMail typeMail;

    public EmailProcessor(TypeMail typeMail) {
        this.typeMail = typeMail;

    }

    @Override
    public void process(Exchange exchange) throws Exception {

        Message ms = exchange.getIn();
        ms.setHeader("From", this.typeMail.getT_from());
        ms.setHeader("To", this.typeMail.getT_to());
        ms.setHeader("Subject", this.typeMail.getT_subject());
        ms.setHeader(Exchange.CONTENT_TYPE, "text/html; charset=UTF-8");

        ms.setBody("<p style='font-family: Calibri;'>" + this.typeMail.getT_mensaje() + "</p>");

        if (this.typeMail.getF_ficher() != null) {
            String mimeType = "application/pdf";
            if ("zip".equals(typeMail.getT_ficext())) {
                mimeType = "application/zip";
            }
            ms.addAttachment(typeMail.getT_ficnom() + "." + typeMail.getT_ficext(), new DataHandler(typeMail.getF_ficher(), mimeType));
        }
    }

}

邮件发件人.java :

import java.util.ResourceBundle;

import org.apache.camel.ExchangePattern;
import org.apache.camel.ProducerTemplate;

public class MailSender extends TypeMail{

    private static final ResourceBundle RES = ResourceBundle.getBundle("mail");
    protected static final String MAIL_NOTIFICATION_ENDPOINT=RES.getString("mail.host.location").trim()+":"+RES.getString("mail.port").trim();

    public MailSender() {
    }

    public void send(ProducerTemplate template) {

        template.send(MAIL_NOTIFICATION_ENDPOINT, ExchangePattern.InOnly, new EmailProcessor(this));
    }

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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