首页 > 解决方案 > C# 从 datagridview 列获取电子邮件地址并使用 Outlook 发送电子邮件

问题描述

我正在尝试开发一个应用程序(C# WinForms),试图从 datagridview(列地址)获取电子邮件列表并使用 Outlook 应用程序将电子邮件发送到整个列表。下面是数据网格视图:

    ADDRESS        NAME    ID    GROUP
mail1@mail.com     Josh   0011   Main
mail2@mail.com     John   0012   Web
mail3@mail.com     Jeff   0013   Sale
mail4@mail.com     Anna   0014   Lawy
mail5@mail.com     Brun   0015   Sale

但我坚持从列中获取信息并使用它向整个列表发送电子邮件。

我尝试了什么:

            dr = comando.ExecuteReader();
            //SQL query to get data from DB suppressed
            int nColunas = dr.FieldCount;
            string[] linhaDados = new string[nColunas];
            while (dr.Read())
            {
                for (int a = 0; a < nColunas; a++)
                {
                    if (dr.GetFieldType(a).ToString() == "System.Int32")
                    {
                        linhaDados[a] = dr.GetInt32(a).ToString();
                    }
                    if (dr.GetFieldType(a).ToString() == "System.String")
                    {
                        linhaDados[a] = dr.GetString(a).ToString();
                    }
                    if (dr.GetFieldType(a).ToString() == "NULL")
                    {
                        MessageBox.Show("No data!");
                    }
                }
                dataGridView1.Rows.Add(linhaDados);
            }
            conn.Close();

然后检查列(索引 0)以收集电子邮件地址到字符串“数据”:

                        string[] arrEmail = new string[dataGridViewMail.Rows.Count];
                        for (int i = 0; i < dataGridViewMail.Rows.Count - 1; i++)
                        {
                            arrEmail[i] = dataGridViewMail.Rows[i].Cells[1].Value.ToString();
                        }

                        string data = arrEmail.ToString();
                        

                        try
                        {
                            Outlook.Application oApp = new Outlook.Application();
                            Outlook.MailItem oMsg = (Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);
                            oMsg.HTMLBody = "<b>Dear all, this is an email for test purposes.</br>";
                            int iPosition = (int)oMsg.Body.Length + 1;
                            oMsg.Subject = "Test mail";
                            Outlook.Recipients oRecips = (Outlook.Recipients)oMsg.Recipients;
                            Outlook.Recipient oRecip1 = (Outlook.Recipient)oRecips.Add(data);

                            oRecip1.Resolve();
                            oMsg.Send();
                            oRecip1 = null;
                            oRecips = null;
                            oRecip1 = null;
                            oMsg = null;
                            oApp = null;
                        }
                        
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.ToString());
                        }

但无法发送任何电子邮件。有任何想法吗?

非常感谢。

标签: c#winformsemaildatagridviewoutlook

解决方案


Recipients.Add采用单个电子邮件地址或名称。您需要为每个收件人调用它。

或者,您可以将MailItem.To//属性设置为单独的名称或地址列表。CCBCC";"


推荐阅读