Reputation: 73
I have a Winform app that I would like to send an email out from, I am able to do so using sendgrid and typed in email addresses. the problem is the the emails I want to send will go to different people depending on the situation, so the "to field" needs to be dynamic.
I have an SQL database that stores email address for events that people want to be notified of, I pull that list up when getting ready to send the email but I cannot figure out how to get those items into the list of <EmailAddress>
that Sendgrid wants.
try
{
using (SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["MfgDataCollector"].ToString()))
{
var apiKey = "SendGrid Key";
var client = new SendGridClient(apiKey);
var from = new EmailAddress("[email protected]", "test from App");
//Standard Email lists
//var tos = new List<EmailAddress>
//{
//new EmailAddress("[email protected]"),
//new EmailAddress("[email protected]")
//};
//Query to get emails from SQL using Dapper
DynamicParameters param = new DynamicParameters();
param.Add("@Zone", Variables.Zone);
param.Add("@Gatelevel", Lbl_GateLevel.Text);
List<AlertMessages> EmailList = conn.Query<AlertMessages>("GetEmailList", param, commandType: CommandType.StoredProcedure).ToList<AlertMessages>();
//Lost here??? Not sure how to get EmailList to EmailAddresses
var tos = new List<EmailAddress> { };
string subject = txt_Subject.Text;
string plainTextContent = txt_Body.Text;
var htmlContent = txt_Body.Text;
var showAllRecipients = true; // Set to true if you want the recipients to see each others email addresses
var msg = MailHelper.CreateSingleEmailToMultipleRecipients(from,
tos,
subject,
plainTextContent,
htmlContent,
showAllRecipients
);
var response = await client.SendEmailAsync(msg);
MessageBox.Show("SendGrid Completed");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Send Email Notification - SendGrid - Oops!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
Alert Messages Class
public class AlertMessages
{
public int ID { get; set; }
public string EmailAddress { get; set; }
public int ZoneID { get; set; }
public string Zone { get; set; }
public string GateLevel { get; set; }
public string Emails { get; set; }
}
Upvotes: 1
Views: 620
Reputation: 9771
Assuming that you are successfully getting list of email from your Dapper query
List<AlertMessages> EmailList = conn.Query<AlertMessages>("GetEmailList", param, commandType: CommandType.StoredProcedure).ToList<AlertMessages>();
And your AlertMessages
class have string
properties are Email
and Name
like
class AlertMessages
{
...
public string Email { get; set; }
public string Name { get; set; }
...
}
Then you can flatten your List<AlertMessages>
to List<EmailAddress>
like,
var tos = EmailList.Select(item => new EmailAddress { Email = item.Email, Name = item.Name }).ToList();
Where item
is a select iterator variable of type of AlertMessages
and item.Email
and item.Name
are the properties of respective iterator variable.
And then pass the above tos
list to CreateSingleEmailToMultipleRecipients
method.
Upvotes: 1