Skip to content

Commit 70bcc91

Browse files
committed
Add FileName property to Message class
1 parent 4128d80 commit 70bcc91

5 files changed

Lines changed: 56 additions & 25 deletions

File tree

‎MsgReader/LanguageConsts.cs‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,15 @@ internal static class LanguageConsts
8282
/// </summary>
8383
public const string CategoriesLabel = "Categories";
8484

85-
// The date format used for dates
85+
/// <summary>
86+
/// The format used for all date related items
87+
/// </summary>
8688
public const string DataFormat = "dd-MM-yyyy HH:mm:ss";
89+
90+
/// <summary>
91+
/// Normally Outlook will use the subject of a MSG object as it filename. Invalid characters
92+
/// are replaced by spaces. When there is no subject outlook uses "Nameless" as default
93+
/// </summary>
94+
public const string NameLessFileName = "Nameless";
8795
}
8896
}

‎MsgReader/Outlook/Storage.cs‎

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -301,47 +301,43 @@ public class Attachment : Storage
301301
{
302302
#region Properties
303303
/// <summary>
304-
/// Gets the filename.
304+
/// Returns the filename of the attachment
305305
/// </summary>
306-
/// <value> The filename. </value>
307-
public string Filename
306+
public string FileName
308307
{
309308
get
310309
{
311-
var filename = GetMapiPropertyString(Consts.PrAttachLongFilename);
310+
var fileName = GetMapiPropertyString(Consts.PrAttachLongFilename);
312311

313-
if (string.IsNullOrEmpty(filename))
314-
filename = GetMapiPropertyString(Consts.PrAttachFilename);
312+
if (string.IsNullOrEmpty(fileName))
313+
fileName = GetMapiPropertyString(Consts.PrAttachFilename);
315314

316-
if (string.IsNullOrEmpty(filename))
317-
filename = GetMapiPropertyString(Consts.PrDisplayName);
315+
if (string.IsNullOrEmpty(fileName))
316+
fileName = GetMapiPropertyString(Consts.PrDisplayName);
318317

319-
return filename;
318+
return FileManager.RemoveInvalidFileNameChars(fileName);
320319
}
321320
}
322321

323322
/// <summary>
324-
/// Gets the data.
323+
/// Retuns the data
325324
/// </summary>
326-
/// <value> The data. </value>
327325
public byte[] Data
328326
{
329327
get { return GetMapiPropertyBytes(Consts.PrAttachData); }
330328
}
331329

332330
/// <summary>
333-
/// Gets the content id.
331+
/// Returns the content id
334332
/// </summary>
335-
/// <value> The content id. </value>
336333
public string ContentId
337334
{
338335
get { return GetMapiPropertyString(Consts.PrAttachContentId); }
339336
}
340337

341338
/// <summary>
342-
/// Gets the rendering posisiton.
339+
/// Returns the rendering posisiton
343340
/// </summary>
344-
/// <value> The rendering posisiton. </value>
345341
public int RenderingPosisiton
346342
{
347343
get { return GetMapiPropertyInt32(Consts.PrRenderingPosition); }
@@ -404,6 +400,24 @@ public string Type
404400
get { return GetMapiPropertyString(Consts.PrMessageClass); }
405401
}
406402

403+
/// <summary>
404+
/// Returns the filename of the message object. For MSG object Outlook uses the subject. It strips
405+
/// invalid filename characters. When there is no filename the name from <see cref=" LanguageConsts.NameLessFileName"/>
406+
/// will be used
407+
/// </summary>
408+
public string FileName
409+
{
410+
get
411+
{
412+
var fileName = GetMapiPropertyString(Consts.PrSubject);
413+
414+
if (string.IsNullOrEmpty(fileName))
415+
fileName = LanguageConsts.NameLessFileName;
416+
417+
return FileManager.RemoveInvalidFileNameChars(fileName);
418+
}
419+
}
420+
407421
/// <summary>
408422
/// Gets the list of recipients in the outlook message.
409423
/// </summary>
@@ -1078,7 +1092,7 @@ public Task(Storage message) : base(message._storage)
10781092
}
10791093
#endregion
10801094

1081-
#region Properties
1095+
#region Fields
10821096
/// <summary>
10831097
/// The statistics for all streams in the IStorage associated with this instance.
10841098
/// </summary>
@@ -1103,7 +1117,9 @@ public Task(Storage message) : base(message._storage)
11031117
/// The IStorage associated with this instance.
11041118
/// </summary>
11051119
private NativeMethods.IStorage _storage;
1120+
#endregion
11061121

1122+
#region Properties
11071123
/// <summary>
11081124
/// Gets the top level outlook message from a sub message at any level.
11091125
/// </summary>

‎MsgReader/Reader.cs‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,7 @@ private List<string> WriteEmail(Storage.Message message, string outputFolder, bo
136136
if (attachment.GetType() == typeof (Storage.Attachment))
137137
{
138138
var attach = (Storage.Attachment) attachment;
139-
fileInfo = new FileInfo(
140-
FileManager.FileExistsMakeNew(outputFolder +
141-
FileManager.RemoveInvalidFileNameChars(attach.Filename)));
139+
fileInfo = new FileInfo(FileManager.FileExistsMakeNew(outputFolder + attach.FileName));
142140
File.WriteAllBytes(fileInfo.FullName, attach.Data);
143141

144142
// When we find an inline attachment we have to replace the CID tag inside the html body
@@ -156,9 +154,8 @@ private List<string> WriteEmail(Storage.Message message, string outputFolder, bo
156154
else if (attachment.GetType() == typeof (Storage.Message))
157155
{
158156
var msg = (Storage.Message) attachment;
159-
fileInfo = new FileInfo(
160-
FileManager.FileExistsMakeNew(outputFolder +
161-
FileManager.RemoveInvalidFileNameChars(msg.Subject) + ".msg"));
157+
158+
fileInfo = new FileInfo(FileManager.FileExistsMakeNew(outputFolder + msg.FileName) + ".msg");
162159
result.Add(fileInfo.FullName);
163160
msg.Save(fileInfo.FullName);
164161
}

‎MsgViewer/MsgViewer.csproj‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
<DefineConstants>DEBUG;TRACE</DefineConstants>
2929
<ErrorReport>prompt</ErrorReport>
3030
<WarningLevel>4</WarningLevel>
31+
<UseVSHostingProcess>false</UseVSHostingProcess>
3132
</PropertyGroup>
3233
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
3334
<PlatformTarget>AnyCPU</PlatformTarget>

‎MsgViewer/ViewerForm.cs‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,21 @@ private void SelectButton_Click(object sender, EventArgs e)
5252
var msgReader = new Reader();
5353
var files = msgReader.ExtractToFolder(openFileDialog1.FileName, tempFolder);
5454

55-
webBrowser1.Navigate(files[0]);
55+
// Check if there was an error
56+
var error = msgReader.GetErrorMessage();
57+
58+
if (!string.IsNullOrEmpty(error))
59+
throw new Exception(error);
60+
61+
if (!string.IsNullOrEmpty(files[0]))
62+
webBrowser1.Navigate(files[0]);
5663
}
57-
catch (Exception)
64+
catch (Exception ex)
5865
{
5966
if (tempFolder != null && Directory.Exists(tempFolder))
6067
Directory.Delete(tempFolder, true);
68+
69+
MessageBox.Show(ex.Message);
6170
}
6271
}
6372
}

0 commit comments

Comments
 (0)