private static async Task <DbModel.Message> GetTestMessage1() { MimeMessage mimeMessage = new MimeMessage(); mimeMessage.From.Add(InternetAddress.Parse("*****@*****.**")); mimeMessage.To.Add(InternetAddress.Parse("*****@*****.**")); mimeMessage.Cc.Add(InternetAddress.Parse("*****@*****.**")); mimeMessage.Subject = "subject"; BodyBuilder bodyBuilder = new BodyBuilder(); bodyBuilder.HtmlBody = "<html>Hi</html>"; bodyBuilder.TextBody = "Hi"; bodyBuilder.Attachments.Add("file1", new MemoryStream(System.Text.Encoding.UTF8.GetBytes(testMessage1File1Content)), new ContentType("text", "plain")).ContentId = "file1"; bodyBuilder.Attachments.Add("file2", new MemoryStream(System.Text.Encoding.UTF8.GetBytes(testMessage1File2Content)), new ContentType("text", "plain")); mimeMessage.Body = bodyBuilder.ToMessageBody(); MemoryMessageBuilder memoryMessageBuilder = new MemoryMessageBuilder(); memoryMessageBuilder.Recipients.Add("*****@*****.**"); memoryMessageBuilder.From = "*****@*****.**"; using (var messageData = await memoryMessageBuilder.WriteData()) { messageData.Write(Encoding.UTF8.GetBytes(mimeMessage.ToString())); } IMessage message = await memoryMessageBuilder.ToMessage(); var dbMessage = await new MessageConverter().ConvertAsync(message); return(dbMessage); }
/// <summary> /// /// </summary> /// <param name="address">The address<see cref="string"/></param> /// <param name="expectedAddress">The expectedAddress<see cref="string"/></param> /// <returns>A <see cref="Task{T}"/> representing the async operation</returns> private async Task TestGoodAddressAsync(string address, string expectedAddress, bool eightBit = false) { TestMocks mocks = new TestMocks(); MemoryMessageBuilder messageBuilder = new MemoryMessageBuilder(); messageBuilder.EightBitTransport = eightBit; mocks.Connection.SetupGet(c => c.CurrentMessage).Returns(messageBuilder); RcptToVerb verb = new RcptToVerb(); await verb.Process(mocks.Connection.Object, new SmtpCommand("TO " + address)).ConfigureAwait(false); mocks.VerifyWriteResponseAsync(StandardSmtpResponseCode.OK); Assert.Equal(expectedAddress, messageBuilder.Recipients.First()); }
public async Task Data_ExactlySizeLimit_Accepted() { TestMocks mocks = new TestMocks(); MemoryMessageBuilder messageBuilder = new MemoryMessageBuilder(); mocks.Connection.SetupGet(c => c.CurrentMessage).Returns(messageBuilder); mocks.ServerBehaviour.Setup(b => b.GetMaximumMessageSize(It.IsAny <IConnection>())).ReturnsAsync(10); string[] messageData = new string[] { new string('x', 10), "." }; int messageLine = 0; mocks.Connection.Setup(c => c.ReadLine()).Returns(() => Task.FromResult(messageData[messageLine++])); DataVerb verb = new DataVerb(); await verb.Process(mocks.Connection.Object, new SmtpCommand("DATA")).ConfigureAwait(false); mocks.VerifyWriteResponseAsync(StandardSmtpResponseCode.StartMailInputEndWithDot); mocks.VerifyWriteResponseAsync(StandardSmtpResponseCode.OK); }
/// <summary> /// /// </summary> /// <param name="address">The address<see cref="string"/></param> /// <returns>A <see cref="Task{T}"/> representing the async operation</returns> private async Task TestBadAddressAsync(string address, bool asException = false) { TestMocks mocks = new TestMocks(); MemoryMessageBuilder messageBuilder = new MemoryMessageBuilder(); mocks.Connection.SetupGet(c => c.CurrentMessage).Returns(messageBuilder); RcptToVerb verb = new RcptToVerb(); if (!asException) { await verb.Process(mocks.Connection.Object, new SmtpCommand("TO " + address)).ConfigureAwait(false); mocks.VerifyWriteResponseAsync(StandardSmtpResponseCode.SyntaxErrorInCommandArguments); } else { SmtpServerException e = await Assert.ThrowsAsync <SmtpServerException>(() => verb.Process(mocks.Connection.Object, new SmtpCommand("TO " + address))).ConfigureAwait(false); Assert.Equal((int)StandardSmtpResponseCode.SyntaxErrorInCommandArguments, e.SmtpResponse.Code); } Assert.Equal(0, messageBuilder.Recipients.Count); }
/// <summary> /// /// </summary> /// <param name="messageData">The messageData<see cref="string"/></param> /// <param name="expectedData">The expectedData<see cref="string"/></param> /// <returns>A <see cref="Task{T}"/> representing the async operation</returns> private async Task TestGoodDataAsync(string[] messageData, string expectedData) { TestMocks mocks = new TestMocks(); MemoryMessageBuilder messageBuilder = new MemoryMessageBuilder(); mocks.Connection.SetupGet(c => c.CurrentMessage).Returns(messageBuilder); mocks.ServerBehaviour.Setup(b => b.GetMaximumMessageSize(It.IsAny <IConnection>())).ReturnsAsync((long?)null); int messageLine = 0; mocks.Connection.Setup(c => c.ReadLine()).Returns(() => Task.FromResult(messageData[messageLine++])); DataVerb verb = new DataVerb(); await verb.Process(mocks.Connection.Object, new SmtpCommand("DATA")).ConfigureAwait(false); mocks.VerifyWriteResponseAsync(StandardSmtpResponseCode.StartMailInputEndWithDot); mocks.VerifyWriteResponseAsync(StandardSmtpResponseCode.OK); using (StreamReader dataReader = new StreamReader(await messageBuilder.GetData().ConfigureAwait(false), Encoding.UTF8)) { Assert.Equal(expectedData, dataReader.ReadToEnd()); } }