-
Notifications
You must be signed in to change notification settings - Fork 445
/
Copy pathExchangeSender.cs
95 lines (76 loc) · 2.97 KB
/
ExchangeSender.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
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
using FluentEmail.Core;
using FluentEmail.Core.Interfaces;
using FluentEmail.Core.Models;
using Microsoft.Exchange.WebServices.Data;
using System.Threading;
using System.Threading.Tasks;
namespace FluentEmail.Exchange
{
public class ExchangeSender : ISender
{
private readonly ExchangeService meExchangeClient;
public ExchangeSender(ExchangeService paExchangeClient)
{
meExchangeClient = paExchangeClient;
}
public SendResponse Send(IFluentEmail email, CancellationToken? token = null)
{
return System.Threading.Tasks.Task.Run(() => SendAsync(email, token)).Result;
}
public async Task<SendResponse> SendAsync(IFluentEmail email, CancellationToken? token = null)
{
var response = new SendResponse();
var message = CreateMailMessage(email);
if (token?.IsCancellationRequested ?? false)
{
response.ErrorMessages.Add("Message was cancelled by cancellation token.");
return response;
}
message.Save();
message.SendAndSaveCopy();
return response;
}
private EmailMessage CreateMailMessage(IFluentEmail paEmail)
{
var paData = paEmail.Data;
EmailMessage loExchangeMessage = new EmailMessage(meExchangeClient)
{
Subject = paData.Subject,
Body = paData.Body,
};
if (!string.IsNullOrEmpty(paData.FromAddress?.EmailAddress))
loExchangeMessage.From = new EmailAddress(paData.FromAddress.Name, paData.FromAddress.EmailAddress);
paData.ToAddresses.ForEach(x =>
{
loExchangeMessage.ToRecipients.Add(new EmailAddress(x.Name, x.EmailAddress));
});
paData.CcAddresses.ForEach(x =>
{
loExchangeMessage.CcRecipients.Add(new EmailAddress(x.Name, x.EmailAddress));
});
paData.BccAddresses.ForEach(x =>
{
loExchangeMessage.BccRecipients.Add(new EmailAddress(x.EmailAddress, x.Name));
});
switch (paData.Priority)
{
case Priority.Low:
loExchangeMessage.Importance = Importance.Low;
break;
case Priority.Normal:
loExchangeMessage.Importance = Importance.Normal;
break;
case Priority.High:
loExchangeMessage.Importance = Importance.High;
break;
}
paData.Attachments.ForEach(x =>
{
// System.Net.Mail.Attachment a = new System.Net.Mail.Attachment(x.Data, x.Filename, x.ContentType);
// a.ContentId = x.ContentId;
loExchangeMessage.Attachments.AddFileAttachment(x.Filename, x.Data);
});
return loExchangeMessage;
}
}
}