ProducerTemplate.Camel.如何添加附件

7uzetpgm  于 2022-11-07  发布在  Apache
关注(0)|答案(2)|浏览(194)

有没有人能告诉我如何使用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;
    }
}

邮件通信转换器.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中有该文件,并且有fileName(t_ficnon)和扩展名(t_ficext),但我找不到如何在MailSender2中的template.sendBodyAndHeaders(.....)之前附加此文件
任何帮助都将不胜感激。问候。

x6492ojm

x6492ojm1#

也许我没有完全理解你的问题,但是ProducerTemplate不知道消息类型。
您只需要向端点发送一个正文,也许还包括标题。
因此,主体只需要是一个完全构造的MimeMessage对象,如Camel Mail文档中所述。
您可以简单地使用construct the mail message with Java,然后将该对象与ProducerTemplate一起使用(您已经这样做了)。

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

gmxoilav2#

谢谢你的回答!
但是,最后,我可以这样做:
新类电子邮件处理器.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));
    }

}

相关问题