-
Notifications
You must be signed in to change notification settings - Fork 444
Expand file tree
/
Copy pathMailKitSmtpSenderTests.cs
More file actions
198 lines (166 loc) · 6.33 KB
/
Copy pathMailKitSmtpSenderTests.cs
File metadata and controls
198 lines (166 loc) · 6.33 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
using System.IO;
using System.Threading.Tasks;
using FluentEmail.Core;
using FluentEmail.MailKitSmtp;
using NUnit.Framework;
using Attachment = FluentEmail.Core.Models.Attachment;
namespace FluentEmail.MailKit.Tests
{
[NonParallelizable]
public class MailKitSmtpSenderTests
{
// Warning: To pass, an smtp listener must be running on localhost:25.
const string toEmail = "bob@test.com";
const string fromEmail = "johno@test.com";
const string subject = "sup dawg";
const string body = "what be the hipitity hap?";
private readonly string tempDirectory;
public MailKitSmtpSenderTests()
{
tempDirectory = Path.Combine(Path.GetTempPath(), "EmailTest");
}
[SetUp]
public void SetUp()
{
var sender = new MailKitSender(new SmtpClientOptions
{
Server = "localhost",
Port = 25,
UseSsl = false,
RequiresAuthentication = false,
UsePickupDirectory = true,
MailPickupDirectory = Path.Combine(Path.GetTempPath(), "EmailTest")
});
Email.DefaultSender = sender;
Directory.CreateDirectory(tempDirectory);
}
[TearDown]
public void TearDown()
{
Directory.Delete(tempDirectory, true);
}
[Test]
public void CanSendEmail()
{
var email = Email
.From(fromEmail)
.To(toEmail)
.Body("<h2>Test</h2>", true);
var response = email.Send();
var files = Directory.EnumerateFiles(tempDirectory, "*.eml");
Assert.IsTrue(response.Successful);
Assert.IsNotEmpty(files);
}
[Test]
public async Task CanSendEmailWithAttachments()
{
var stream = new MemoryStream();
var sw = new StreamWriter(stream);
sw.WriteLine("Hey this is some text in an attachment");
sw.Flush();
stream.Seek(0, SeekOrigin.Begin);
var attachment = new Attachment
{
Data = stream,
ContentType = "text/plain",
Filename = "MailKitAttachment.txt"
};
var email = Email
.From(fromEmail)
.To(toEmail)
.Subject(subject)
.Body(body)
.Attach(attachment);
var response = await email.SendAsync();
var files = Directory.EnumerateFiles(tempDirectory, "*.eml");
Assert.IsTrue(response.Successful);
Assert.IsNotEmpty(files);
}
[Test]
[TestCase]
[TestCase("logotest.png")]
public async Task CanSendEmailWithInlineImages(string contentId = null)
{
using (var stream = File.OpenRead($"{Path.Combine(Directory.GetCurrentDirectory(), "logotest.png")}"))
{
var attachment = new Attachment
{
IsInline = true,
Data = stream,
ContentType = "image/png",
Filename = "logotest.png",
ContentId = contentId
};
var email = Email
.From(fromEmail)
.To(toEmail)
.Subject(subject)
.Body("<html>Inline image here: <img src=\"cid:logotest.png\">" +
"<p>You should see an image without an attachment, or without a download prompt, depending on the email client.</p></html>", true)
.Attach(attachment);
var response = await email.SendAsync();
var files = Directory.EnumerateFiles(tempDirectory, "*.eml");
Assert.IsTrue(response.Successful);
Assert.IsNotEmpty(files);
}
}
[Test]
public async Task CanSendEmailWithInlineImagesAndAttachmentTogether()
{
var attachmentStream = new MemoryStream();
var sw = new StreamWriter(attachmentStream);
sw.WriteLine("Hey this is some text in an attachment");
sw.Flush();
attachmentStream.Seek(0, SeekOrigin.Begin);
var attachment = new Attachment
{
Data = attachmentStream,
ContentType = "text/plain",
Filename = "MailKitAttachment.txt",
};
using var inlineStream = File.OpenRead($"{Path.Combine(Directory.GetCurrentDirectory(), "logotest.png")}");
var attachmentInline = new Attachment
{
IsInline = true,
Data = inlineStream,
ContentType = "image/png",
Filename = "logotest.png",
};
var email = Email
.From(fromEmail)
.To(toEmail)
.Subject(subject)
.Body("<html>Inline image here: <img src=\"cid:logotest.png\">" +
"<p>You should see an image inline without a picture attachment.</p>" +
"<p>A single .txt file should also be attached.</p></html>", true)
.Attach(attachment)
.Attach(attachmentInline);
var response = await email.SendAsync();
var files = Directory.EnumerateFiles(tempDirectory, "*.eml");
Assert.IsTrue(response.Successful);
Assert.IsNotEmpty(files);
}
[Test]
public async Task CanSendAsyncHtmlAndPlaintextTogether()
{
var email = Email
.From(fromEmail)
.To(toEmail)
.Body("<h2>Test</h2><p>some body text</p>", true)
.PlaintextAlternativeBody("Test - Some body text");
var response = await email.SendAsync();
Assert.IsTrue(response.Successful);
}
[Test]
public void CanSendHtmlAndPlaintextTogether()
{
var email = Email
.From(fromEmail)
.To(toEmail)
.Body("<h2>Test</h2><p>some body text</p>", true)
.PlaintextAlternativeBody("Test - Some body text");
var response = email.Send();
Assert.IsTrue(response.Successful);
}
}
}