val mailActions = getActions("mail","mail:smtp:sampleserver")
mailActions.sendHtmlMail("mail@example.com", "Test subject", "<h1>Header</h1>This is the mail content.", attachmentUrlList)
```
+
+## Mail Headers
+
+The binding allows one to add custom e-mail headers to messages that it sends.
+For example if you want e-mails sent by this binding to be grouped into a "threaded view" in your email client, you must provide an e-mail "Reference" header, which acts as the key for grouping messages together.
+Headers can be added inside a rule by calling the `mailActions.addHeader()` method before calling the respective `mailActions.sendMail()` method.
+See the example below.
+
+```
+rule "Send Mail with a 'Reference' header; for threaded view in e-mail client"
+when
+ ...
+then
+ val mailActions = getActions("mail","mail:smtp:sampleserver")
+ mailActions.addHeader("Reference", "<unique-thread-identifier>")
+ mailActions.sendMail("mail@example.com", "Test subject", "Test message text")
+end
+```
+
+Note: in the case of the "Reference" header, the `<unique-thread-identifier>` has to be an ASCII string enclosed in angle brackets.
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
+import java.util.HashMap;
import java.util.List;
+import java.util.Map;
import javax.activation.FileDataSource;
import javax.mail.internet.AddressException;
private String subject = "(no subject)";
private String text = "";
private String html = "";
+ private Map<String, String> headers = new HashMap<>();
/**
* Create a new MailBuilder
return this;
}
+ public MailBuilder withHeader(String name, String value) {
+ headers.put(name, value);
+ return this;
+ }
+
/**
* Build the Mail
*
mail.setFrom(sender);
}
+ headers.forEach((name, value) -> mail.addHeader(name, value));
+
return mail;
}
}
import java.net.MalformedURLException;
import java.util.ArrayList;
+import java.util.HashMap;
import java.util.List;
+import java.util.Map;
import javax.mail.internet.AddressException;
private final Logger logger = LoggerFactory.getLogger(SendMailActions.class);
private @Nullable SMTPHandler handler;
+ private Map<String, String> headers = new HashMap<>();
@RuleAction(label = "@text/sendMessageActionLabel", description = "@text/sendMessageActionDescription")
public @ActionOutput(name = "success", type = "java.lang.Boolean") Boolean sendMail(
}
}
+ headers.forEach((name, value) -> builder.withHeader(name, value));
+
final SMTPHandler handler = this.handler;
if (handler == null) {
logger.info("Handler is null, cannot send mail.");
}
}
+ headers.forEach((name, value) -> builder.withHeader(name, value));
+
final SMTPHandler handler = this.handler;
if (handler == null) {
logger.warn("Handler is null, cannot send mail.");
public @Nullable ThingHandler getThingHandler() {
return handler;
}
+
+ @RuleAction(label = "@text/addHeaderActionLabel", description = "@text/addHeaderActionDescription")
+ public @ActionOutput(name = "success", type = "java.lang.Boolean") Boolean addHeader(
+ @ActionInput(name = "name") @Nullable String name, @ActionInput(name = "value") @Nullable String value) {
+ if (name != null && !name.isEmpty()) {
+ if (value != null && !value.isEmpty()) {
+ headers.put(name, value);
+ } else {
+ headers.remove(name);
+ }
+ return true;
+ }
+ return false;
+ }
+
+ public static boolean addHeader(ThingActions actions, @Nullable String name, @Nullable String value) {
+ return ((SendMailActions) actions).addHeader(name, value);
+ }
}
sendHTMLAttachmentsMessageActionLabel = send a HTML mail with several attachments
sendHTMLAttachmentsMessageActionDescription = Sends a HTML mail with several URL attachments.
+
+addHeaderActionLabel = add a mail header
+addHeaderActionDescription = Adds a mail header to the mail message.
import java.io.IOException;
import java.net.MalformedURLException;
+import java.util.Map;
import javax.mail.MessagingException;
import javax.mail.internet.AddressException;
private static final String TEST_STRING = "test";
private static final String TEST_EMAIL = "foo@bar.zinga";
+ private static final String HEADER_1_KEY = "key_one";
+ private static final String HEADER_1_VAL = "value_one";
+ private static final String HEADER_2_KEY = "key_two";
+ private static final String HEADER_2_VAL = "value_two";
+
@Test
public void illegalToAddressThrowsException() {
assertThrows(AddressException.class, () -> new MailBuilder("foo bar.zinga"));
assertEquals(TEST_EMAIL, builder.build().getToAddresses().get(0).getAddress());
assertEquals(2, builder.withRecipients(TEST_EMAIL).build().getToAddresses().size());
}
+
+ @Test
+ public void withHeaders() throws EmailException, MessagingException, IOException {
+ MailBuilder builder = new MailBuilder(TEST_EMAIL);
+ Email mail = builder.withHeader(HEADER_1_KEY, HEADER_1_VAL).withHeader(HEADER_2_KEY, HEADER_2_VAL).build();
+
+ Map<String, String> headers = mail.getHeaders();
+
+ assertEquals(2, headers.size());
+ assertEquals(HEADER_2_VAL, headers.get(HEADER_2_KEY));
+ assertEquals(HEADER_1_VAL, headers.get(HEADER_1_KEY));
+ }
}