You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by Dorfschmid, Jürgen <Ju...@buerotex.de> on 2020/12/02 09:17:36 UTC

How to send attachments together with alternative mail text (plain text/html) ?

I'm trying to send mail attachments together with alternative body text with the following piece of code. Having the receiving mail client configured for HTML body, all is working as desired: mail text and attachment is displayed correctly in the mail client (Outlook, Thunderbird).
But when I switch to plain text only, the attachment is not displayed / downloadable - it seems to be "swallowed" by the email clients. Is that normal behavior ? How should the message look like that the attachment is visible in both cases ? Any help very much appreciated.
I'm trying to send mail attachments together with alternative body text with the following piece of code. Having the receiving mail client configured for HTML body, all is working as desired: mail text and attachment is displayed correctly in the mail client (Outlook, Thunderbird).
But when I switch to plain text only, the attachment is not displayed / downloadable - it seems to be "swallowed" by the email clients. Is that normal behavior ? How should the message look like that the attachment is visible in both cases ? Any help very much appreciated.

@Component
public class MyRouteBuilder3 extends RouteBuilder {
  /*****************************************************************************
   * Camel (3.4.0)
   * @throws Exception
   ****************************************************************************/
  @Override
  public void configure() throws Exception
  {
    from( "direct:mail" )
      .doTry()
        .setBody( constant( new Invoice( "111111", "9999999999", "13.03.2020")))

        .setHeader( "copyOfBody", simple( "${body}" ))
        .to( "velocity://templates/template_txt.vm?encoding=utf-8" )
        .setHeader( "plaintext", simple( "${body}" ))

        .setBody( simple( "${header.copyOfBody}" ))
        .to( "velocity://templates/template_html.vm?encoding=utf-8" )

        .setHeader( "contentType",   constant( "text/html;charset=UTF-8" ))
        .setHeader( "subject",       simple( "Your invoice" ))
        .setHeader( "from",          simple( "sender@localhost.localdomain" ))
        .setHeader( "to",            simple( "receiver@localhost.localdomain" ))

        .process( exchange ->
        {
          AttachmentMessage attMsg = exchange.getIn( AttachmentMessage.class );
          attMsg.addAttachment( "invoice.pdf",
            new DataHandler( new FileDataSource( new File( "C:/tmp/invoice.pdf"))));
        })

        .to( "smtp://127.0.0.1:25?debugMode=true&alternativeBodyHeader=plaintext" )
    ;
  }
}


Mit freundlichen Grüßen / Kind regards

Jürgen Dorfschmid
Dipl.-Ing.(FH), Software-Entwicklung

Tel.: +49 (7022) 2790 376
Fax: +49 (7022) 2790 499
Juergen.Dorfschmid@buerotex.de
www.buerotex.de

--------------------------------------------------------------------------------

BÜROTEX metadok GmbH, Max-Eyth-Str. 21, D-72622 Nürtingen
Geschäftsführung: Werner Führer, Thomas Griesinger
Handelsregisternummer: Amtsgericht Stuttgart, HRB 225678

--------------------------------------------------------------------------------

Diese E-Mail enthält vertrauliche oder rechtlich geschützte Informationen. Wenn Sie nicht der beabsichtigte Empfänger sind, informieren Sie bitte sofort den Absender und löschen Sie diese E-Mail. Das unbefugte Kopieren dieser E-Mail oder die unbefugte Weitergabe der enthaltenen Informationen ist nicht gestattet.

Mit freundlichen Grüßen / Kind regards

Jürgen Dorfschmid
Dipl.-Ing.(FH), Software-Entwicklung

Tel.: +49 (7022) 2790 376
Fax: +49 (7022) 2790 499
Juergen.Dorfschmid@buerotex.de
www.buerotex.de

--------------------------------------------------------------------------------

BÜROTEX metadok GmbH, Max-Eyth-Str. 21, D-72622 Nürtingen
Geschäftsführung: Werner Führer, Thomas Griesinger
Handelsregisternummer: Amtsgericht Stuttgart, HRB 225678

--------------------------------------------------------------------------------

Diese E-Mail enthält vertrauliche oder rechtlich geschützte Informationen. Wenn Sie nicht der beabsichtigte Empfänger sind, informieren Sie bitte sofort den Absender und löschen Sie diese E-Mail. Das unbefugte Kopieren dieser E-Mail oder die unbefugte Weitergabe der enthaltenen Informationen ist nicht gestattet.

Rép. : How to send attachments together with alternative mail text (plain text/html) ?

Posted by Daniel Langevin <da...@shq.gouv.qc.ca>.
Hi,

you only have tu put the email text in a header.

here is the way , to get clean alternative body text by using groovy in SPRING in camel 2.xx
only have to adapt in camel 3.4


             <setHeader headerName="CamelMailAlternativeBody">
               <groovy>
                   def Jsoup       = new org.jsoup.Jsoup();
                   def wlist(org.jsoup.safety.Whitelist basic ) {
                        return new org.jsoup.safety.Whitelist()
                                .addTags(
                                        "a", "b", "blockquote", "br", "cite", "code", "dd", "dl", "dt", "em",
                                        "i", "li", "ol", "p", "pre", "q", "small", "span", "strike", "strong", "sub",
                                        "sup", "u", "ul")
                                .addAttributes("a", "href")
                                .addAttributes("blockquote", "cite")
                                .addAttributes("q", "cite")
                                .addProtocols("a", "href", "ftp", "http", "https", "mailto")
                                .addProtocols("blockquote", "cite", "http", "https")
                                .addProtocols("cite", "cite", "http", "https")
                   }
                   zbody           = Jsoup.clean(request.body,wlist()); // preserve some tags, bases one
                   zbody           = zbody.replaceAll("&lt;a .*href=","")
                   zbody           = zbody.replaceAll("&gt;&lt;/a&gt;","")
                   result          = Jsoup.parse(zbody).wholeText(); // preserve CRLF
               </groovy>
             </setHeader>

Hope is help!


Daniel Langevin
Direction de l’assistance et des technologies
Direction des ressources informationnelles
 
Société d’habitation du Québec
Édifice Marie-Guyart
1054, rue Louis-Alexandre-Taschereau
Aile Jacques-Parizeau, 1er étage
Québec (Québec) G1R 5E7
Téléphone : 418 643-4035, poste 1191
Sans frais : 1 800 463-4315





>>> 
De : 	Dorfschmid, Jürgen <Ju...@buerotex.de>
À :	"'users@camel.apache.org'" <us...@camel.apache.org>
Date : 	2020-12-02 04:17
Objet : 	How to send attachments together with alternative mail text (plain text/html) ?

I'm trying to send mail attachments together with alternative body text with the following piece of code. Having the receiving mail client configured for HTML body, all is working as desired: mail text and attachment is displayed correctly in the mail client (Outlook, Thunderbird).
But when I switch to plain text only, the attachment is not displayed / downloadable - it seems to be "swallowed" by the email clients. Is that normal behavior ? How should the message look like that the attachment is visible in both cases ? Any help very much appreciated.
I'm trying to send mail attachments together with alternative body text with the following piece of code. Having the receiving mail client configured for HTML body, all is working as desired: mail text and attachment is displayed correctly in the mail client (Outlook, Thunderbird).
But when I switch to plain text only, the attachment is not displayed / downloadable - it seems to be "swallowed" by the email clients. Is that normal behavior ? How should the message look like that the attachment is visible in both cases ? Any help very much appreciated.

@Component
public class MyRouteBuilder3 extends RouteBuilder {
  /*****************************************************************************
   * Camel (3.4.0)
   * @throws Exception
   ****************************************************************************/
  @Override
  public void configure() throws Exception
  {
    from( "direct:mail" )
      .doTry()
        .setBody( constant( new Invoice( "111111", "9999999999", "13.03.2020")))

        .setHeader( "copyOfBody", simple( "${body}" ))
        .to( "velocity://templates/template_txt.vm?encoding=utf-8" )
        .setHeader( "plaintext", simple( "${body}" ))

        .setBody( simple( "${header.copyOfBody}" ))
        .to( "velocity://templates/template_html.vm?encoding=utf-8" )

        .setHeader( "contentType",   constant( "text/html;charset=UTF-8" ))
        .setHeader( "subject",       simple( "Your invoice" ))
        .setHeader( "from",          simple( "sender@localhost.localdomain" ))
        .setHeader( "to",            simple( "receiver@localhost.localdomain" ))

        .process( exchange ->
        {
          AttachmentMessage attMsg = exchange.getIn( AttachmentMessage.class );
          attMsg.addAttachment( "invoice.pdf",
            new DataHandler( new FileDataSource( new File( "C:/tmp/invoice.pdf"))));
        })

        .to( "smtp://127.0.0.1:25?debugMode=true&alternativeBodyHeader=plaintext" )
    ;
  }
}


Mit freundlichen Grüßen / Kind regards

Jürgen Dorfschmid
Dipl.-Ing.(FH), Software-Entwicklung

Tel.: +49 (7022) 2790 376
Fax: +49 (7022) 2790 499
Juergen.Dorfschmid@buerotex.de 
www.buerotex.de 

--------------------------------------------------------------------------------

BÜROTEX metadok GmbH, Max-Eyth-Str. 21, D-72622 Nürtingen
Geschäftsführung: Werner Führer, Thomas Griesinger
Handelsregisternummer: Amtsgericht Stuttgart, HRB 225678

--------------------------------------------------------------------------------

Diese E-Mail enthält vertrauliche oder rechtlich geschützte Informationen. Wenn Sie nicht der beabsichtigte Empfänger sind, informieren Sie bitte sofort den Absender und löschen Sie diese E-Mail. Das unbefugte Kopieren dieser E-Mail oder die unbefugte Weitergabe der enthaltenen Informationen ist nicht gestattet.

Mit freundlichen Grüßen / Kind regards

Jürgen Dorfschmid
Dipl.-Ing.(FH), Software-Entwicklung

Tel.: +49 (7022) 2790 376
Fax: +49 (7022) 2790 499
Juergen.Dorfschmid@buerotex.de 
www.buerotex.de 

--------------------------------------------------------------------------------

BÜROTEX metadok GmbH, Max-Eyth-Str. 21, D-72622 Nürtingen
Geschäftsführung: Werner Führer, Thomas Griesinger
Handelsregisternummer: Amtsgericht Stuttgart, HRB 225678

--------------------------------------------------------------------------------

Diese E-Mail enthält vertrauliche oder rechtlich geschützte Informationen. Wenn Sie nicht der beabsichtigte Empfänger sind, informieren Sie bitte sofort den Absender und löschen Sie diese E-Mail. Das unbefugte Kopieren dieser E-Mail oder die unbefugte Weitergabe der enthaltenen Informationen ist nicht gestattet.