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