繁体   English   中英

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

[英]Problem sending mail to mlutiple users in C#

你好

我正在尝试开发一个wpf应用程序,以便使用Microsoft Outlook应用程序发送批量电子邮件。 我正在使用一个datagridview,将从中提取cc和附件。 此外,每封邮件都会有多个to和cc。 如果“收件人”和/或“抄送”字段包含单个邮件地址,则可以使用。 但是,当添加多个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!!");
    }

等待专家的帮助。 提前致谢。

此致 Subrata

Regex.Split的第二个参数应为正则表达式模式。 如此匹配,; 作为分隔符,您可以使用\\s*[,;]\\s*\\s*允许在分隔符周围使用空白):

string[] tos = Regex.Split(to, @"\s*[,;]\s*");

请注意,这会在字符串上使用@符号来保留转义符。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM