private static void ValidateFileAttachment(EmailMessageAttachment attachment) { // throw when null if (attachment == null) { throw new ArgumentNullException("attachment"); } // throw when null if (attachment.Bytes == null) { throw new ArgumentNullException("attachment", "{Bytes} cannot be null."); } // throw when null/empty if (string.IsNullOrWhiteSpace(attachment.FileName)) { throw new ArgumentException("{FileName} cannot be null or empty.", "attachment"); } // throw when null/empty if (string.IsNullOrWhiteSpace(attachment.MimeType)) { throw new ArgumentException("{MimeType} cannot be null or empty.", "attachment"); } }
public async Task ShouldSendEmailWithAttachmentUsingSmtp() { var provider = new SmtpClientProvider(_settings.Object); var message = new EmailMessageWithAttachment("*****@*****.**", "*****@*****.**") { Subject = "Unit test" }; message.AddBody(EmailMessageBody.FromText("Hello world")); var textStream = new MemoryStream(); using (var wtr = new StreamWriter(textStream)) { wtr.WriteLine("Hello world attachment"); wtr.Flush(); message.AddAttachment(EmailMessageAttachment.Create(textStream, Encoding.UTF8, "test.txt", "plain/text")); await provider.SendAsync(message); } _server.ReceivedEmailCount.Should().Be(1); var receivedMessage = _server.ReceivedEmail.First(); receivedMessage.Subject.Should().Be("Unit test"); receivedMessage.Body.Should().Be("Hello world"); receivedMessage.IsBodyHtml.Should().BeFalse(); receivedMessage.Attachments.Count.Should().Be(1); receivedMessage.Attachments.First().Name.Should().Be("test.txt"); receivedMessage.Attachments.First().ContentType.MediaType.Should().Be("plain/text"); }