Last active
May 10, 2022 17:37
-
-
Save PontiacGTX/76e60b588989f6c7350fa7ade05142e4 to your computer and use it in GitHub Desktop.
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.Mail; | |
MyEmailBatch myEmailBatch = new MyEmailBatch | |
{ | |
Message = "Hello this is a general shared message", | |
Sender = "name@email.com", | |
Recipients = new[] { "name1@email.com", "name2@email.com", "name3@email.com" }, | |
SenderName = "Me" | |
}; | |
MailAddress sender = new MailAddress(myEmailBatch.Sender, myEmailBatch.SenderName); | |
System.Net.Mail.SmtpClient client; | |
var requests = myEmailBatch.Recipients.Select(async x => { | |
MailAddress to = new MailAddress(x); | |
using (MailMessage message = new MailMessage(sender, to)) | |
{ | |
message.Subject = myEmailBatch.Subject; | |
message.Body = myEmailBatch.Message; | |
using (client = new System.Net.Mail.SmtpClient | |
{ | |
Host = "smtp.gmail.com", | |
Port = 587, | |
EnableSsl = true, | |
UseDefaultCredentials = false, | |
Credentials = new System.Net.NetworkCredential(myEmailBatch.Sender, "password") | |
}) | |
{ | |
await client.SendMailAsync(message); | |
} | |
} | |
Thread.Sleep(1000); | |
}); | |
await Task.WhenAll(requests); | |
MailAddress from = new MailAddress(myEmailBatch.Sender, myEmailBatch.SenderName); | |
MailAddress to = new MailAddress(myEmailBatch.Recipients[0]); | |
MailMessage message = new MailMessage(from, to); | |
message.Subject = "Subject"; | |
string body = "Body"; | |
message.Body = body; | |
myEmailBatch.Recipients.Select(x => | |
{ | |
message.Bcc.Add(new MailAddress(x)); | |
return true; | |
}); | |
using (client = new System.Net.Mail.SmtpClient | |
{ | |
Host = "smtp.gmail.com", | |
Port = 587, | |
EnableSsl = true, | |
UseDefaultCredentials = false, | |
Credentials = new System.Net.NetworkCredential(myEmailBatch.Sender, "password") | |
}) | |
{ | |
await client.SendMailAsync(message); | |
} | |
class MyEmailBatch | |
{ | |
public string Message { get; set; } | |
public string Sender { get; set; } | |
public string SenderName { get; set; } | |
public string Subject { get; set; } | |
public IList<string> Recipients { get; set; } | |
}; | |
class EmailSenderStream | |
{ | |
MyEmailBatch myEmailBatch = new MyEmailBatch | |
{ | |
Message = "Hello this is a general shared message", | |
Sender = "name@email.com", | |
Recipients = new[] { "name1@email.com", "name2@email.com", "name3@email.com" }, | |
SenderName = "Me" | |
}; | |
public async IAsyncEnumerable<string> GetEmails() | |
{ | |
foreach (var recipients in myEmailBatch.Recipients.Select(x => x)) | |
{ | |
yield return recipients; | |
} | |
} | |
public async Task SendEmail() | |
{ | |
await foreach (var recipient in GetEmails()) | |
{ | |
MailAddress to = new MailAddress(recipient); | |
MailAddress from = new MailAddress(myEmailBatch.Sender); | |
using (MailMessage message = new MailMessage(from, to)) | |
{ | |
message.Subject = myEmailBatch.Subject; | |
message.Body = myEmailBatch.Message; | |
using (var client = new System.Net.Mail.SmtpClient | |
{ | |
Host = "smtp.gmail.com", | |
Port = 587, | |
EnableSsl = true, | |
UseDefaultCredentials = false, | |
Credentials = new System.Net.NetworkCredential(myEmailBatch.Sender, "password") | |
}) | |
{ | |
await client.SendMailAsync(message); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment