Hi,
I'm trying to develop a wpf application for sending bulk email using Microsoft outlook app. I'm using a datagridview from which to, cc, and attachment will be extracted. Besides, for each mail, there'll be multiple to(s) and cc(s). If to and/or cc field contain single mail address, it works. But when multiple to or cc added, it gets the following error.
System.Runtime.InteropServices.COMException: 'Outlook does not recognize one or more names. '
I things it's issue with mail separation and put some efforts to resolve it.
Here's the code for sending mail.
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!!");
}
Waiting for experts help. Thanks in advance.
Regards, Subrata
The second argument to Regex.Split
should be a regex pattern. So to match ,
or ;
as a delimiter, you'd use a pattern like \s*[,;]\s*
(the \s*
allows optional whitespace around the delimiters):
string[] tos = Regex.Split(to, @"\s*[,;]\s*");
Note this uses the @
symbol on the string to preserve the escapes.