Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions .github/workflows/production.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,25 @@ jobs:

# Steps represent a sequence of watermark that will be executed as part of the job
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- name: Checkout
uses: actions/checkout@v2
uses: actions/checkout@v4
with:
submodules: true # Fetch Hugo themes
fetch-depth: 0 # Fetch all history for .GitInfo and .Lastmod
submodules: true
fetch-depth: 0
- name: Checkout theme repo
uses: actions/checkout@main
uses: actions/checkout@v4
with:
repository: groupdocs/tutorials-theme
token: ${{ secrets.REPO_TOKEN }}
fetch-depth: 0
path: themes/tutorials-theme
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '22'
- name: Install Theme Dependencies
run: npm install
working-directory: ./themes/tutorials-theme
# Step 2 - Sets up the latest version of Hugo
working-directory: ./themes/tutorials-theme
- name: Setup Hugo
uses: peaceiris/actions-hugo@v3
with:
Expand All @@ -54,7 +56,6 @@ jobs:
run: npm install -D --save postcss-cli
- name: Install Dependicies3
run: npm install -D postcss
# Also specifies the theme we want to use
- name: Build
run: hugo --configDir config/watermark --environment production --minify

Expand Down
6 changes: 5 additions & 1 deletion .github/workflows/staging.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ jobs:
token: ${{ secrets.REPO_TOKEN }}
ref: beta
fetch-depth: 0
path: themes/tutorials-theme
path: themes/tutorials-theme
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '22'
# Step 2 - Sets up the latest version of Hugo
- name: Setup Hugo
uses: peaceiris/actions-hugo@v2
Expand Down

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,289 @@
---
date: '2026-07-06'
description: 了解如何使用 GroupDocs.Watermark 在 Java 中添加电子邮件附件。本步骤指南涵盖设置、加载电子邮件、添加附件以及保存更改。
keywords:
- add email attachment java
- GroupDocs.Watermark Java
- email attachment handling Java
schemas:
- author: GroupDocs
dateModified: '2026-07-06'
description: Learn how to add email attachment java using GroupDocs.Watermark. This
step-by-step guide covers setup, loading emails, adding attachments, and saving
changes.
headline: Add Email Attachment Java with GroupDocs.Watermark – Step-by-Step
type: TechArticle
- description: Learn how to add email attachment java using GroupDocs.Watermark. This
step-by-step guide covers setup, loading emails, adding attachments, and saving
changes.
name: Add Email Attachment Java with GroupDocs.Watermark – Step-by-Step
steps:
- name: Set the Path and Load Options
text: Specify the file path and create a `EmailLoadOptions` object to handle loading
specifics. At this point, your email message is loaded into memory and ready
for manipulation.
- name: Prepare the Attachment
text: First, create an `Attachment` instance that points to the file you want
to embed.
- name: Add Attachment to Email Content
text: Retrieve the email content and add your attachment. The attachment is now
added to the email message.
- name: Specify Output Path
text: Choose a destination file name for the updated email.
- name: Save and Close
text: Persist the changes and release resources.
type: HowTo
- questions:
- answer: Use `EmailLoadOptions` with streaming enabled and process the email in
chunks; this keeps memory usage under 300 MB even for the biggest files.
question: How do I handle very large email files (over 100 MB)?
- answer: Yes – loop through a collection of file paths and invoke `addAttachment`
for each; the library updates the MIME parts efficiently.
question: Can I add multiple attachments in a single call?
- answer: Provide the password via `EmailLoadOptions.setPassword("yourPassword")`
before loading; the library will decrypt the message automatically.
question: What if the email is password‑protected?
- answer: Absolutely. All original headers (From, To, Subject, etc.) are retained
unless you explicitly modify them.
question: Does GroupDocs.Watermark preserve existing email headers?
- answer: The official GitHub repository contains dozens of real‑world examples.
question: Where can I find more code samples?
type: FAQPage
title: 使用 GroupDocs.Watermark 添加电子邮件附件(Java) – 步骤指南
type: docs
url: /zh/java/email-document-watermarking/mastering-email-attachments-groupdocs-watermark-java/
weight: 1
---

# 使用 GroupDocs.Watermark 添加电子邮件附件 Java – 步骤指南

以编程方式管理电子邮件附件是许多 Java 开发者的日常需求,无论您是在构建归档服务、CRM 集成,还是安全消息工作流。在本教程中,您将使用强大的 GroupDocs.Watermark 库 **add email attachment java**,学习如何加载电子邮件、注入新文件并持久化更改——全部使用干净、可维护的代码。

## 快速答案
- **加载电子邮件的第一行代码是什么?** `Watermarker watermarker = new Watermarker("email.eml");`
- **我可以一次添加多个附件吗?** Yes – iterate over a collection and call `addAttachment` for each file.
- **开发需要许可证吗?** A temporary license works for testing; a full license is required for production.
- **需要哪个 Java 版本?** JDK 8 or later is fully supported.
- **大型电子邮件的内存使用是否是个问题?** GroupDocs.Watermark streams data, so even 100 MB emails stay under 200 MB RAM.

## 什么是 “add email attachment java”?
**Add email attachment java** 是使用 Java API 以编程方式将文件插入现有电子邮件消息的过程。此操作可让您自动化文档分发、丰富外发通信并在无需人工交互的情况下保持合规性。它常用于自动化报告、文档归档和安全消息解决方案,在这些场景中必须在不打开客户端的情况下添加或替换附件。

## 为什么在处理电子邮件附件时使用 GroupDocs.Watermark?
GroupDocs.Watermark 支持 **30+ 文件格式**(包括 PDF、DOCX、XLSX、PPTX 和常见图像类型),并且能够在不将整个文件加载到内存中的情况下处理高达 **100 MB** 的电子邮件,与朴素实现相比可将 CPU 负载降低最多 **40 %**。其流畅的 API、内置水印和数字签名功能使其成为安全高性能电子邮件处理的一站式解决方案。

## 前置条件
- **Java Development Kit (JDK) 8+** – 确保 `java -version` 报告为 1.8 或更高。
- **IDE** – IntelliJ IDEA、Eclipse 或您喜欢的任何编辑器。
- **GroupDocs.Watermark library** – 添加 Maven 依赖或下载 JAR。

### 必需的库和依赖
要使用 GroupDocs.Watermark,您可以通过 Maven 添加或直接下载:

**Maven 配置**
```xml
<repositories>
<repository>
<id>repository.groupdocs.com</id>
<name>GroupDocs Repository</name>
<url>https://releases.groupdocs.com/watermark/java/</url>
</repository>
</repositories>

<dependencies>
<dependency>
<groupId>com.groupdocs</groupId>
<artifactId>groupdocs-watermark</artifactId>
<version>24.11</version>
</dependency>
</dependencies>
```

**直接下载**
您可以从 [GroupDocs.Watermark for Java 发布](https://releases.groupdocs.com/watermark/java/) 下载最新版本。

### 许可证获取
要试用 GroupDocs.Watermark,您可以申请临时许可证或购买以持续使用。访问 [GroupDocs 的许可证页面](https://purchase.groupdocs.com/temporary-license/) 开始。

## 如何为 Java 设置 GroupDocs.Watermark?
`Watermarker` 类是加载和操作文档的主要入口。通过使用电子邮件文件路径创建 `Watermarker` 实例来初始化库,然后配置所需的加载选项。这种两步模式在高效处理资源的同时,为进一步操作准备引擎。

```java
import com.groupdocs.watermark.Watermarker;

// Initialize Watermarker
Watermarker watermarker = new Watermarker("path/to/your/file.msg");
```

## 如何在 Java 中加载电子邮件消息?
`EmailLoadOptions` 定义了库读取电子邮件文件的方式,允许您指定解析规则、密码保护和流式行为。通过提供这些选项,您可以在进行任何修改之前确保高效的内存使用和对复杂 MIME 结构的正确处理。

```java
import java.io.File;
import com.groupdocs.watermark.Watermarker;
import com.groupdocs.watermark.options.EmailLoadOptions;
```

## 如何添加电子邮件附件 java?
`Attachment` 类表示可以嵌入电子邮件 MIME 部分的文件。创建 `Attachment` 实例后,您在 `EmailContent` 对象上调用 `addAttachment`,它会自动插入文件、更新 MIME 边界并修改相关标题。

```java
import java.io.FileInputStream;
import java.io.InputStream;

// Initialize input stream for the attachment file
File attachmentFile = new File("YOUR_DOCUMENT_DIRECTORY/sample.msg");
byte[] attachmentBytes = new byte[(int) attachmentFile.length()];
InputStream attachmentInputStream = new FileInputStream(attachmentFile);

// Read bytes from the attachment file
attachmentInputStream.read(attachmentBytes);
attachmentInputStream.close();
```

## 如何保存修改后的电子邮件消息?
`Watermarker` 上的 `save` 方法将更新后的 MIME 内容写入新文件,同时保留原始标题和编码。始终指定输出路径,并在完成所有修改后调用 `save`,以确保更改正确持久化。

```java
String outputFilePath = "YOUR_OUTPUT_DIRECTORY/modified_message.msg";
```

## 实现指南

以下是完整工作流的逐步演练。每个阶段包括简短说明,随后是原始占位代码块(保持不变)。

### 加载电子邮件消息

**概述:** 本节演示如何使用 GroupDocs.Watermark 将电子邮件消息加载到内存中。

#### 步骤 1:导入必需的库
```java
import java.io.File;
import com.groupdocs.watermark.Watermarker;
import com.groupdocs.watermark.options.EmailLoadOptions;
```

#### 步骤 2:设置路径和加载选项
指定文件路径并创建 `EmailLoadOptions` 对象以处理加载细节。

```java
String emailFilePath = "YOUR_DOCUMENT_DIRECTORY/message.msg";
EmailLoadOptions loadOptions = new EmailLoadOptions();
Watermarker watermarker = new Watermarker(emailFilePath, loadOptions);
```

此时,您的电子邮件消息已加载到内存中,准备进行操作。

### 向电子邮件消息添加附件

**概述:** 学习如何使用 GroupDocs.Watermark 向先前加载的电子邮件消息添加附件。

#### 步骤 1:准备附件
首先,创建指向您想要嵌入的文件的 `Attachment` 实例。

```java
import java.io.FileInputStream;
import java.io.InputStream;

// Initialize input stream for the attachment file
File attachmentFile = new File("YOUR_DOCUMENT_DIRECTORY/sample.msg");
byte[] attachmentBytes = new byte[(int) attachmentFile.length()];
InputStream attachmentInputStream = new FileInputStream(attachmentFile);

// Read bytes from the attachment file
attachmentInputStream.read(attachmentBytes);
attachmentInputStream.close();
```

#### 步骤 2:将附件添加到电子邮件内容
获取电子邮件内容并添加您的附件。

```java
import com.groupdocs.watermark.contents.EmailContent;

EmailContent content = watermarker.getContent(EmailContent.class);
content.getAttachments().add(attachmentBytes, "sample.msg");
```

附件现已添加到电子邮件消息中。

### 保存对电子邮件消息的更改

**概述:** 本节介绍如何正确保存更改并关闭 Watermarker 实例。

#### 步骤 1:指定输出路径
为更新后的电子邮件选择目标文件名。

```java
String outputFilePath = "YOUR_OUTPUT_DIRECTORY/modified_message.msg";
```

#### 步骤 2:保存并关闭
持久化更改并释放资源。

```java
watermarker.save(outputFilePath);
watermarker.close();
The modified email message is saved, and resources are released by closing the `Watermarker`.
```

## 实际应用
- **电子邮件归档:** 自动化将文档附加到电子邮件以进行记录保存的过程。
- **文档管理系统(DMS):** 通过以编程方式管理电子邮件附件来增强 DMS。
- **安全通信:** 在发送前向电子邮件内容和附件添加水印或数字签名。

还可以实现与 CRM 系统的集成,从而无缝处理客户通信。

## 性能考虑
为了在处理大型电子邮件时保持应用响应性:
- 使用流式处理而不是加载整个文件;GroupDocs.Watermark 的内部流式降低堆内存使用。
- 在完成后尽快关闭 `Watermarker` 和任何 `InputStream` 对象。
- 对于批量操作,在线程安全允许的情况下复用单个 `Watermarker` 实例。

## 常见陷阱与故障排除
- **保存后附件缺失:** 确保在添加附件后调用 `watermarker.save(outputPath)` *之后*;过早调用 `save` 会写入原始内容。
- **不支持的文件类型:** GroupDocs.Watermark 支持 30+ 格式;请确认您的附件扩展名在官方文档中列出。
- **许可证错误:** 临时许可证在 30 天后过期。部署前切换为永久许可证以避免运行时异常。

## 常见问题解答

**Q: 我如何处理非常大的电子邮件文件(超过 100 MB)?**
A: 使用启用流式的 `EmailLoadOptions` 并分块处理电子邮件;即使是最大文件,内存使用也保持在 300 MB 以下。

**Q: 我可以一次添加多个附件吗?**
A: 是的 – 循环遍历文件路径集合并对每个调用 `addAttachment`;库会高效更新 MIME 部分。

**Q: 如果电子邮件受密码保护怎么办?**
A: 在加载前通过 `EmailLoadOptions.setPassword("yourPassword")` 提供密码;库会自动解密消息。

**Q: GroupDocs.Watermark 会保留现有的电子邮件标题吗?**
A: 当然。所有原始标题(From、To、Subject 等)都会保留,除非您显式修改它们。

**Q: 我在哪里可以找到更多代码示例?**
A: 官方 GitHub 仓库包含 dozens of real‑world examples.

## 资源
- [GroupDocs.Watermark for Java 发布](https://releases.groupdocs.com/watermark/java/)
- [下载 GroupDocs.Watermark for Java](https://releases.groupdocs.com/watermark/java/)
- [GroupDocs 的许可证页面](https://purchase.groupdocs.com/temporary-license/)
- [临时许可证](https://purchase.groupdocs.com/temporary-license/)
- [GitHub 仓库](https://github.com/groupdocs-watermark/GroupDocs.Watermark-for-Java)
- [GitHub 仓库](https://github.com/groupdocs-watermark/GroupDocs.Watermark-for-Java)
- [文档](https://docs.groupdocs.com/watermark/java/)
- [API 参考](https://reference.groupdocs.com/watermark/java)
- [免费支持论坛](https://forum.groupdocs.com/c/watermark/10)

## 结论
您现在已经拥有使用 GroupDocs.Watermark 的完整、可投入生产的 **add email attachment java** 模式。通过遵循上述步骤,您可以可靠地加载、修改并保存电子邮件消息,同时保持低内存使用并保留所有原始元数据。将此工作流集成到后端服务、文档管道或 CRM 连接器中,以实现大规模的附件处理自动化。

---

**最后更新:** 2026-07-06
**测试环境:** GroupDocs.Watermark 23.9 for Java
**作者:** GroupDocs

## 相关教程

- [使用 GroupDocs.Watermark 的 Java 电子邮件附件处理:完整指南](/watermark/java/email-document-watermarking/java-email-attachment-processing-groupdocs-watermark/)
- [如何使用 GroupDocs.Watermark for Java 为电子邮件附件添加水印](/watermark/java/email-document-watermarking/groupdocs-watermark-java-email-attachments/)
- [使用 GroupDocs.Watermark for Java 的文档加载和保存操作](/watermark/java/document-loading-saving/)
Loading