Skip to content

Instantly share code, notes, and snippets.

@BlitzkriegSoftware
Created February 4, 2019 19:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BlitzkriegSoftware/3fdcdd2d180517669aa91274fdf50220 to your computer and use it in GitHub Desktop.
Save BlitzkriegSoftware/3fdcdd2d180517669aa91274fdf50220 to your computer and use it in GitHub Desktop.
Send an e-mail from .NET Core 2.1+
using System.Net;
using System.Net.Mail;
namespace testemail
{
public static class SendMailer
{
public static void SendMail(EmailDTO dto)
{
SmtpClient client = new SmtpClient(dto.address);
if (!string.IsNullOrEmpty(dto.username))
{
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential(dto.username, dto.password);
}
client.DeliveryFormat = SmtpDeliveryFormat.International;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.Host = dto.address;
client.Port = dto.portNumber;
MailMessage mailMessage = new MailMessage();
mailMessage.From = new MailAddress(dto.from);
mailMessage.Body = dto.body;
mailMessage.Subject = dto.subject;
foreach (var e in dto.replytolist) if (!string.IsNullOrWhiteSpace(e)) mailMessage.ReplyToList.Add(new MailAddress(e));
foreach (var e in dto.tolist) if (!string.IsNullOrWhiteSpace(e)) mailMessage.To.Add(new MailAddress(e));
foreach (var e in dto.cclist) if (!string.IsNullOrWhiteSpace(e)) mailMessage.CC.Add(new MailAddress(e));
client.Send(mailMessage);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment