Skip to content

Commit 40f15e4

Browse files
committed
Improved FAQ and examples for getting/using filenames
1 parent 763341a commit 40f15e4

2 files changed

Lines changed: 26 additions & 2 deletions

File tree

Documentation/Examples/AttachmentExamples.cs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,27 @@ public static void SaveAttachments (MimeMessage message)
2727
#region SaveAttachments
2828
foreach (var attachment in message.Attachments) {
2929
if (attachment is MessagePart) {
30-
var fileName = attachment.ContentDisposition?.FileName;
30+
var fileName = attachment.ContentDisposition?.FileName ?? attachment.ContentType.Name;
3131
var rfc822 = (MessagePart) attachment;
3232

3333
if (string.IsNullOrEmpty (fileName))
3434
fileName = "attached-message.eml";
3535

36+
// make sure that the filename value does not contain a full path or invalid path characters
37+
fileName = Path.GetFileName (fileName);
38+
3639
using (var stream = File.Create (fileName))
3740
rfc822.Message.WriteTo (stream);
3841
} else {
3942
var part = (MimePart) attachment;
4043
var fileName = part.FileName;
4144

45+
if (string.IsNullOrEmpty (fileName))
46+
fileName = "untitled.dat";
47+
48+
// make sure that the filename value does not contain a full path or invalid path characters
49+
fileName = Path.GetFileName (fileName);
50+
4251
using (var stream = File.Create (fileName))
4352
part.Content.DecodeTo (stream);
4453
}
@@ -54,18 +63,27 @@ public static void SaveAttachments (MimeMessage message)
5463
continue;
5564

5665
if (bodyPart is MessagePart) {
57-
var fileName = attachment.ContentDisposition?.FileName;
66+
var fileName = attachment.ContentDisposition?.FileName ?? attachment.ContentType.Name;
5867
var rfc822 = (MessagePart) attachment;
5968

6069
if (string.IsNullOrEmpty (fileName))
6170
fileName = "attached-message.eml";
6271

72+
// make sure that the filename value does not contain a full path or invalid path characters
73+
fileName = Path.GetFileName (fileName);
74+
6375
using (var stream = File.Create (fileName))
6476
rfc822.Message.WriteTo (stream);
6577
} else {
6678
var part = (MimePart) attachment;
6779
var fileName = part.FileName;
6880

81+
if (string.IsNullOrEmpty (fileName))
82+
fileName = "untitled.dat";
83+
84+
// make sure that the filename value does not contain a full path or invalid path characters
85+
fileName = Path.GetFileName (fileName);
86+
6987
using (var stream = File.Create (fileName))
7088
part.Content.DecodeTo (stream);
7189
}

FAQ.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -968,6 +968,12 @@ If you are iterating over all of the attachments in a message, you might do some
968968
foreach (var attachment in message.Attachments) {
969969
var fileName = attachment.ContentDisposition?.FileName ?? attachment.ContentType.Name;
970970

971+
if (string.IsNullOrEmpty (fileName))
972+
fileName = "untitled.dat";
973+
974+
// make sure that the filename value does not contain a full path or invalid path characters
975+
fileName = Path.GetFileName (fileName);
976+
971977
using (var stream = File.Create (fileName)) {
972978
if (attachment is MessagePart) {
973979
var rfc822 = (MessagePart) attachment;

0 commit comments

Comments
 (0)