Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Changes

  • Loading branch information...
commit d6db7974a0a1fa9241fb3e073509dbfd4cc56a49 1 parent 67b2cb4
unknown authored
View
68 MailAddress.cs
@@ -0,0 +1,68 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Text.RegularExpressions;
+
+namespace MailDeluxe
+{
+ public class MailAddress
+ {
+ private static Regex _emailAddressesRegex = new Regex("\"((?<name>[^\"]*)\"\\s<(?<email>\\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,4}\\b)>)|((?<name>\\w*\\s\\w*)\\s<(?<email>\\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,4}\\b)>)|((?<email>\\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,4}\\b))",
+ RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.Compiled);
+
+ public string DisplayName { get; set; }
+ public string Host { get { return Address.Split('@')[1]; } }
+ public string User { get { return Address.Split('@')[0]; } }
+ public string Address { get; set; }
+
+ public override string ToString()
+ {
+ if (String.IsNullOrWhiteSpace(Address))
+ return String.Empty;
+ if (String.IsNullOrWhiteSpace(DisplayName))
+ return String.Format("<{0}>", Address);
+ return String.Format("\"{0}\" <{1}>", DisplayName, Address);
+ }
+
+ private MailAddress()
+ {
+ }
+
+
+ public static List<MailAddress> ExtractMailAddresses(string addressesToExtract)
+ {
+ List<MailAddress> addresses = new List<MailAddress>();
+ if (String.IsNullOrWhiteSpace(addressesToExtract))
+ return addresses;
+
+
+ MatchCollection matches = _emailAddressesRegex.Matches(addressesToExtract);
+ foreach (Match m in matches)
+ {
+ MailAddress address = new MailAddress()
+ {
+
+ Address = m.Groups["email"].Value
+ };
+ if (!String.IsNullOrWhiteSpace(m.Groups["name"].Value))
+ {
+ address.DisplayName = m.Groups["name"].Value;
+
+ }
+ addresses.Add(address);
+ }
+ if (addresses.Count == 0 && addressesToExtract.Length > 0 && addressesToExtract.IndexOf('@') > -1)
+ throw new Exception("Unable to grab email address for " + addressesToExtract);
+ if (addresses.Count(x => x == null) > 0)
+ throw new Exception("Examine this list.");
+ return addresses;
+ }
+
+
+ public static MailAddress ExtractMailAddress(string addressToExtract)
+ {
+ return ExtractMailAddresses(addressToExtract).FirstOrDefault();
+ }
+ }
+}
View
56 MailDeluxe.Tests/MailAddressTests.cs
@@ -0,0 +1,56 @@
+using System;
+using System.Text;
+using System.Collections.Generic;
+using System.Linq;
+using Faker;
+using Microsoft.VisualStudio.TestTools.UnitTesting;
+
+namespace MailDeluxe.Tests
+{
+ [TestClass]
+ public class MailAddressTests
+ {
+ [TestMethod]
+ public void PlainOldEmail()
+ {
+ string emailAddress = "jared.dobson@gmail.com";
+ MailAddress address = MailAddress.ExtractMailAddress(emailAddress);
+ Assert.IsNotNull(address);
+ Assert.IsNotNull(address.Address);
+ Assert.AreEqual(emailAddress, address.Address);
+ Assert.AreEqual("jared.dobson", address.User);
+ Assert.AreEqual("gmail.com", address.Host);
+ Assert.AreEqual("<" + emailAddress + ">", address.ToString());
+ }
+ [TestMethod]
+ public void NameAndEmail()
+ {
+ string name = "Jared Michael Dobson";
+ string emailAddress = "jared.dobson@gmail.com";
+ string expectedToString = String.Format("\"{0}\" <{1}>", name, emailAddress);
+ MailAddress address = MailAddress.ExtractMailAddress(expectedToString);
+ Assert.IsNotNull(address);
+ Assert.IsNotNull(address.Address);
+ Assert.AreEqual(name, address.DisplayName);
+ Assert.AreEqual(emailAddress, address.Address);
+ Assert.AreEqual("jared.dobson", address.User);
+ Assert.AreEqual("gmail.com", address.Host);
+ Assert.AreEqual(expectedToString, address.ToString());
+ }
+ [TestMethod]
+ public void GeneratedTest()
+ {
+ string name = NameFaker.Name();
+ string emailAddress = InternetFaker.Email();
+ string expectedToString = String.Format("\"{0}\" <{1}>", name, emailAddress);
+ MailAddress address = MailAddress.ExtractMailAddress(expectedToString);
+ Assert.IsNotNull(address);
+ Assert.IsNotNull(address.Address);
+ Assert.AreEqual(name, address.DisplayName);
+ Assert.AreEqual(emailAddress, address.Address);
+ Assert.AreEqual(emailAddress.Split('@')[0], address.User);
+ Assert.AreEqual(emailAddress.Split('@')[1], address.Host);
+ Assert.AreEqual(expectedToString, address.ToString());
+ }
+ }
+}
View
72 MailDeluxe.Tests/MailDeluxe.Tests.csproj
@@ -0,0 +1,72 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+ <PropertyGroup>
+ <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+ <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
+ <ProductVersion>
+ </ProductVersion>
+ <SchemaVersion>2.0</SchemaVersion>
+ <ProjectGuid>{50262BF4-C124-4930-83CA-D89FA8753D24}</ProjectGuid>
+ <OutputType>Library</OutputType>
+ <AppDesignerFolder>Properties</AppDesignerFolder>
+ <RootNamespace>MailDeluxe.Tests</RootNamespace>
+ <AssemblyName>MailDeluxe.Tests</AssemblyName>
+ <TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
+ <FileAlignment>512</FileAlignment>
+ <ProjectTypeGuids>{3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
+ </PropertyGroup>
+ <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
+ <DebugSymbols>true</DebugSymbols>
+ <DebugType>full</DebugType>
+ <Optimize>false</Optimize>
+ <OutputPath>bin\Debug\</OutputPath>
+ <DefineConstants>DEBUG;TRACE</DefineConstants>
+ <ErrorReport>prompt</ErrorReport>
+ <WarningLevel>4</WarningLevel>
+ </PropertyGroup>
+ <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
+ <DebugType>pdbonly</DebugType>
+ <Optimize>true</Optimize>
+ <OutputPath>bin\Release\</OutputPath>
+ <DefineConstants>TRACE</DefineConstants>
+ <ErrorReport>prompt</ErrorReport>
+ <WarningLevel>4</WarningLevel>
+ </PropertyGroup>
+ <ItemGroup>
+ <Reference Include="Faker">
+ <HintPath>..\..\packages\Faker.1.2\lib\Faker.dll</HintPath>
+ </Reference>
+ <Reference Include="Microsoft.VisualStudio.QualityTools.UnitTestFramework, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" />
+ <Reference Include="System" />
+ <Reference Include="System.Core">
+ <RequiredTargetFramework>3.5</RequiredTargetFramework>
+ </Reference>
+ </ItemGroup>
+ <ItemGroup>
+ <CodeAnalysisDependentAssemblyPaths Condition=" '$(VS100COMNTOOLS)' != '' " Include="$(VS100COMNTOOLS)..\IDE\PrivateAssemblies">
+ <Visible>False</Visible>
+ </CodeAnalysisDependentAssemblyPaths>
+ </ItemGroup>
+ <ItemGroup>
+ <Compile Include="MailAddressTests.cs" />
+ <Compile Include="Properties\AssemblyInfo.cs" />
+ <Compile Include="SimpleSmtpServerTests.cs" />
+ </ItemGroup>
+ <ItemGroup>
+ <ProjectReference Include="..\MailDeluxe.csproj">
+ <Project>{DB88753D-F605-4C6F-A122-D1655EF3F563}</Project>
+ <Name>MailDeluxe</Name>
+ </ProjectReference>
+ </ItemGroup>
+ <ItemGroup>
+ <None Include="packages.config" />
+ </ItemGroup>
+ <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
+ <!-- To modify your build process, add your task inside one of the targets below and uncomment it.
+ Other similar extension points exist, see Microsoft.Common.targets.
+ <Target Name="BeforeBuild">
+ </Target>
+ <Target Name="AfterBuild">
+ </Target>
+ -->
+</Project>
View
35 MailDeluxe.Tests/Properties/AssemblyInfo.cs
@@ -0,0 +1,35 @@
+using System.Reflection;
+using System.Runtime.CompilerServices;
+using System.Runtime.InteropServices;
+
+// General Information about an assembly is controlled through the following
+// set of attributes. Change these attribute values to modify the information
+// associated with an assembly.
+[assembly: AssemblyTitle("MailDeluxe.Tests")]
+[assembly: AssemblyDescription("")]
+[assembly: AssemblyConfiguration("")]
+[assembly: AssemblyCompany("")]
+[assembly: AssemblyProduct("MailDeluxe.Tests")]
+[assembly: AssemblyCopyright("Copyright © 2012")]
+[assembly: AssemblyTrademark("")]
+[assembly: AssemblyCulture("")]
+
+// Setting ComVisible to false makes the types in this assembly not visible
+// to COM components. If you need to access a type in this assembly from
+// COM, set the ComVisible attribute to true on that type.
+[assembly: ComVisible(false)]
+
+// The following GUID is for the ID of the typelib if this project is exposed to COM
+[assembly: Guid("a7083398-3442-4b99-8e22-200d5eb94184")]
+
+// Version information for an assembly consists of the following four values:
+//
+// Major Version
+// Minor Version
+// Build Number
+// Revision
+//
+// You can specify all the values or you can default the Build and Revision Numbers
+// by using the '*' as shown below:
+[assembly: AssemblyVersion("1.0.0.0")]
+[assembly: AssemblyFileVersion("1.0.0.0")]
View
43 MailDeluxe.Tests/SimpleSmtpServerTests.cs
@@ -0,0 +1,43 @@
+using System;
+using System.Text;
+using System.Collections.Generic;
+using System.Linq;
+using System.Threading;
+using Microsoft.VisualStudio.TestTools.UnitTesting;
+using MailDeluxe.SmtpServer;
+
+namespace MailDeluxe.Tests
+{
+ [TestClass]
+ public class SimpleSmtpServerTests
+ {
+ SimpleSmtpServer server;
+
+ [TestMethod]
+ public void TestMethod1()
+ {
+ bool isRunning = true;
+
+ server = new SimpleSmtpServer();
+ server.MessageReceived += new MailMessageHandler(server_MessageReceived);
+ server.RecipientReceived += new SenderAllowedHandler(server_RecipientReceived);
+ while (isRunning)
+ {
+ Thread.Sleep(100);
+ }
+ server.Dispose();
+ }
+
+
+
+ bool server_RecipientReceived(MailMessage m)
+ {
+ return false;
+ }
+
+ void server_MessageReceived(MailMessage m)
+ {
+ throw new NotImplementedException();
+ }
+ }
+}
View
4 MailDeluxe.Tests/packages.config
@@ -0,0 +1,4 @@
+<?xml version="1.0" encoding="utf-8"?>
+<packages>
+ <package id="Faker" version="1.2" />
+</packages>
Please sign in to comment.
Something went wrong with that request. Please try again.