c# - 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());
}
但无法发送任何电子邮件。有任何想法吗?
非常感谢。
解决方案
Recipients.Add
采用单个电子邮件地址或名称。您需要为每个收件人调用它。
或者,您可以将MailItem.To
//属性设置为单独的名称或地址列表。CC
BCC
";"
推荐阅读
- javascript - 使用 three.js 导入 3d 模型
- wordpress - 在多个商店中复制 Woocommerce 产品
- ruby-on-rails-5 - Rails:用于 `invoice.payment_action_required` 和 `invoice.payment_failed` 的条纹 webhook
- react-native - 如何在本机反应中删除弯曲之间的空间?(flexWrap)
- aws-lambda - 使用 Java 创建 URL 签名 - Cloudfront - InvalidKey - Unknown Key
- python - Python模块'scm'没有属性'Open'
- regex - 使用 Dockerhub Regex 进行自动构建
- json - 解析包含 json 字符串的 json
- kubernetes - Kubernetes 的 pod 状态为 CrashLoopBackOff 但没有显示日志
- firebase - 我在运行我的应用程序(Flutter、FireBase)时收到此 E/flutter (25055) 错误