-
Notifications
You must be signed in to change notification settings - Fork 153
/
AlternateViewCollection.cs
35 lines (31 loc) · 1.15 KB
/
AlternateViewCollection.cs
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
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
namespace AE.Net.Mail {
public class AlternateViewCollection : Collection<Attachment> {
/// <summary>
/// Find views matching a specific content-type.
/// </summary>
/// <param name="contentType">The content-type to search for; such as "text/html"</param>
/// <returns></returns>
public IEnumerable<Attachment> OfType(string contentType) {
contentType = (contentType ?? string.Empty).ToLower();
return OfType(x => x.Is(contentType));
}
/// <summary>
/// Find views where the content-type matches a condition
/// </summary>
/// <param name="predicate"></param>
/// <returns></returns>
public IEnumerable<Attachment> OfType(Func<string, bool> predicate) {
return this.Where(x => predicate((x.ContentType ?? string.Empty).Trim()));
}
public Attachment GetHtmlView() {
return OfType("text/html").FirstOrDefault() ?? OfType(ct => ct.Contains("html")).FirstOrDefault();
}
public Attachment GetTextView() {
return OfType("text/plain").FirstOrDefault() ?? OfType(ct => ct.StartsWith("text/")).FirstOrDefault();
}
}
}