通过Interop库C#向多个收件人发送邮件
发布时间:2020-12-15 17:28:00 所属栏目:百科 来源:网络整理
导读:我正在开发一个具有邮件发送选项的桌面应用程序.我有以下代码,它只适用于1个收件人: DialogResult status;status = MessageBox.Show("Some message","Info",MessageBoxButtons.OKCancel,MessageBoxIcon.Information);if (status == DialogResult.OK){ try {
我正在开发一个具有邮件发送选项的桌面应用程序.我有以下代码,它只适用于1个收件人:
DialogResult status; status = MessageBox.Show("Some message","Info",MessageBoxButtons.OKCancel,MessageBoxIcon.Information); if (status == DialogResult.OK) { try { // Create the Outlook application. Outlook.Application oApp = new Outlook.Application(); // Create a new mail item. Outlook.MailItem oMsg = (Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem); // Set HTMLBody. //add the body of the email oMsg.HTMLBody = "<html>" + "<body>" + "some html text" + "</body>" + "</html>"; int iPosition = (int)oMsg.Body.Length + 1; //Subject line oMsg.Subject = txt_mailKonu.Text; oMsg.Importance = Outlook.OlImportance.olImportanceHigh; // Recipient Outlook.Recipients oRecips = (Outlook.Recipients)oMsg.Recipients; //Following line causes the problem Outlook.Recipient oRecip = (Outlook.Recipient)oRecips.Add(senderForm.getRecipientList().ToString()); oRecip.Resolve(); //oRecip.Resolve(); // Send. oMsg.Send(); // Clean up. oRecip = null; oRecips = null; oMsg = null; oApp = null; MessageBox.Show("Successful",MessageBoxButtons.OK,MessageBoxIcon.Information); } catch (Exception) { MessageBox.Show("Failed","Eror",MessageBoxIcon.Error); } } 我在粗线处得到错误,我在以下模式中添加多个收件人: 它适用于1个地址,但是当我将多个地址分开时,它会抛出COM异常 – Outlook无法解析一个或多个名称. 希望你能帮助我. 解决方法
您是否尝试将多个收件人添加到oMsg.Recipients?
// I assume that senderForm.getRecipientList() returns List<String> foreach(String recipient in senderForm.getRecipientList()) { Outlook.Recipient oRecip = (Outlook.Recipient)oRecips.Add(recipient); oRecip.Resolve(); } 如果需要,你可以爆炸发送者使用senderForm.getRecipientList().ToString() String [] rcpts = senderForm.getRecipientList().ToString().Split(new string[] { "; " },StringSplitOptions.None); 并在foreach循环中使用新对象. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |