【问题标题】:sendgrid send to List of emails from C# Winform Appsendgrid 从 C# Winform App 发送到电子邮件列表
【发布时间】:2019-03-22 12:49:12
【问题描述】:

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

我有一个 SQL 数据库,用于存储人们希望收到通知的事件的电子邮件地址,我在准备发送电子邮件时拉出该列表,但我不知道如何将这些项目放入 @987654321 的列表中@Sendgrid 想要的。

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

警报消息类

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

【问题讨论】:

  • 在你的AlertMessages 类中EmailAddressEmails 有什么区别?

标签: c# sendgrid


【解决方案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&lt;AlertMessages&gt; 扁平化为List&lt;EmailAddress&gt; 喜欢,

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

其中itemAlertMessages 类型的选择迭代器变量,item.Emailitem.Name 是各自迭代器变量的属性。

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

【讨论】:

  • 效果很好,非常感谢!!我花了几个小时尝试不同的事情,但没有什么对我有用的一个快速问题,bam问题解决了!
  • 哦,很高兴听到,如果你觉得不错,请在答案左侧打勾,让它变成绿色;)
猜你喜欢
  • 2013-05-03
  • 2018-11-08
  • 1970-01-01
  • 2018-08-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-30
相关资源
最近更新 更多