@@ -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 }
0 commit comments