Skip to content

Commit 62d6990

Browse files
committed
Merge branch 'docs/conversion-26.10-use-cases' into staging
2 parents de9b5f8 + bed298d commit 62d6990

3 files changed

Lines changed: 640 additions & 0 deletions

File tree

‎net/use-cases/_index.md‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
id: use-cases
3+
url: conversion/net/use-cases
4+
title: Use cases
5+
weight: 4
6+
description: "Complete scenarios for GroupDocs.Conversion for .NET: converting untrusted HTML and SVG uploads safely, and converting email messages with their attachments to PDF."
7+
keywords: GroupDocs.Conversion use cases, convert untrusted uploads, email attachments to PDF, .NET document conversion scenarios
8+
productName: GroupDocs.Conversion for .NET
9+
hideChildren: True
10+
toc: False
11+
---
12+
13+
Complete scenarios for GroupDocs.Conversion for .NET. Each page solves one real problem end to end, with a runnable sample project on GitHub:
14+
15+
* [Convert untrusted uploads safely]({{< ref "conversion/net/use-cases/convert-untrusted-uploads-safely.md" >}}) — convert HTML and SVG files your users upload without letting them reach the network, read other files on the server or run scripts.
16+
* [Convert email with attachments to PDF]({{< ref "conversion/net/use-cases/convert-email-with-attachments-to-pdf.md" >}}) — turn a message into one PDF, or the message and every attachment into separate files that never overwrite each other.
Lines changed: 265 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,265 @@
1+
---
2+
id: convert-email-with-attachments-to-pdf
3+
url: conversion/net/use-cases/convert-email-with-attachments-to-pdf
4+
title: Convert email with attachments to PDF
5+
weight: 2
6+
description: "A technical walkthrough of 3 code patterns for converting an Outlook message and its attachments to PDF with GroupDocs.Conversion for .NET, with the trade-offs between them."
7+
keywords: email to PDF, MSG to PDF, EML to PDF, email attachments, EmailLoadOptions, SaveContext, convert attachments to PDF
8+
productName: GroupDocs.Conversion for .NET
9+
hideChildren: False
10+
toc: True
11+
---
12+
13+
{{< alert style="info" >}}
14+
💡 Full working example available on GitHub:
15+
[convert-email-and-attachments-to-pdf-dotnet](https://github.com/groupdocs-conversion/convert-email-and-attachments-to-pdf-dotnet)
16+
{{< /alert >}}
17+
18+
## Executive Summary
19+
20+
Email-with-attachments conversion is a GroupDocs.Conversion capability for
21+
.NET that turns an Outlook message and its attachments into PDF output,
22+
either as one PDF describing the message or as a separate PDF per item. The
23+
underlying API stays the same across all three patterns below:
24+
`EmailLoadOptions` on the load side, `SaveContext` on the save side. What
25+
changes is which options are set and how the output path gets built.
26+
27+
## What This Guide Covers
28+
29+
This guide walks through `EmailLoadOptions` and `SaveContext` on
30+
`Converter.Convert`, using a sample Outlook `.msg` message and a sample
31+
support `.eml` message as the running examples. Every output named below
32+
comes from running the sample against those two files.
33+
34+
**Prerequisites:**
35+
- .NET SDK 8.0 or later
36+
- GroupDocs.Conversion for .NET 26.10.0 or later
37+
38+
Install the package with NuGet:
39+
40+
```bash
41+
dotnet add package GroupDocs.Conversion --version 26.10.0
42+
```
43+
44+
## Implementation Patterns Overview
45+
46+
Three patterns cover the practical range of this problem. Pattern 1
47+
converts only the message. Pattern 2 converts the message and every
48+
attachment, each as its own file. Pattern 3 adds the handling a real
49+
inbox eventually needs: attachments that share a file name, and
50+
attachments with no route to PDF at all.
51+
52+
| Pattern | Output | `SaveContext` needed | Name clashes | Unsupported attachments |
53+
|---------|--------|----------------------|---------------|--------------------------|
54+
| Message only | 1 PDF | No | Not applicable | Not applicable |
55+
| Message and every attachment | One PDF per item | Yes | Needs `ItemIndex` | Not handled |
56+
| Duplicates and no-route attachments | One PDF per item, plus pass-through files | Yes | Handled via `ItemIndex` | Handled via `TargetFormat` |
57+
58+
## Pattern 1: Message Only
59+
60+
### Design Overview
61+
62+
This is the simplest of the three. `EmailLoadOptions.ConvertOwner` set to
63+
`true` and `ConvertOwned` set to `false` treats the message as the only
64+
thing worth converting. Attachments appear by name only.
65+
66+
### Implementation
67+
68+
```csharp
69+
var loadOptions = new EmailLoadOptions
70+
{
71+
ConvertOwner = true,
72+
ConvertOwned = false
73+
};
74+
```
75+
76+
```csharp
77+
using (var converter = new Converter(messagePath, (LoadContext _) => loadOptions))
78+
{
79+
converter.Convert(outputPath, new PdfConvertOptions());
80+
}
81+
```
82+
83+
### Technical Notes
84+
85+
A plain output path is enough here. No callback is involved. Running this
86+
against the sample Outlook message produces a one-page PDF whose header
87+
lists `From`, `Sent`, `To`, `Subject` and `Attachments`, followed by the
88+
body. The trade-off is simple: smaller output, but the attachment content
89+
never reaches a page.
90+
91+
## Pattern 2: Message and Every Attachment
92+
93+
### Design Overview
94+
95+
Setting both `ConvertOwner` and `ConvertOwned` to `true`, with `Depth`
96+
left at its default of `2`, makes the converter call back once per output
97+
item instead of writing to one fixed path. `SaveContext.ItemIndex` starts
98+
at 1 for the message and counts up for each attachment that follows.
99+
100+
Two attachments, three files. The sample Outlook message carries an image
101+
and a Word document, and the message itself is item one, not item zero.
102+
103+
### Implementation
104+
105+
```csharp
106+
var loadOptions = new EmailLoadOptions
107+
{
108+
ConvertOwner = true,
109+
ConvertOwned = true,
110+
Depth = 2
111+
};
112+
int written = 0;
113+
```
114+
115+
```csharp
116+
using (var converter = new Converter(messagePath, (LoadContext _) => loadOptions))
117+
{
118+
converter.Convert((SaveContext item) =>
119+
{
120+
written++;
121+
string name = $"{item.ItemIndex:D2}-{item.SourceFileName}.pdf";
122+
return File.Create(Path.Combine(outputFolder, name));
123+
}, new PdfConvertOptions());
124+
}
125+
```
126+
127+
### Technical Notes
128+
129+
`SaveContext.HierarchyLevel` is 0 for the message and 1 for an
130+
attachment. That one field is enough to tell the two apart inside the
131+
callback. Point this pattern at the sample Outlook message, and the
132+
message lands first, then the image attachment, then the Word
133+
attachment, one call for each.
134+
135+
## Pattern 3: Duplicates and No-Route Attachments
136+
137+
### Design Overview
138+
139+
Two attachments can carry the same file name. One attachment can have no
140+
conversion route to PDF at all. Pattern 3 handles both by reading two
141+
fields off `SaveContext` instead of one: `ItemIndex` for naming, and
142+
`TargetFormat` for deciding whether to convert at all.
143+
144+
### Where do two identically named attachments end up on disk?
145+
146+
Each one keeps its own numbered file. The sample support e-mail attaches
147+
two files both called `report.csv`; the first becomes `02-report.csv.pdf`
148+
and the second becomes `03-report.csv.pdf`, prefixed by their distinct
149+
`ItemIndex` values. Neither file touches the other on disk, even though
150+
the two source names are identical.
151+
152+
### Implementation
153+
154+
```csharp
155+
var loadOptions = new EmailLoadOptions { ConvertOwner = true, ConvertOwned = true };
156+
int keptAsTheyAre = 0;
157+
```
158+
159+
```csharp
160+
using (var converter = new Converter(messagePath, (LoadContext _) => loadOptions))
161+
{
162+
converter.Convert((SaveContext item) =>
163+
{
164+
bool passedThrough = item.TargetFormat == FileType.Unknown;
165+
string baseName = $"{item.ItemIndex:D2}-{item.SourceFileName}";
166+
string name = passedThrough
167+
? baseName
168+
: $"{baseName}.{item.TargetFormat.Extension}";
169+
keptAsTheyAre += passedThrough ? 1 : 0;
170+
return File.Create(Path.Combine(outputFolder, name));
171+
}, new PdfConvertOptions());
172+
}
173+
```
174+
175+
### Technical Notes
176+
177+
`item.TargetFormat == FileType.Unknown` is the one check that separates
178+
this pattern from Pattern 2. When it is true, the item is handed back
179+
unconverted. Diffing the kept attachment against the saved file turns up
180+
nothing: all 32 bytes match. The sample's console marks every item as
181+
`converted` or `kept`, so the run log alone shows which attachment stayed
182+
in its original format.
183+
184+
## Operational Considerations
185+
186+
Wrap every `Converter` in a `using` block. It implements `IDisposable`,
187+
and all three patterns share this requirement equally. Build output paths with
188+
`Path.Combine` rather than string concatenation, so the file names stay
189+
correct regardless of which operating system runs the console app. Run the
190+
sample from its project folder: the inputs are read from the relative
191+
`resources/` folder, the message-only PDF lands in `output/`, and the
192+
other two patterns each write to a subfolder of their own.
193+
194+
## Real-World Use Cases
195+
196+
**Support case archiving.** A support team receives e-mails with several
197+
spreadsheet attachments that need to be filed and searched independently
198+
of the message. Pattern 2 turns the message and every attachment into a
199+
separately indexable PDF, named from `ItemIndex` and `SourceFileName`.
200+
201+
**Mixed-format support inbox.** An inbox mixes CSV reports with an
202+
occasional capture file that has no conversion route to PDF. Pattern 3
203+
converts every CSV attachment, keeps duplicate names separate, and lets
204+
the unsupported attachment survive the run under its own name.
205+
206+
## Common Pitfalls
207+
208+
1. Naming outputs from `SourceFileName` alone. Two attachments with the
209+
same name overwrite each other; always include `ItemIndex` in the
210+
output file name instead.
211+
2. Assuming every attachment becomes a PDF. An attachment with no route
212+
to PDF is handed back unchanged, so code that always appends `.pdf`
213+
saves its original bytes under a misleading name. Branch on
214+
`item.TargetFormat == FileType.Unknown` first.
215+
3. Leaving `Depth` implicit. It limits how far attachments inside
216+
attached messages are followed, so set it explicitly when attached
217+
messages carry attachments of their own.
218+
219+
## Notes
220+
221+
`EmailLoadOptions` defaults to `ConvertOwner = true`, `ConvertOwned = true`
222+
and `Depth = 2`. For a passed-through item `TargetFormat` is
223+
`FileType.Unknown`, so its name comes from `SourceFileName` alone.
224+
225+
## FAQ
226+
227+
**Q: Which pattern should I start with?**
228+
A: Pattern 1 for a message-only record. Move to Pattern 2 once any
229+
attachment needs its own PDF, and add Pattern 3's `TargetFormat` check
230+
once the inbox can contain a format GroupDocs.Conversion cannot route to
231+
PDF.
232+
233+
**Q: Can the three patterns run side by side in the same program?**
234+
A: Yes. They are independent `Converter` calls over the same input file,
235+
so nothing stops a program from running Pattern 1 for a summary PDF and
236+
Pattern 3 for the full breakdown in the same request.
237+
238+
**Q: What happens to an attachment that cannot be converted to PDF?**
239+
A: Since GroupDocs.Conversion for .NET 26.10, it no longer stops the
240+
conversion. The callback still receives it, `TargetFormat` equal to
241+
`FileType.Unknown`. In 26.9 this failed the whole message with a
242+
`ConversionNotSupportedException`.
243+
244+
**Q: Is a license required to run these patterns?**
245+
A: Without one the sample runs in evaluation mode, with its limits. Apply
246+
a GroupDocs.Conversion license, or request a
247+
[temporary license](https://purchase.groupdocs.com/temp-license/100032)
248+
while testing.
249+
250+
## Conclusion and Recommendations
251+
252+
All three patterns sit on the same two building blocks: `EmailLoadOptions`
253+
for what gets converted, `SaveContext` for how each output is named. Start
254+
with whichever pattern matches today's requirement, and move to the next
255+
one only when the inbox actually demands it. There is no benefit to
256+
building Pattern 3's handling before a duplicate name or an unsupported
257+
format shows up in real data.
258+
259+
## See Also
260+
261+
- [In-depth blog article about this project](https://blog.groupdocs.com/conversion/convert-email-with-attachments-to-pdf-net/)
262+
- [Load options for e-mail documents](https://docs.groupdocs.com/conversion/net/load-email-document-with-options/)
263+
- [Converting each email attachment to a different format](https://docs.groupdocs.com/conversion/net/convert-each-email-attachment-to-different-format/)
264+
- [GitHub repository](https://github.com/groupdocs-conversion/convert-email-and-attachments-to-pdf-dotnet)
265+
- [GroupDocs.Conversion for .NET documentation](https://docs.groupdocs.com/conversion/net/)

0 commit comments

Comments
 (0)