Hej,
dodałem do projektu plik .html
, w nim umieściłem miejsca, w które chcę coś wrzucić dynamicznego. Używając StreamReader
a odczytuję html i z pomocą string format pakuję pojedyncze dane np.
var rootPath = Directory.GetCurrentDirectory();
var filePath = $"{rootPath}\\MailTemplates\\PlacedSalesOrder.html";
if (!System.IO.File.Exists(filePath))
{
return NotFound();
}
string HtmlBody = "";
using(StreamReader stream = System.IO.File.OpenText(filePath))
{
HtmlBody = stream.ReadToEnd();
}
string messageBody = string.Format(HtmlBody, "xxxxxxx", "yyyyyyy");
_emailService.Send("[email protected]", "Test From Net Core Application", messageBody);
i metoda .Send()
(uzywam MailKit).
public void Send(string to, string subject, string html, string from = null)
{
var email = new MimeMessage();
email.From.Add(MailboxAddress.Parse(from ?? _mailBoxSettings.Username));
email.To.Add(MailboxAddress.Parse(to));
email.Subject = subject;
email.Body = new TextPart(TextFormat.Html) { Text = html };
// send email
using var smtp = new SmtpClient();
smtp.Connect(_mailBoxSettings.Host, _mailBoxSettings.Port, SecureSocketOptions.SslOnConnect);
smtp.Authenticate(_mailBoxSettings.Username, _mailBoxSettings.Password);
smtp.Send(email);
smtp.Disconnect(true);
}
to na razie dirty code ale działa.
Potrzebuje jeszcze funkcjonalności gdzie user wysyła do kogoś jakieś zestawienie np. listę niezapłaconych faktur tzn. potrzebny jest template.html, do którego mógłbym przekazać listę i wyświetlić iterując.
Jak to osiągnąć?