Sending formatted mails with Mule

2019-02-20 02:09发布

问题:

I'm sending emails using Mule. I need to add format to the text I send. The content of the mail is the payload which has a String in it that I form in a Java method and send to the flow with an Expression transformer. I need to add format to that String: bold, underline, colour.... How can I do it?

This is an extract of my flow:

   <expression-transformer expression="#[com.generateText4Email(payload)]" doc:name="mailText"/>
<smtp:outbound-endpoint host="${smtp.host}"
        responseTimeout="10000" doc:name="SMTP" connector-ref="smtpConnector"
        from="${smtp.from}" port="${smtp.port}" subject="Invoice"
        to="mail@mail.com" />

I've tried with this but it doesn't work.

<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"><head><title>Email</title></head><body style="font-family:'Century Gothic'"><h1 style="text-align:center;"> company </h1><h2 style="font-size:14px;">Name : name <br />Company : arch <br />Email : qqq@aaaa.com</h2><p>fin</p></body></html>

Thanks in advance.

回答1:

I solved it. I was missing setting content-type in the smtp:connector and the mime-type in the smtp:outbpund-endbound as text/html for the to work. This is the correct code:

<smtp:connector name="smtpConnector"
    validateConnections="true" doc:name="SMTP" contentType="text/html"/>
 <smtp:outbound-endpoint host="${smtp.host}"
    responseTimeout="10000" doc:name="SMTP" connector-ref="smtpConnector"
    from="${smtp.from}" port="${smtp.port}" subject="Invoice"
    to="mail@mail.com"" mimeType="text/html"/>

With this configuration you can use html in your text like this

<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"><head><title>Email</title></head><body style="font-family:'Century Gothic'"><h1 style="text-align:center;"> company </h1><h2 style="font-size:14px;">Name : name <br />Company : arch <br />Email : qqq@aaaa.com</h2><p>fin</p></body></html>

and it will be shown when receiving the email.

Hope it'll help someone else.



回答2:

Alternately you can use a Velocity template with your Mule application.
Velocity is a Java-based template engine and can be used to send formatted email.
You can also use various HTML tags to format your content
ref :- https://velocity.apache.org/engine/releases/velocity-1.5/user-guide.html