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"); }
public byte[] Send(MailMessage message, DirectoryInfo tempDir = null, bool returnBlob = false, bool needDispositionNotification = false, MailAddress notificationTo = null) { var mailContent = new byte[] { }; switch (_type) { case MailClientType.Smtp: // Se è richiesto il salvataggio del blob if (returnBlob) { if (tempDir != null) { // se è definita la tempo allora posso esportare mailContent = message.ToArray(tempDir); } else { // se non è definita allora si interrompe tutto throw new Exception("Impostare la directory TEMP"); } } FileLogger.Debug(Logger, string.Format("[SendMails] - needDispositionNotification {0}", needDispositionNotification)); //Specifico se notificare al mittente la notifiche di lettura e ricezione if (needDispositionNotification && notificationTo != null) { FileLogger.Debug(Logger, string.Format("[SendMails] - Sender {0}", notificationTo.ToString())); message.Headers.Add("Disposition-Notification-To", notificationTo.ToString()); message.Headers.Add("Return-Receipt-To", notificationTo.ToString()); } // Se sono qui significa che posso inviare il messaggio. // se avessi avuto problemi sarei uscito prima SmtpClientProvider.Send(message); break; case MailClientType.Exchange: mailContent = EwsClientProvider.Send(message, returnBlob, needDispositionNotification, notificationTo); break; case MailClientType.Limilabs: mailContent = LimilabsClientProvider.Send(message, returnBlob, needDispositionNotification, notificationTo); break; } return(mailContent); }
public async Task ShouldSendEmailUsingSmtp() { var provider = new SmtpClientProvider(_settings.Object); var message = new EmailMessage("*****@*****.**", "*****@*****.**") { Subject = "Unit test" }; message.AddBody(EmailMessageBody.FromText("Hello world")); 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(); }
public EmailService(SmtpClientProvider smtpClientProvider) { this._smtpClientProvider = smtpClientProvider; }