sendgrid从C#Winform App发送到电子邮件列表

时间:2019-03-22 12:49:12

标签: c# sendgrid

我有一个Winform应用程序,我想从中发送电子邮件,我可以使用sendgrid并输入电子邮件地址来发送。问题是我要发送的电子邮件将根据情况发送给不同的人,因此“发送至”必须动态。

我有一个SQL数据库,该数据库存储了人们想通知的事件的电子邮件地址,在准备发送电子邮件时会拉出该列表,但我不知道如何将这些项目放入{{ 1}}是Sendgrid想要的。

<EmailAddress>

警报消息类

            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("test@gmail.com", "test from App");

                //Standard Email lists
                //var tos = new List<EmailAddress>
                //{
                //new EmailAddress("test@gmail.com"),
                //new EmailAddress("test2@gmail.com")
                //};

                //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);
        }

1 个答案:

答案 0 :(得分:1)

假设您已成功从Dapper查询中获取电子邮件列表

List<AlertMessages> EmailList = conn.Query<AlertMessages>("GetEmailList", param, commandType: CommandType.StoredProcedure).ToList<AlertMessages>();

您的AlertMessages类具有string个属性,就像EmailName一样

class AlertMessages
{
    ...
    public string Email { get; set; }
    public string Name { get; set; }
    ...
}

然后,您可以将List<AlertMessages>展平为List<EmailAddress>,例如

var tos = EmailList.Select(item => new EmailAddress { Email = item.Email, Name = item.Name }).ToList();

其中item是类型为AlertMessages的选择迭代变量,而item.Emailitem.Name是相应迭代变量的属性。

然后将上面的tos列表传递给CreateSingleEmailToMultipleRecipients方法。