首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在DataTable中向多个收件人发送电子邮件

在DataTable中向多个收件人发送电子邮件
EN

Stack Overflow用户
提问于 2019-03-15 08:22:17
回答 1查看 158关注 0票数 1

我想将数据表中的所有电子邮件添加到电子邮件的To:中-我已经尝试了下面的代码,但它在电子邮件地址中的每个字符后面添加了一个分号。我需要如何重写这篇文章,以便添加每个电子邮件地址?

foreach (DataRow dr in datatatblefirst.Rows)  
{
  foreach (char eadd in r["Email"].ToString())
  {
    Outlook.Application oApp = new Outlook.Application();
    Outlook.MailItem oMsg = (Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);
    oMsg.HTMLBody = "Body";
    oMsg.Subject = "Your Subject will go here.";
    Outlook.Recipients oRecips = (Outlook.Recipients)oMsg.Recipients;
    foreach (char email in r["Email"].ToString())
      oRecips.Add(eadd.ToString());
    oMsg.Save();
    oRecip = null;
    oRecips = null;
    oMsg = null;
    oApp = null; 
  }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-03-15 08:42:39

看起来您正在迭代电子邮件地址中的每个char

如果r["Email"]包含一个电子邮件地址,您可以遍历数据行。下面的代码将为每个电子邮件地址创建一封电子邮件:

foreach (DataRow dr in datatatblefirst.Rows)  
{  
    Outlook.Application oApp = new Outlook.Application();
    Outlook.MailItem oMsg = (Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);
    oMsg.HTMLBody = "Body";
    oMsg.Subject = "Your Subject will go here.";
    Outlook.Recipients oRecips = (Outlook.Recipients)oMsg.Recipients;

    string emailAddress = r["Email"].ToString();
    Outlook.Recipient oRecip = (Outlook.Recipient)oRecips.Add(emailAddress);
    oRecip.Resolve();

    oMsg.Save();
    //oMsg.Send();
    oRecip = null;
    oRecips = null;
    oMsg = null;
    oApp = null; 
}

要仅创建一封电子邮件并发送到多个地址,请在foreach之前创建电子邮件

Outlook.Application oApp = new Outlook.Application();
Outlook.MailItem oMsg = (Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);
oMsg.HTMLBody = "Body";
oMsg.Subject = "Your Subject will go here.";
Outlook.Recipients oRecips = (Outlook.Recipients)oMsg.Recipients;

foreach (DataRow dr in datatatblefirst.Rows)  
{  
    string emailAddress = r["Email"].ToString();
    Outlook.Recipient oRecip = (Outlook.Recipient)oRecips.Add(emailAddress);
    oRecip.Resolve();
}

oMsg.Save();
//oMsg.Send();
oRecips = null;
oMsg = null;
oApp = null; 

oRecip.Resolve();不是必需的。如果通讯录中存在该联系人,它会将电子邮件地址格式化为Some Name <somename@email.com>

无需创建或解析Recipient对象,只需使用oRecips.Add(emailAddress);即可添加地址。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55173881

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档