-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdditionalEmailAddrs.cs
More file actions
175 lines (160 loc) · 7.36 KB
/
Copy pathAdditionalEmailAddrs.cs
File metadata and controls
175 lines (160 loc) · 7.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// Copyright © 2020-2022 ASM-SW
//asmeyers@outlook.com https://github.com/asm-sw
using FileHelpers;
using Microsoft.VisualBasic.FileIO;
using System;
using System.Collections.Generic;
using System.Text;
//using System.Windows;
namespace EmailWithAttachedFile
{
/// <summary>
/// This Class defines the columns in the IO file. FileHelpers uses it to read and write the CSV file.
/// </summary>
[DelimitedRecord(","), IgnoreFirst]
class CustomerRecord
{
// NameLastFirst, Name,FileName, Email
[FieldQuoted('"', QuoteMode.OptionalForBoth, MultilineMode.NotAllow), FieldTrimAttribute(TrimMode.Both)]
public string NameLastFirst { get; set; }
[FieldQuoted('"', QuoteMode.OptionalForBoth, MultilineMode.NotAllow), FieldTrimAttribute(TrimMode.Both)]
public string Name { get; set; }
[FieldQuoted('"', QuoteMode.OptionalForBoth, MultilineMode.NotAllow), FieldTrimAttribute(TrimMode.Both)]
public string FileName { get; set; }
[FieldQuoted('"', QuoteMode.OptionalForBoth, MultilineMode.NotAllow), FieldTrimAttribute(TrimMode.Both)]
public string Email { get; set; }
}
/// <summary>
/// This is the class that does all of the work of reading in files, parsing and modifying the IO file.
/// The IO file email column will be modified to hold a semicolon seprated list of email addresses.
/// These addtional addresses are from the addition eamil addres file.
/// The email address found in the IO file is looked up in the main email column of the addtional email address file.
/// </summary>
class AdditionalEmailAddrs
{
readonly Dictionary<string, EmailInfo> m_additionEmails = new Dictionary<string, EmailInfo>();
/// <summary>
/// This class is used to hold one line in the addition emails input file.
/// </summary>
class EmailInfo
{
public string Name { get; set; }
public string EmailAddrs { get; set; }
public EmailInfo()
{
Name = string.Empty;
EmailAddrs = string.Empty;
}
}
/// <summary>
/// Reads in the addtional email file
/// </summary>
/// <param name="errMessage"></param>
/// <param name="filenameAdditionEmaillAddr">filename to parse for email addresses</param>
/// <returns>false on error with message in errMessage</returns>
public bool ReadAdditionalEmailAddrFile(string filenameAdditionEmaillAddr, ref StringBuilder errMessage)
{
bool res = true;
try
{
using (TextFieldParser csvReader = new TextFieldParser(filenameAdditionEmaillAddr))
{
csvReader.SetDelimiters(new string[] { "," });
csvReader.HasFieldsEnclosedInQuotes = true;
string[] colFields = csvReader.ReadFields();
int nameIdx = -1; // Customer name
int mainEmailIdx = -1; // this is the main email address and is used as the key in the dictionaray
res = FindColumn(errMessage, colFields, "Customer", ref nameIdx);
res &= FindColumn(errMessage, colFields, "Main Email", ref mainEmailIdx);
// find the columns in the data that contain additional email address
List<int> emailIdxs = new List<int>();
for (int idx = 0; idx < colFields.Length; idx++)
{
if (idx == mainEmailIdx)
continue;
if (colFields[idx].Contains("Email"))
emailIdxs.Add(idx);
}
if (emailIdxs.Count < 1)
{
res = false;
errMessage.AppendFormat("No email addresses found in {0}\n", filenameAdditionEmaillAddr);
}
if (!res)
return res;
while (!csvReader.EndOfData)
{
EmailInfo info = new EmailInfo();
string[] fieldData = csvReader.ReadFields();
if (string.IsNullOrWhiteSpace(fieldData[mainEmailIdx]))
continue;
StringBuilder additionalAddrs = new StringBuilder(128);
additionalAddrs.Append(fieldData[mainEmailIdx]);
bool bFoundAdditionalEmails = false;
foreach (int index in emailIdxs)
{
if (!string.IsNullOrWhiteSpace(fieldData[index]))
{
bFoundAdditionalEmails = true;
additionalAddrs.AppendFormat(";{0}", fieldData[index]);
}
}
if (bFoundAdditionalEmails)
{
info.Name = fieldData[nameIdx];
info.EmailAddrs = additionalAddrs.ToString();
try
{
m_additionEmails.Add(fieldData[mainEmailIdx], info);
}
catch (ArgumentException)
{
errMessage.AppendFormat("Duplicate email found for: {0} - {1}\n", info.Name, info.EmailAddrs);
}
}
}
}
}
catch (Exception ex)
{
errMessage.AppendLine(ex.ToString());
return false;
}
return res;
}
/// <summary>
/// Finds the column colName in colFields array.
/// </summary>
/// <param name="errMessage"></param>
/// <param name="colFields">array to find colName in</param>
/// <param name="colName">column to find</param>
/// <param name="index">set to index of found column.</param>
/// <returns>true if colName is found</returns>
private bool FindColumn(StringBuilder errMessage, string[] colFields, string colName, ref int index)
{
index = Array.IndexOf(colFields, colName);
if (index < 0)
{
errMessage.AppendFormat("Cannot find required column {0} in additional email addresses file\n", colName);
return false;
}
return true;
}
/// <summary>
/// gets a list of email addresses
/// </summary>
/// <param name="mainEmailAddr">the main email address</param>
/// <param name="additionalEmailAddresses">semicolon separated email addresses including the main email address</param>
/// <returns>true if additonal email addresses are found, false if not</returns>
public bool GetAddtionalEmailAddresses(string mainEmailAddr, out string additionalEmailAddresses)
{
if (m_additionEmails.TryGetValue(mainEmailAddr, out EmailInfo emailInfo))
{
additionalEmailAddresses = emailInfo.EmailAddrs;
return true;
}
additionalEmailAddresses = string.Empty;
return false;
}
}
}