Код: Выделить всё
public interface IEmailService
{
public string From { get; set; }
public string To { get; set; }
public string Cc { get; set; }
public string Subject { get; set; }
public string Body { get; set; }
public bool Send();
}
public class EmailService: IEmailService
{
private readonly IOptions _settings;
private readonly IEmailLogService _logService;
public string From { get; set; }
public string To { get; set; }
public string Cc { get; set; }
public string Subject { get; set; }
public string Body { get; set; }
public EmailService(IOptions settings, IEmailLogService logService)
{
_settings = settings;
_logService = logService;
}
public bool Send()
{
SmtpClient smtpClient = new SmtpClient(_settings.SmtpHost);
MailMessage message= new System.Net.Mail.MailMessage(new MailAddress(From), new MailAddress(To));
message.Subject = Subject;
message.Body = Body;
if (Cc != null)
message.CC.Add(Cc);
smtpClient.Send(message);
logService.log();
return true;
}
}
Код: Выделить всё
public interface ISomeService
{
public void SendEmails();
}
public class SomeService: ISomeService
{
private readonly IEmailService _emailService;
public SomeService(IEmailService emailService)
{
_emailService = emailService;
}
public void SendEmails()
{
// Send first email
_emailService.From = "sender@example.com";
_emailService.To = "recipient@example.com";
_emailService.Cc= "recipient_cc@example.com";
_emailService.Subject = "My Subject";
_emailService.Body= "My Message";
_emailService.Send();
// Send 2nd email
_emailService.From = "sender@example.com";
_emailService.To = "recipient2@example.com";
_emailService.Subject = "My Subject 2";
_emailService.Body= "My Message 2";
_emailService.Send();
}
}
Подробнее здесь: https://stackoverflow.com/questions/784 ... -injection