Skip to content

Commit e17a830

Browse files
Merge pull request pmengal#16 from reinaldocoelho/master
closes pmengal#15 - Bug fix and test to parse email without ContentType Subtype
2 parents b03f73d + 080a76e commit e17a830

5 files changed

Lines changed: 108 additions & 18 deletions

File tree

Class Library/ActiveUp.Net.Common/ContentType.cs

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,62 +19,86 @@
1919

2020
namespace ActiveUp.Net.Mail
2121
{
22+
/// <summary>
23+
/// ContentType Header Class.
24+
/// </summary>
2225
#if !PocketPC
2326
[Serializable]
2427
#endif
2528
public class ContentType : StructuredHeaderField
2629
{
2730
string _mimeType = "text/plain";
2831

29-
public string Type
32+
/// <summary>
33+
/// Mimetype original or Type + Subtype.
34+
/// </summary>
35+
public string MimeType
3036
{
3137
get
3238
{
33-
return _mimeType.Split('/')[0];
39+
return _mimeType;
3440
}
3541
set
3642
{
37-
_mimeType = value + "/" + SubType;
43+
_mimeType = value;
3844
}
3945
}
40-
public string SubType
46+
47+
/// <summary>
48+
/// The type of content-type
49+
/// </summary>
50+
public string Type
4151
{
4252
get
4353
{
44-
return _mimeType.Split('/')[1];
54+
var type = _mimeType.Split('/')[0];
55+
return type ?? "";
4556
}
4657
set
4758
{
48-
_mimeType = Type + "/" + value;
59+
_mimeType = value + "/" + SubType;
4960
}
5061
}
51-
public string MimeType
62+
63+
/// <summary>
64+
/// Subtype of content-type received, if invalid, return "plain"
65+
/// </summary>
66+
public string SubType
5267
{
5368
get
5469
{
55-
return _mimeType;
70+
var mimeParts = _mimeType.Split('/');
71+
if (mimeParts.Length < 2) return "plain";
72+
var subtype = _mimeType.Split('/')[1];
73+
subtype = string.IsNullOrWhiteSpace(subtype) ? "plain" : subtype;
74+
return subtype;
5675
}
5776
set
5877
{
59-
_mimeType = value;
78+
_mimeType = Type + "/" + value;
6079
}
6180
}
81+
82+
/// <summary>
83+
/// Override of method to explain content type string
84+
/// </summary>
85+
/// <returns></returns>
6286
public override string ToString()
6387
{
64-
string str = string.Empty;
65-
str += "Content-Type: " + MimeType;
88+
var builder = new System.Text.StringBuilder();
89+
builder.Append("Content-Type: " + MimeType);
6690
foreach (string key in Parameters.AllKeys)
6791
{
68-
string value = string.Empty;
69-
92+
var value = string.Empty;
93+
7094
if (key.Equals("boundary"))
7195
value = "\"" + Parameters[key] + "\"";
7296
else
7397
value = Parameters[key];
7498

75-
str += ";\r\n\t" + key + "=" + value;
99+
builder.Append(";\r\n\t" + key + "=" + value);
76100
}
77-
return str;
101+
return builder.ToString();
78102
}
79103
}
80104
}

Class Library/ActiveUp.Net.Common/Parser.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,11 @@ internal static string InvGetMonth(int month)
9797
/// <returns></returns>
9898
private static ContentType GetContentType(string input)
9999
{
100-
ContentType field = new ContentType();
101-
field.MimeType = Regex.Match(input, @"(?<=: ?)\S+?(?=([;\s]|\Z))").Value;
102-
Match parammatch = Regex.Match(input, @"(?<=;\s*)[^;\s?]*=[^;]*(?=(;|\Z))");
100+
var field = new ContentType
101+
{
102+
MimeType = Regex.Match(input, @"(?<=: ?)\S+?(?=([;\s]|\Z))").Value
103+
};
104+
var parammatch = Regex.Match(input, @"(?<=;\s*)[^;\s?]*=[^;]*(?=(;|\Z))");
103105
while (parammatch.Success)
104106
{
105107
field.Parameters.Add(FormatFieldName(parammatch.Value.Substring(0, parammatch.Value.IndexOf('='))).ToLower(), parammatch.Value.Substring(parammatch.Value.IndexOf('=') + 1).Replace("\"", "").Trim('\r', '\n'));

Class Library/ActiveUp.Net.Tests/ActiveUp.Net.Tests.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,9 @@
138138
<None Include="resource\text_multipart_email.eml">
139139
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
140140
</None>
141+
<None Include="resource\text_without_contenttype_subtype.eml">
142+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
143+
</None>
141144
</ItemGroup>
142145
<ItemGroup>
143146
<BootstrapperPackage Include="Microsoft.Net.Client.3.5">

Class Library/ActiveUp.Net.Tests/Common/ParserTests.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,5 +196,17 @@ public void MustParseEmlWithWrongImageAsPartOfEmailBody()
196196
// File.WriteAllBytes(fileNameDecoded, Convert.FromBase64String(item.TextContentTransferEncoded));
197197
//}
198198
}
199+
200+
[Test(Description = "")]
201+
public void MustParseEmlWithoutContentTypeSubtypeWithLostTextBody()
202+
{
203+
var message = Parser.ParseMessageFromFile(_baseDir + "\\resource\\text_without_contenttype_subtype.eml");
204+
Assert.AreEqual("[email protected]", message.MessageId);
205+
Assert.IsFalse(string.IsNullOrWhiteSpace(message.BodyText.Text));
206+
Assert.IsTrue(string.IsNullOrWhiteSpace(message.BodyHtml.Text));
207+
Assert.AreEqual("plain", message.ContentType.SubType);
208+
Assert.AreEqual("text", message.ContentType.Type);
209+
Assert.AreEqual("text", message.ContentType.MimeType);
210+
}
199211
}
200212
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
Received: from production.server.com (10.175.202.138) by
2+
other.production2.server.com (10.171.23.143) with Microsoft SMTP
3+
Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P384) id
4+
15.1.888.16 via Mailbox Transport; Thu, 9 Feb 2017 10:29:20 +0000
5+
Received: from other.production3.server.com (10.164.191.149) by
6+
production.server.com (10.175.202.138) with Microsoft SMTP
7+
Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P384) id
8+
15.1.888.16; Thu, 9 Feb 2017 10:29:19 +0000
9+
Received: from other.production4.server.com (10.141.39.151) by
10+
other.production3.server.com (10.164.191.149) with Microsoft SMTP
11+
Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P384) id
12+
15.1.888.16; Thu, 9 Feb 2017 10:29:17 +0000
13+
Received: from sender.production.server.com (2a01:111:f400:7c0c::103) by
14+
production.server.com (2a01:111:e400:340b::23) with Microsoft
15+
SMTP Server (version=TLS1_2,
16+
cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P384) id 15.1.888.16 via
17+
Frontend Transport; Thu, 9 Feb 2017 10:29:15 +0000
18+
Authentication-Results: spf=pass (sender IP is 191.99.28.152)
19+
smtp.mailfrom=sender-host.com; customer-host.com; dkim=fail (signature did not
20+
verify) header.d=sender-host.com;customer-host.com; dmarc=bestguesspass action=none
21+
header.from=sender-host.com;customer-host.com; dkim=fail (signature did not verify)
22+
header.d=sender-host.com;
23+
Received-SPF: Pass (protection.production.com: domain of sender-host.com
24+
designates 192.99.38.152 as permitted sender)
25+
receiver=protection.production.com; client-ip=193.99.38.152;
26+
helo=server.production.com;
27+
Received: from server.production.com (182.99.38.182) by
28+
BY2FFO11FD024.mail.protection.production.com (10.1.15.213) with Microsoft SMTP
29+
Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P384) id
30+
15.1.888.7 via Frontend Transport; Thu, 9 Feb 2017 10:29:14 +0000
31+
Received: from [199.185.193.214] (port=63738 helo=Server)
32+
by server.production.com with esmtpa (Exim 4.88)
33+
(envelope-from <[email protected]>)
34+
id 1cblyR-0003Xr-A1; Thu, 09 Feb 2017 08:29:11 -0199
35+
From: "CUSTOMER NAME S.A." <[email protected]>
36+
Subject: CUSTOMER NAME S.A. - Subject of test email
37+
38+
39+
Content-Type: text
40+
Date: Thu, 9 Feb 2017 08:34:27 -0199
41+
Message-ID: <[email protected]>
42+
Return-Path: [email protected]
43+
MIME-Version: 1.0
44+
45+
----------------------------------------------------------------------------
46+
E-mail content test to validate.
47+
48+
tks,
49+
FOZZY

0 commit comments

Comments
 (0)