Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在C中向多个用户发送邮件时出现问题#_C#_Wpf_Datagridview_Bulk Mail - Fatal编程技术网

C# 在C中向多个用户发送邮件时出现问题#

C# 在C中向多个用户发送邮件时出现问题#,c#,wpf,datagridview,bulk-mail,C#,Wpf,Datagridview,Bulk Mail,你好, 我正在尝试开发一个wpf应用程序,用于使用Microsoft outlook应用程序发送批量电子邮件。我使用的是一个datagridview,将从中提取到、抄送和附件。此外,对于每封邮件,将有多个收件人和抄送。如果“收件人”和/或“抄送”字段包含单个邮件地址,则该字段有效。但是当添加multiple to或cc时,会出现以下错误 System.Runtime.InteropServices.COMException:“Outlook无法识别一个或多个名称。” 我认为这是邮件分离的问题,并

你好,

我正在尝试开发一个wpf应用程序,用于使用Microsoft outlook应用程序发送批量电子邮件。我使用的是一个datagridview,将从中提取到、抄送和附件。此外,对于每封邮件,将有多个收件人和抄送。如果“收件人”和/或“抄送”字段包含单个邮件地址,则该字段有效。但是当添加multiple to或cc时,会出现以下错误

System.Runtime.InteropServices.COMException:“Outlook无法识别一个或多个名称。”

我认为这是邮件分离的问题,并努力解决它

这是我的datagridview数据

这是发送邮件的代码

   private void BtnSendMail_Click(object sender, RoutedEventArgs e)
    {

        foreach (DataRowView rows in dgdData.ItemsSource)
        {

            try
            {
                Outlook.Application oApp = new Outlook.Application();
                Outlook.MailItem oMsg = (Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);
                Outlook.Recipients oRecips = oMsg.Recipients;

                List<string> sTORecipsList = new List<string>();
                List<string> sCCRecipsList = new List<string>();


                var to = rows.Row.ItemArray[1].ToString();            
                var cc = rows.Row.ItemArray[2].ToString();
                var attachment = rows.Row.ItemArray[3].ToString();
                //var status = rows.Row.ItemArray[0] ="Sending";

                if (to.Contains(",") || to.Contains(";") || to.Contains(" "))
                {
                    string[] tos = Regex.Split(to, ",; ");
                    // string[] lines = Regex.Split(value, "\r\n");
                    for (int i = 0; i < tos.Length; i++)
                    {
                        //mail.To.Add(new MailAddress(tos[i]));

                        sTORecipsList.Add(tos[i]);

                    }
                }
                else
                {
                    sTORecipsList.Add(to);

                }

                if (cc.Contains(",") || cc.Contains(";") || cc.Contains(" "))
                {
                    string[] ccs = Regex.Split(cc, ",; ");
                    // string[] lines = Regex.Split(value, "\r\n");
                    for (int i = 0; i < ccs.Length; i++)
                    {
                        //mail.To.Add(new MailAddress(tos[i]));

                        sCCRecipsList.Add(ccs[i]);

                    }
                }
                else
                {
                    sCCRecipsList.Add(cc);

                }



                oMsg.Body = "Sample Text";///rtbBody.Text.ToString();


                string sDisplayName = "MyAttachment";
                int iPosition = oMsg.Body.Length + 1;
                int iAttachType = (int)Outlook.OlAttachmentType.olByValue;
                Outlook.Attachment oAttach = oMsg.Attachments.Add(attachment, iAttachType, iPosition, sDisplayName);

                oMsg.Subject = txtSubject.Text.ToString();

                foreach (string t in sTORecipsList)
                {
                    Outlook.Recipient recipTo = oMsg.Recipients.Add(t);
                    recipTo.Type = (int)Outlook.OlMailRecipientType.olTo;
                }

                foreach (string c in sCCRecipsList)
                {
                    Outlook.Recipient recipCc = oMsg.Recipients.Add(c);
                    recipCc.Type = (int)Outlook.OlMailRecipientType.olCC;
                }


                oMsg.Recipients.ResolveAll();

                Thread.Sleep(2000);

                oMsg.Send();
                rows.Row.ItemArray[0] = "Sent";

                sTORecipsList = null;
                sCCRecipsList = null;
                //recipTo = null;
                oRecips = null;
                oAttach = null;
                oMsg = null;
                oApp = null;


                Thread.Sleep(2000);



            }

            catch (Exception)
            {
                throw;
            }



        }

        MessageBox.Show("All message sent!!");
    }
private void BtnSendMail\u单击(对象发送者,路由目标e)
{
foreach(dgdData.ItemsSource中的DataRowView行)
{
尝试
{
Outlook.Application oApp=新建Outlook.Application();
Outlook.MailItem oMsg=(Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);
Outlook.Recipients oRecips=oMsg.Recipients;
List sTORecipsList=新列表();
List SCCRICIPSLIST=新列表();
var to=rows.Row.ItemArray[1].ToString();
var cc=rows.Row.ItemArray[2].ToString();
var attachment=rows.Row.ItemArray[3].ToString();
//var status=rows.Row.ItemArray[0]=“正在发送”;
if(to.Contains(“,”)| to.Contains(“;”)| to.Contains(“))
{
字符串[]tos=Regex.Split(到“,;”);
//字符串[]行=Regex.Split(值“\r\n”);
for(int i=0;i
等待专家的帮助。提前谢谢

问候,
Subrata

Regex.Split的第二个参数应该是Regex模式。所以要匹配
作为分隔符,您可以使用类似
\s*[,;]\s*
的模式(
\s*
允许在分隔符周围使用可选空格):


注意:这使用字符串上的
@
符号来保留转义。

Regex.Split的第二个参数应该是Regex模式。所以要匹配
作为分隔符,您可以使用类似
\s*[,;]\s*
的模式(
\s*
允许在分隔符周围使用可选空格):


注意:这使用字符串上的
@
符号来保留转义。

另一个请求。。如何更改datagridview中单元格的值//var status=rows.Row.ItemArray[0]=“正在发送”;我想在发送邮件时将状态列的值设置为“发送”。邮件发送后,它将变为“已发送”。@Subrataholder可能会打开一个新的SO问题来解决这个问题?另一个请求。。如何更改datagridview中单元格的值//var status=rows.Row.ItemArray[0]=“正在发送”;我想在发送邮件时将状态列的值设置为“发送”。邮件发送后,它将变为“已发送”。@Subrataholder可能会提出一个新的SO问题来解决这个问题?
string[] tos = Regex.Split(to, @"\s*[,;]\s*");