microsoft/TypeAgent

Public

mirrored fromhttps://github.com/microsoft/TypeAgentAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
copilot/fix-github-actions-workflow-issue

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

dotnet/email/EmailAddress.cs

89lines · modecode

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4using System.Net.Mail;
5
6namespace TypeAgent;
7
8public class EmailAddress
9{
10 public EmailAddress()
11 {
12 }
13
14 public EmailAddress(string address, string displayName = null)
15 {
16 Address = address;
17 DisplayName = displayName;
18 }
19
20 [JsonPropertyName("address")]
21 public string Address { get; set; }
22 [JsonPropertyName("displayName")]
23 public string DisplayName { get; set; }
24
25 public override string ToString()
26 {
27 if (string.IsNullOrEmpty(DisplayName))
28 {
29 return Address ?? string.Empty;
30 }
31 else
32 {
33 return string.IsNullOrEmpty(Address) ? DisplayName : $"\"{DisplayName}\" <{Address}>";
34 }
35 }
36
37 public static EmailAddress FromString(string address)
38 {
39 EmailAddress emailAddress = new EmailAddress();
40 if (address.Length > 0)
41 {
42 emailAddress.DisplayName = address;
43 try
44 {
45 MailAddress mailAddress = new MailAddress(address);
46 emailAddress.Address = mailAddress.Address;
47 emailAddress.DisplayName = mailAddress.DisplayName;
48 }
49 catch
50 {
51 }
52 }
53 return emailAddress;
54 }
55
56 public static List<EmailAddress> ListFromString(string addresses)
57 {
58 // Outlook interop has a text export bug
59 List<EmailAddress> emailAddresses = new List<EmailAddress>();
60 if (addresses.Length > 0)
61 {
62 var addressParts = addresses.Split(";", StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries);
63 foreach (var part in addressParts)
64 {
65 emailAddresses.Add(FromString(part));
66 }
67
68 }
69 return emailAddresses;
70 }
71}
72
73public static class EmailAddressEx
74{
75 public static string Join(this List<EmailAddress> list)
76 {
77 StringBuilder sb = new StringBuilder();
78 for (int i = 0; i < list.Count; ++i)
79 {
80 var item = list[i];
81 if (i > 0)
82 {
83 sb.Append(", ");
84 }
85 sb.Append(item.ToString());
86 }
87 return sb.ToString();
88 }
89}
90