This is an automated email from the ASF dual-hosted git repository.

btellier pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/james-project.git


The following commit(s) were added to refs/heads/master by this push:
     new a40d96cf8e JAMES-4147 SaveMailNameInHeaders Mailet (#2818)
a40d96cf8e is described below

commit a40d96cf8eafd611b977486358aad7e4de364db8
Author: Benoit TELLIER <[email protected]>
AuthorDate: Mon Sep 22 17:29:26 2025 +0200

    JAMES-4147 SaveMailNameInHeaders Mailet (#2818)
---
 .../servers/partials/SaveMailNameInHeaders .adoc   | 15 +++++
 .../transport/mailets/SaveMailNameInHeaders.java   | 58 +++++++++++++++++
 .../mailets/SaveMailNameInHeadersTest.java         | 73 ++++++++++++++++++++++
 3 files changed, 146 insertions(+)

diff --git a/docs/modules/servers/partials/SaveMailNameInHeaders .adoc 
b/docs/modules/servers/partials/SaveMailNameInHeaders .adoc
new file mode 100644
index 0000000000..e3970fd23a
--- /dev/null
+++ b/docs/modules/servers/partials/SaveMailNameInHeaders .adoc 
@@ -0,0 +1,15 @@
+=== SaveMailNameInHeaders Mailet
+
+Save Mail name into X-JAMES-Mail-Name header.
+
+This allows from mailetcontainer logs to find the mail in user mailbox.
+
+== Configuration
+
+You can configure it simply by adding the following in your 
`mailetcontainer.xml` file:
+
+....
+<mailet match="All" class="SaveMailNameInHeaders"/>
+....
+
+
diff --git 
a/mailet/standard/src/main/java/org/apache/james/transport/mailets/SaveMailNameInHeaders.java
 
b/mailet/standard/src/main/java/org/apache/james/transport/mailets/SaveMailNameInHeaders.java
new file mode 100644
index 0000000000..00a1bd701f
--- /dev/null
+++ 
b/mailet/standard/src/main/java/org/apache/james/transport/mailets/SaveMailNameInHeaders.java
@@ -0,0 +1,58 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+
+package org.apache.james.transport.mailets;
+
+import jakarta.mail.MessagingException;
+
+import org.apache.mailet.Mail;
+import org.apache.mailet.base.GenericMailet;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Save Mail name into X-JAMES-Mail-Name header.
+ *
+ * This allows from mailetcontainer logs to find the mail in user mailbox.
+ *
+ * Eg:
+ *
+ * <pre><code>
+ * &lt;mailet match="All" class="SaveMailNameInHeaders"/&gt;
+ * </code></pre>
+ */
+public class SaveMailNameInHeaders extends GenericMailet {
+
+    public static final Logger LOGGER = 
LoggerFactory.getLogger(SaveMailNameInHeaders.class);
+
+    @Override
+    public void service(Mail mail) throws MessagingException {
+        try {
+            mail.getMessage().setHeader("X-JAMES-Mail-Name", mail.getName());
+            mail.getMessage().saveChanges();
+        } catch (Exception e) {
+            LOGGER.error("Cannot record mail name in headers", e);
+        }
+    }
+
+    @Override
+    public String getMailetInfo() {
+        return "SaveMailNameInHeaders Mailet";
+    }
+}
diff --git 
a/mailet/standard/src/test/java/org/apache/james/transport/mailets/SaveMailNameInHeadersTest.java
 
b/mailet/standard/src/test/java/org/apache/james/transport/mailets/SaveMailNameInHeadersTest.java
new file mode 100644
index 0000000000..c34435942f
--- /dev/null
+++ 
b/mailet/standard/src/test/java/org/apache/james/transport/mailets/SaveMailNameInHeadersTest.java
@@ -0,0 +1,73 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+package org.apache.james.transport.mailets;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import java.util.Properties;
+
+import jakarta.mail.Session;
+import jakarta.mail.internet.MimeMessage;
+
+import org.apache.mailet.base.test.FakeMail;
+import org.apache.mailet.base.test.FakeMailetConfig;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+public class SaveMailNameInHeadersTest {
+
+    private SaveMailNameInHeaders testee;
+
+    @BeforeEach
+    public void setUp() throws Exception {
+        testee = new SaveMailNameInHeaders();
+        testee.init(FakeMailetConfig.builder()
+            .mailetName("SaveMailNameInHeaders")
+            .build());
+    }
+
+    @Test
+    public void shouldRecordMailName() throws Exception {
+        MimeMessage mimeMessage = new 
MimeMessage(Session.getDefaultInstance(new Properties()));
+        FakeMail mail = FakeMail.builder()
+            .name("mail")
+            .mimeMessage(mimeMessage)
+            .build();
+
+        testee.service(mail);
+
+        
assertThat(mail.getMessage().getHeader("X-JAMES-Mail-Name")).contains("mail");
+    }
+
+    @Test
+    public void shouldResetValueWhenProcessedSeveralTimes() throws Exception {
+        MimeMessage mimeMessage = new 
MimeMessage(Session.getDefaultInstance(new Properties()));
+        mimeMessage.addHeader("X-JAMES-Mail-Name", "oldValue");
+        mimeMessage.setHeader("Message-ID", "<[email protected]>");
+        FakeMail mail = FakeMail.builder()
+            .name("mail")
+            .mimeMessage(mimeMessage)
+            .build();
+
+        testee.service(mail);
+
+        
assertThat(mail.getMessage().getHeader("X-JAMES-Mail-Name")).containsExactly("mail");
+    }
+}
+


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to