Created
February 4, 2019 19:51
-
-
Save BlitzkriegSoftware/3fdcdd2d180517669aa91274fdf50220 to your computer and use it in GitHub Desktop.
Send an e-mail from .NET Core 2.1+
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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